2015-10-26 35 views
0

我打電話與如何呼應與HTTP方法

List<NameValuePair> params = new ArrayList<NameValuePair>(); 
JSONObject json = jParser.makeHttpRequest(urlServer, "GET", params); 

的JSONParser類JSONParser是這個 公共類JSONParser android系統中的服務器響應{ 靜態InputStream爲= NULL; static JSONObject jObj = null; static String json =「」;

// constructor 
    public JSONParser() { 

    } 

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

     // Making HTTP request 
     try { 

      if (method == "GET") { 

       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       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(); 
       Log.e("Pasa x aki","ssss"); 
      } 

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

    } 

    } 

我的錯誤是在「JSON解析器」,因爲JSON對象爲空。我想知道服務器中發生了什麼。我怎樣才能打印來自服務器的回聲?

+0

服務器的回聲是JSON字符串。 – greenapps

+0

題外話:AFAIK HttpClient已經被棄用,目前的Android版本。 – hgoebl

+0

'//返回JSON字符串'。不,那是json對象。 – greenapps

回答

1

試試這個

HttpEntity entity = response.getEntity(); 

     if (entity != null) { 
      String retSrc = EntityUtils.toString(entity); 
      Log.v("response server ", retSrc); 
     } 
+0

非常感謝brayan,你是對的! – manolodewiner

+0

歡迎你投我一票回答:) –

+0

我想這樣做,但我需要15個名聲,我沒有。對不起 – manolodewiner