嘿,我正在嘗試調用一個JSON對象的http獲取我通過我的異步任務,但我很難找出如何得到響應,更重要的是如何將即可通過它發送請求...http與android的異步與JSON對象獲取
這裏是我當前的代碼:
private class search_send extends AsyncTask<JSONObject, Integer, Double> {
// private ProgressDialog dialog
ProgressDialog progress_search = new ProgressDialog(search.this);
@Override
protected void onPreExecute() {
progress_search.setMessage("Uploading...");
progress_search.show();
}
protected Double doInBackground(JSONObject... params) {
// TODO Auto-generated method stub
get_search(params[0]);
return null;
}
protected void onPostExecute(Double result) {
progress_search.dismiss();
Toast.makeText(search.this, "Search sending...", Toast.LENGTH_LONG)
.show();
}
public void get_search(JSONObject search_data) {
// Create a new HttpClient and Post Header
HttpParams httpParams = new BasicHttpParams();
HttpClient httpclient = new DefaultHttpClient(httpParams);
HttpGet httpget = new HttpGet("http://10.0.2.2:3000/search");
try {
// add data
StringEntity se = new StringEntity(search_data.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
httpget .setEntity(se);
// execute http request
HttpResponse response = httpclient.execute(httpget);
} catch (ClientProtocolException e) {
Log.e("sds", e.getMessage());
} catch (IOException e) {
Log.e("sds 22", e.getMessage());
}
}
}
需要這條線作爲獲取具有像這樣沒有方法改變,即使我用它爲我的http post ...
httpget .setEntity(se);
因此,如果有人能告訴我我在做什麼錯了,以及如何處理迴應......這將是偉大的。
您忘記了@覆蓋嗎?你的'doInBackground'和'onPostExecute'方法? – Fred