2014-03-24 165 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好很多。