http使用JSON对象在android上使用异步获取

时间:2014-01-23 22:30:13

标签: android json http

嘿我试图通过我的异步任务调用带有JSON对象的http get但是我很难弄清楚如何获得响应,更重要的是如何发送带有请求的get ......

这是我目前的代码:

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());
                        }
                }
        }

这行需要更改,因为get没有这样的方法,即使我将它用于我的http帖子......

httpget .setEntity(se); 

因此,如果有人可以告诉我我的错误是什么,以及如何处理回复......这将是很棒的。

3 个答案:

答案 0 :(得分:0)

试试这个

HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity)

这应该给你字符串

的结果

答案 1 :(得分:0)

我正在使用这些

public static String asString( HttpResponse response ) throws IOException {
  BufferedReader reader = new BufferedReader( new InputStreamReader( response.getEntity().getContent() ) );
  try{
    StringBuilder sb = new StringBuilder();
    String line = null;
    while( null != ( line = reader.readLine() ) ) sb.append( line );
    return sb.toString().trim();
  }finally{
    reader.close();
  }
}

public static JSONObject asJSON( HttpResponse response ) throws IOException, JSONException {
  return new JSONObject( asString( response ) );
}

答案 2 :(得分:0)

我最终这样做似乎工作正常:

private class search_send extends AsyncTask<String, Void, String> {
        // private ProgressDialog dialog
        ProgressDialog progress_search = new ProgressDialog(search.this);

        @Override
        protected void onPreExecute() {
            progress_search.setMessage("Uploading...");
            progress_search.show();
        }

        protected String doInBackground(String... search_data) {
            String url = "http://10.0.2.2:3000/search?data="
                    + search_data[0];
            Log.e(" more ", "what " + search_data[0]);
            String response = "";
            DefaultHttpClient client = new DefaultHttpClient();

            try {
                Log.e("did we hit", "check " + url);
                HttpGet httpGet = new HttpGet(url);
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                response = client.execute(httpGet, responseHandler);
                Log.e(" more ", "what " + response);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return response;
        }

        protected void onPostExecute(String result) {
            progress_search.dismiss();
            Toast.makeText(search.this, "Search sending..." + result,
                    Toast.LENGTH_LONG).show();
        }

    }