调用php webservice json resonse在android中发送[英] Call php webservice json resonse to send in android

本文是小编为大家收集整理的关于调用php webservice json resonse在android中发送的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我是Android开发的新初学者. 我想调用与JSON响应一起制作的WebService. 如何调用该WebService以及如何将响应存储到数组中.

请帮助我.

thanx提前.

推荐答案

我会建议调查休息服务.基本结构是使您的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服务回声.

本文地址:https://www.itbaoku.cn/post/102677.html

问题描述

I am new beginner of the android developing. 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.

pls help me.

thanx in advance.

推荐答案

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.