在android中为JSON响应执行从服务器获取/发布
当使用http get/post时,获取JSON响应的最有效方法是什么。显然,它们必须异步完成 注意:我已经在清单文件中启用了internet权限 张贴:在android中为JSON响应执行从服务器获取/发布,android,json,http,post,get,Android,Json,Http,Post,Get,当使用http get/post时,获取JSON响应的最有效方法是什么。显然,它们必须异步完成 注意:我已经在清单文件中启用了internet权限 张贴: HttpPost httpPost = new HttpPost("MYDOMAIN"); HttpResponse response = httpClient.execute(httpPost); 获取: HttpGet httpGet = new HttpGet("MYDOMAIN"); HttpResponse response =
HttpPost httpPost = new HttpPost("MYDOMAIN");
HttpResponse response = httpClient.execute(httpPost);
获取:
HttpGet httpGet = new HttpGet("MYDOMAIN");
HttpResponse response = httpClient.execute(httpGet);
HTTP请求必须异步完成。 首先确保您在AndroidManifest.xml中具有INTERNET权限 然后为请求创建一个类,以便可以重用它
public class Request extends AsyncTask<List<NameValuePair>, Void, String> {
Callback.JSONCallback callback;
String url;
String type;
public Request(String type, String url, Callback.JSONCallback callback) {
this.callback = callback;
extension = url;
this.type = type;
}
// What to do Async, in this case its POST/GET
protected String doInBackground(List<NameValuePair>... pairs) {
HttpClient httpClient = new DefaultHttpClient();
if (type.equals("POST")) {
HttpPost httpPost = new HttpPost(url);
try {
// Add your data
httpPost.setEntity(new UrlEncodedFormEntity(pairs[0], "UTF-8"));
// Execute HTTP Post Request
HttpResponse response = httpClient.execute(httpPost);
String result = EntityUtils.toString(response.getEntity());
return result;
} catch (Exception e) {
Log.v("error", e.toString());
}
} else if (type.equals("GET")) {
try {
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
response.getStatusLine().getStatusCode();
String result = EntityUtils.toString(response.getEntity());
return result;
} catch (Exception e) {
Log.v("error", e.toString());
}
}
return "";
}
// What to do after AsyncTask
protected void onPostExecute(String feed) {
JSONObject JSON = null;
try {
JSON = new JSONObject(feed);
} catch (JSONException e) {
e.printStackTrace();
}
callback.call(JSON);
}
}
然后使用POST或GET。服务器应该返回JSON,然后您可以根据需要解析它
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(2);
// Not manditory, can be used for things like token, etc.
nameValuePairList.add(new BasicNameValuePair("ID", "VALUE"));
new Request("POST", "URL", new Callback.JSONCallback() {
@Override
public void call(JSONObject JSON) {
try {
// Parse JSON here
} catch (JSONException e) {
Log.v("error", e.toString());
}
}
}).execute(nameValuePairList);
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(2);
// Not manditory, can be used for things like token, etc.
nameValuePairList.add(new BasicNameValuePair("ID", "VALUE"));
new Request("GET", "URL", new Callback.JSONCallback() {
@Override
public void call(JSONObject JSON) {
try {
// Parse JSON here
} catch (JSONException e) {
Log.v("error", e.toString());
}
}
}).execute(nameValuePairList);
List-nameValuePairList=new-ArrayList(2);
//不是下颌,可以用来做代币之类的东西。
nameValuePairList.add(新的BasicNameValuePair(“ID”,“VALUE”));
新请求(“POST”、“URL”、new Callback.JSONCallback(){
@凌驾
公共void调用(JSONObject JSON){
试一试{
//在这里解析JSON
}捕获(JSONException e){
Log.v(“错误”,例如toString());
}
}
}).执行(nameValuePairList);
列表名称ValuePairList=新的ArrayList(2);
//不是下颌,可以用来做代币之类的东西。
nameValuePairList.add(新的BasicNameValuePair(“ID”,“VALUE”));
新请求(“GET”,“URL”,new Callback.JSONCallback(){
@凌驾
公共void调用(JSONObject JSON){
试一试{
//在这里解析JSON
}捕获(JSONException e){
Log.v(“错误”,例如toString());
}
}
}).执行(nameValuePairList);
为什么不改为使用?
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(2);
// Not manditory, can be used for things like token, etc.
nameValuePairList.add(new BasicNameValuePair("ID", "VALUE"));
new Request("POST", "URL", new Callback.JSONCallback() {
@Override
public void call(JSONObject JSON) {
try {
// Parse JSON here
} catch (JSONException e) {
Log.v("error", e.toString());
}
}
}).execute(nameValuePairList);
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(2);
// Not manditory, can be used for things like token, etc.
nameValuePairList.add(new BasicNameValuePair("ID", "VALUE"));
new Request("GET", "URL", new Callback.JSONCallback() {
@Override
public void call(JSONObject JSON) {
try {
// Parse JSON here
} catch (JSONException e) {
Log.v("error", e.toString());
}
}
}).execute(nameValuePairList);