【问题标题】:http get with async on android with a JSON Objecthttp get with async on android with a JSON Object
【发布时间】:2014-01-23 22:30:13
【问题描述】:

嘿,我正在尝试使用我通过异步任务的 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); 

因此,如果有人能告诉我我在获取时做错了什么以及如何处理回复......那就太好了。

【问题讨论】:

  • 您是否在 doInBackgroundonPostExecute 方法中忘记了 @Override

标签: android json http


【解决方案1】:

试试这个

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

这应该会给你字符串中的结果

【讨论】:

    【解决方案2】:

    我正在使用这些

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 让我知道它是否存在我没​​有意识到的问题或什么......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多