调用php webservice json响应发送到android

时间:2011-08-11 02:52:08

标签: android json

在Android中,我想调用使用Json响应制作成php的webservice。 如何调用该Web服务以及如何将响应存储到数组中?

1 个答案:

答案 0 :(得分:5)

我建议调查REST服务。基本结构是让你的Android应用程序预先向服务器发出HTTP请求(最好是在一个单独的线程中),让服务器用xml或json进行响应。

这是我经常使用的线程http post类。

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服务回声。