2014-03-24 40 views
0

我要在下面的JSON格式如何处理的android post请求

{ 
    "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; 

    } 
+0

什么是“不工作”呢?描述你的具体问题。 – Wyzard

+0

我编辑了这个问题 – FlintOff

回答

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好很多。