调用PHP Web服务JSON resonse派的机器人 [英] Call php webservice json resonse to send in android
问题描述
我是机器人发展的新的初学者。 我想打电话给web服务它被做成PHP和JSON响应。 如何调用Web服务以及如何存储响应到数组。
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.
请帮我。
感谢名单提前。
推荐答案
我会建议寻找到REST服务。其基本结构是让你的Android应用程序preform HTTP请求(preferably在一个单独的线程)到服务器,并与XML或JSON服务器响应。
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.
下面有带螺纹的HTTP POST类我经常使用。
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();
要回答你下面的问题,服务可以与任意数量的技术来实现。下面是一个PHP服务,获取键/值对从上面的例子中的一个简单的例子。
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.
一个简单的PHP服务的示例
Example of a simple PHP service
<?php
$value = $_POST['key'];
$msg "The value".$value. "was received by the service!";
echo json_encode($msg);
?>
当服务器响应的handleMessage将被调用,答案里面的值是无论你的PHP服务回声。
When the server responds handleMessage will be called and the value inside of answer be whatever your php service echos.
这篇关于调用PHP Web服务JSON resonse派的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!