我想發送JSON
與http post。我serializationed JSON
與GSON
,這是我的源代碼:Android發送JSON與http get method.result爲空
public void SendHttpPostRequest(final String url, final String json) {
class HttpGetAsyncTask2 extends AsyncTask<String, Void, String> {
private Dialog pDialog;
private String response;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(CredoGenerireba.this);
pDialog.setCancelable(false);
pDialog.show();
pDialog.setContentView(R.layout.custom_progressdialog);
}
@Override
protected String doInBackground(String... params) {
try {
HttpPost post = new HttpPost(url);
HttpResponse resp;
post.setEntity(new StringEntity("{ p: " + json + " }",HTTP.UTF_8));
// Add headers
post.addHeader("Accept", "application/json");
post.addHeader("Content-type", "application/json");
// Convert Response Entity To JsonString
HttpClient httpclient = new DefaultHttpClient();
resp = httpclient.execute(post);
HttpEntity httpEntity = resp.getEntity();
response = EntityUtils.toString(httpEntity);
System.out.println(response +" response");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pDialog != null)
{
pDialog.dismiss();
}
System.out.println(result + "Result issss");
}
}
HttpGetAsyncTask2 httpGetAsyncTask2 = new HttpGetAsyncTask2();
httpGetAsyncTask2.execute();
}
我打電話像這樣我的AsyncTask功能:
final Gson gson = new Gson();
myjson = gson.toJson(mainjson);
handler.postDelayed(new Runnable() {
@Override
public void run() {
SendHttpPostRequest(
"myurl",
gson.toJson(mainjson));
}
}, 5000);
我登錄myjson串的我修改正確的結果(如我所接收)我有問題在http post方法。當我嘗試在onPostExecute
中運行我的程序時,結果爲空。 什麼是錯誤我不知道有誰知道解決方案,請幫助我謝謝。
null結果issss @Gauraw Yadav – baggio 2015-04-01 10:20:22
你的json文本是在字符串響應;所以在onPostExecute中使用它。添加一個println()來查看它。 – greenapps 2015-04-01 10:41:54
是的,我知道,但在onPostExecute resulit爲空,服務器nothink返回@greenapps – baggio 2015-04-01 10:46:30