开发者

Call php webservice json response to send in android

开发者 https://www.devze.com 2023-03-26 23:28 出处:网络
In Android I want to call webservice which is made into php with Json response. How to call that webservice and how to开发者_开发知识库 store the response into array?I would recommend looking into RES

In Android I want to call webservice which is made into php with Json response. How to call that webservice and how to开发者_开发知识库 store the response into array?


I would recommend looking into REST services. The basic structure is to have your android app preform HTTP requests(preferably in a separate thread) to the server and have the server respond with xml or json.

Heres a threaded http post class i use often.

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);
}

}
}

Whenever you need to communicate with the server you do something like this.

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();

To answer you question below, the service can be implemented with any number of technologies. Below is a simple example of a php service that gets the key/value pair from the example above.

Example of a simple PHP service

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

When the server responds handleMessage will be called and the value inside of answer be whatever your php service echos.

0

精彩评论

暂无评论...
验证码 换一张
取 消