gpt4 book ai didi

java - Json 遇到意外字符 'q'

转载 作者:太空宇宙 更新时间:2023-11-03 15:00:18 24 4
gpt4 key购买 nike

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

问题是这一行:

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

错误是:

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

}

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

如果您需要其余功能,请告诉我。

最佳答案

经过一些帮助,我找到了解决方案:

问题是这一行:

httpPost.setEntity(new UrlEncodedFormEntity(params));

而不是将参数创建为 List<NameValuePair>并将其传递给 setEntity作为UrlEncodedFormEntity ,您应该将参数创建为 JSONObject并将其传递给 setEntity作为StringEntity .有关示例,请参阅此 Stack Overflow 答案: Json not working with HttpPost probably around setEntity

关于java - Json 遇到意外字符 'q',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23246215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com