[英]Can't get an http JSON response in the post http request
I'm try to receive http response in the folowing code: 我尝试通过以下代码接收http响应:
public void httpRes (){
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://validate.jsontest.com/";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user", "kris"));
params.add(new BasicNameValuePair("pass", "xyz"));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}}
Got an error - 遇到错误-
LogCat log: LogCat日志:
09-12 21:48:58.099: INFO/RESPONSE(28485): {
"error": "No JSON to validate. Please post JSON to validate via the json parameter.",
"validate": false
}
I've tried to receive response from GET requsets - everything was ok, but when i've tried POST request all variety of POST code - I can't get an answer! 我试图从GET requsets接收响应-一切正常,但是当我尝试POST请求所有各种POST代码时-我无法得到答案! What is the problem?
问题是什么? need help.
需要帮忙。
I've also tried: 我也尝试过:
Map<String, String> comment = new HashMap<String, String>();
comment.put("password", "password");
comment.put("avatar", "httpssssssss");
String json = new GsonBuilder().create().toJson(comment, Map.class);
HttpResponse response = makeRequest("http://validate.jsontest.com/", json);
//Log.w(TAG, EntityUtils.toString(response));
try {
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
String sss= convertStreamToString(is);
Log.w("SSSSSSSSSSSSSSSSSS", sss);
} catch (Exception ex) {
Log.w("Exception exxx", ex);
}
public static HttpResponse makeRequest(String uri, String json) {
try {
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(new StringEntity(json));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
return new DefaultHttpClient().execute(httpPost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private 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();
}
So, what is the problem? 那么,有什么问题呢?
除非Content-Type
标头设置为application/x-www-form-urlencoded
否则http://validate.jsontest.com拒绝POST请求。
Via GET your line of code 通过获取您的代码行
HttpPost httpPost = new HttpPost(uri);
should be 应该
HttpPost httpPost = new HttpPost(uri+"?json="+json);
and I don't think you need the three lines below that. 而且我认为您不需要下面的三行。
Via POST you need to pass the json parameter like so. 通过POST,您需要像这样传递json参数。
params.add(new BasicNameValuePair("json", json));
assuming json is a variable containing your json data. 假设json是包含您的json数据的变量。
You can test the GET method in a browser like so http://validate.jsontest.com/?json= {validate: true} 您可以在这样的浏览器中测试GET方法,例如http://validate.jsontest.com/?json= {validate:true}
You need to post a valid json to the url http://validate.jsontest.com/ to validate it. 您需要将有效的json发布到url http://validate.jsontest.com/进行验证。 What you're doing is basically sending a post request to the url, which is not in json format.
您要做的基本上是向url发送发布请求,该请求不是json格式。 You'll have to encode it to json and then send it to the validating url.
您必须将其编码为json,然后将其发送到验证网址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.