2014-04-23 2 views
0

HTTP 요청을 Https 웹 사이트에 보내 json 형식으로 응답을 보내려고합니다.Json 예기치 않은 문자 'q'가 발생했습니다.

문제는이 라인 ::이다

Log.e ("결과", "응답 : AA"+ 결과);

오류 :

응답 : 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(); 
    } 

} 

혼란스럽고 어떤 해결책도 찾을 수 없습니다.

필요한 경우 나머지 기능은 나에게 알려줍니다. ;

httpPost.setEntity (새 UrlEncodedFormEntity (PARAMS))

문제점이 라인이다

+0

아이디어가 있으십니까? – Kinn

답변

0

일부 도움 후,이 용액을 발견

params를 List로 만들고 setEntity에 UrlEncodedFormEntity로 전달하는 대신 params를 JSONObject로 만들고 setEntity에 StringEntity로 전달해야합니다. 예를 들어,이 스택 오버플로 대답을 참조하십시오. Json not working with HttpPost probably around setEntity

관련 문제