我正在尝试向Https网站发送HTTPost请求,以获得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 Parser函数):
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 :(得分:0)
经过一些帮助,我找到了解决方案:
问题在于这一行:
httpPost.setEntity(new UrlEncodedFormEntity(params));
您应该将params创建为List<NameValuePair>
,并将其传递给setEntity
,而不是将UrlEncodedFormEntity
创建并将其作为JSONObject
传递给setEntity
。 }作为StringEntity
。有关示例,请参阅此Stack Overflow答案:
Json not working with HttpPost probably around setEntity