2011-08-11 6 views

答えて

5

RESTサービスを調べることをおすすめします。基本的な構造は、あなたのアンドロイドアプリのHTTP要求(好ましくは別のスレッド)をサーバーに送信し、サーバーがxmlまたはjsonで応答するようにすることです。

私は頻繁に使用するスレッドのHTTPポストクラスです。

import java.util.ArrayList; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.params.BasicHttpParams; 
import org.apache.http.params.HttpConnectionParams; 
import org.apache.http.params.HttpParams; 
import org.apache.http.util.EntityUtils; 
import android.os.Handler; 
import android.os.Message; 

public class HttpPostThread extends Thread { 
    public static final int FAILURE = 0; 
    public static final int SUCCESS = 1; 
    public static final String VKEY = "FINDURB#V0"; 

    private final Handler handler; 
    private String url; 
    ArrayList<NameValuePair> pairs; 
public HttpPostThread(String Url, ArrayList<NameValuePair> pairs, final Handler handler) 
{ 
this.url =Url; 
    this.handler = handler; 
    this.pairs = pairs; 
    if(pairs==null){ 
     this.pairs = new ArrayList<NameValuePair>(); 
    } 
} 


@Override 
public void run() 
{ 
try { 

HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost(url); 
HttpParams httpParameters = new BasicHttpParams(); 
int timeoutConnection = 3000; 
HttpConnectionParams.setConnectionTimeout(httpParameters, 
     timeoutConnection); 
if(pairs!=null) 
    post.setEntity(new UrlEncodedFormEntity(pairs)); 
    HttpResponse response = client.execute(post); 
    HttpEntity entity = response.getEntity(); 
    String answer = EntityUtils.toString(entity); 
    Message message = new Message(); 
      message.obj = answer; 
      message.what = HttpPostThread.SUCCESS; 
      handler.sendMessage(message); 


} catch (Exception e) { 
    e.printStackTrace(); 
    handler.sendEmptyMessage(HttpPostThread.FAILURE); 
} 

} 
} 

サーバーと通信する必要があるときはいつでも、このようなことをします。

Handler handler = new Handler() 
    { 
     @Override 
     public void handleMessage(Message msg) 
     { 
      removeDialog(0); 
      switch (msg.what) 
      { 
      case HttpPostThread.SUCCESS: 
       String answer = (String)msg.obj; 
       if (answer != null) 
       { 
       try { 
        JSONObject jsonObj = new JSONObject(answer); 
        String message = jsonObj.getString("msg"); 
       } catch (JSONException e) { 
         e.printStackTrace(); 
       } 
       } 
       break; 

       case HttpPostThread.FAILURE: 
       // do some error handeling 
       break; 

       default: 
       break; 
      } 
     } 
} 
ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
pairs.add(new BasicNameValuePair("key", "value")); 
HttpPostThread thread = new HttpPostThread("http://serviceURL",pairs, handler); 
thread.start(); 

以下の質問にお答えするために、サービスは任意の数のテクノロジで実装できます。以下は、上記の例からキーと値のペアを取得するphpサービスの簡単な例です。

シンプルなPHPサービス

<?php 
    $value = $_POST['key']; 
    $msg "The value".$value. "was received by the service!"; 
    echo json_encode($msg); 
    ?> 

の例サーバがのhandleMessageが呼び出されると、答えの内部値が何であれ、あなたのPHPサービスのエコーも応答します。

+0

私はそれを修正しようとしましたが、コードgenはEclipseの字下げが気に入らない – NSjonas

+0

REST Webサービスを使用した要約の例を使って本当に素敵な答えです! +1 – Hiral

+0

すてきな答え+1。 –

関連する問題