2014-04-23 41 views
0

我試圖發送一個HTTPost請求到一個Https網站以得到json格式的響應。Json遇到意想不到的字符'q'

問題是這樣的線::

Log.e( 「結果」, 「RESPONSE:AA」 +結果);

的錯誤是:

RESPONSE:AA 「遇到意外的字符 'Q'。」關於請求

詳細說明:

REQUEST 
POST https://localhost:17778/SolarWinds/InformationService/v3/Json/Query HTTP/1.1 
Authorization: Basic YWRtaW46 
User-Agent: curl/7.20.0 (i386-pc-win32) libcurl/7.20.0 OpenSSL/0.9.8l zlib/1.2.3 
Host: localhost:17778 
Accept: */* 
Content-Type: application/json 
Content-Length: 130 
{"query":"SELECT PollerID FROM Orion.Pollers"} 

詳細關於響應:

RESPONSE 
HTTP/1.1 200 OK 
Content-Length: 99 
Content-Type: application/json 
Server: Microsoft-HTTPAPI/2.0 
Date: Tue, 07 Aug 2012 17:36:27 GMT 
{"results":[{"PollerID":1},{"PollerID":2},{"PollerID":3},{"PollerID":4},{"PollerID":5},{"PollerID":6},{"PollerID":7},{"PollerID":8}]} 

這是我的代碼:

JsonReaderPost(的JSON分析器功能):

public class JsonReaderPost { 

    public JsonReaderPost() { 

     } 

    public void Reader() throws IOException, JSONException, KeyStoreException, NoSuchAlgorithmException, CertificateException, KeyManagementException, UnrecoverableKeyException, URISyntaxException { 



     String result = null; 
     String ints = ""; 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("query","SELECT PollerID FROM Orion.Pollers")); 
     //HttpClient client =new MyHttpClient(mContext); 

     HttpPost httpPost = new HttpPost("https://192.168.56.101:17778/SolarWinds/InformationService/v3/Json/Query"); 
     httpPost.addHeader("content-type", "application/json"); 
     httpPost.addHeader("Authorization", "Basic YWRtaW46"); 
     httpPost.setEntity(new UrlEncodedFormEntity(params)); 


     HttpClient client = new DefaultHttpClient(); 
     client=MyHttpClient.sslClient(client); 
     HttpResponse response =client.execute(httpPost); 

     HttpEntity entity = response.getEntity(); 

     if (entity != null) { 

      // A Simple JSON Response Read 
      InputStream instream = entity.getContent(); 
      result = convertStreamToString(instream); 
      // now you have the string representation of the HTML request 
      // System.out.println("RESPONSE: " + result); 

        //Here i Get THe error in this line : 
      Log.e("Result", "RESPONSE:aa " + result); 
      instream.close(); 
     } 

     // Converting the String result into JSONObject jsonObj and then into 
     // JSONArray to get data 
     JSONObject jsonObj = new JSONObject(result); 
     JSONArray results = jsonObj.getJSONArray("results"); 
     for (int i = 0; i < results.length(); i++) { 
      JSONObject r = results.getJSONObject(i); 
      ints = r.getString("PollerID"); 
      Log.e("Final Result", "RESPONSE: zz" + ints); 
     } 

    } 


    public static String convertStreamToString(InputStream is) { 

     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line = null; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 

} 

我很困惑,我找不到任何解決方案。

如果你需要其餘的功能告訴我。

+0

任何想法傢伙? – Kinn

回答

0

一些幫助後,我找到了解決辦法:

的問題是這一行:

httpPost.setEntity(新UrlEncodedFormEntity(PARAMS));

不是將params創建爲List並將其作爲UrlEncodedFormEntity傳遞給setEntity,而應該創建params作爲JSONObject並將其作爲StringEntity傳遞給setEntity。舉個例子來看這個Stack Overflow答案: Json not working with HttpPost probably around setEntity

相關問題