如何处理android帖子请求

时间:2014-03-24 17:05:37

标签: android json post

我必须以下面的json格式发送请求

  {
    "expert_request":
    {
     "topic":"gggg",
     "expert":"10"
    } 
  }

我使用了以下代码,但它不起作用。我已经设置了主题和专家字段。但是如何设置expert_request字段?

protected JSONObject doInBackground(String... params) {
            // TODO Auto-generated method stub
            String returnValue = null;

            String topic = params[0]; // topic
            String doctor_id = params[1]; // expert

            // Building Parameters
            List<NameValuePair> paramList = new ArrayList<NameValuePair>();
            paramList.add(new BasicNameValuePair("topic", topic));
            paramList.add(new BasicNameValuePair("expert", doctor_id));

            JSONObject json = jParser.makeHttpRequest(URL_REQUEST, "POST", paramList);

            return json;
        }


public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        Log.e("URL", url);

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();

                if(params.size() > 0){
                    String paramString = URLEncodedUtils.format(params, "utf-8");
                    url += "?" + paramString;
                }



                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }

1 个答案:

答案 0 :(得分:0)

为什么不这样做:

public JSONObject makeJSON(String topic, String expert) {
    JSONObject parent = new JSONObject();
    JSONObject child = new JSONObject();
    child.put("topic", topic);
    child.put("expert", expert);
    parent.put("expert_request", child);
    return parent;
}

这应该会为您提供格式正确的JSON对象。要发送请求,您可能需要查看Google的Volley库。它比ASyncTask好多了。

相关问题