我正在尝试将HTTPost请求发送到Https网站以获得json格式的响应。

问题是此行:

Log.e("Result", "RESPONSE:aa " + result);

错误是:

RESPONSE:aa "Encountered unexpected character '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(new UrlEncodedFormEntity(params));

不要将参数创建为List<NameValuePair>并将其作为setEntity传递给UrlEncodedFormEntity,而应将参数创建为JSONObject并将其作为setEntity传递给StringEntity。有关示例,请参见此堆栈溢出答案:
Json not working with HttpPost probably around setEntity

07-24 14:20