你好我想从http post请求获取json。我使用了这个网站:http://www.hurl.it/来测试我的请求并且它运行良好所以我想在我的代码中执行相同的请求。
我的代码中的某些东西不起作用:
@SuppressWarnings({ "rawtypes", "unchecked" })
public static String postHttpResponse() {
Log.d("post", "Going to make a post request");
StringBuilder response = new StringBuilder();
try {
Log.d("post", "Im inside :-)))))");
HttpPost post = new HttpPost();
URI uri = new URI("https://mywebsite.com/Home/GetHomeTemplates");
post.setURI(uri);
List params = new ArrayList();
params.add(new BasicNameValuePair("Skip","100"));
params.add(new BasicNameValuePair("Take","50"));
params.add(new BasicNameValuePair("Type","-1"));
params.add(new BasicNameValuePair("PluginType","-1"));
params.add(new BasicNameValuePair("Proportion","-1"));
params.add(new BasicNameValuePair("Sort","6"));
params.add(new BasicNameValuePair("SortDirection","1"));
params.add(new BasicNameValuePair("Categories","[]"));
params.add(new BasicNameValuePair("Controls","[]"));
params.add(new BasicNameValuePair("IsFree","true"));
params.add(new BasicNameValuePair("IsSystem","true"));
post.setHeader("Host","mywebsite.com");
post.setHeader("Accept","application/json, text/javascript, */*; q=0.01");
post.setHeader("Content-Type","application/json; charset=UTF-8, charset=utf-8");
post.setHeader("Referer","https://mywebsite.com/Templates");
post.setEntity(new UrlEncodedFormEntity(params));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(post);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
Log.d("post", "HTTP POST succeeded");
HttpEntity messageEntity = httpResponse.getEntity();
InputStream is = messageEntity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}
} else {
Log.d("post", "HTTP POST status code is not 200");
}
Log.d("post", "Done with HTTP posting");
Log.d("post", "Post" + response.toString());
} catch (Exception e) {
Log.d("Exception: ", "Post " + e.getMessage());
}
return response.toString();
带有后置过滤器的我的logcat:
09-25 19:49:05.290: D/post(26343): Going to make a post request
09-25 19:49:05.290: D/post(26343): post Im inside :-)))))
09-25 19:49:05.290: D/Exception:(26343): Post null
因此,我们认为try / catch中的代码并没有运行。
修改
没有记录"我在里面: - ))))"因为我正在过滤字符串" post"在logcat搜索。我还调试并检查了我的参数是否未传递给我的请求。
我编辑了我的参数,因为@Almer Nakano建议,但它仍然没有看到参数。
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Skip","100"));
params.add(new BasicNameValuePair("Take","50"));
params.add(new BasicNameValuePair("Type","-1"));
params.add(new BasicNameValuePair("PluginType","-1"));
params.add(new BasicNameValuePair("Proportion","-1"));
params.add(new BasicNameValuePair("Sort","6"));
params.add(new BasicNameValuePair("SortDirection","1"));
params.add(new BasicNameValuePair("Categories","[]"));
params.add(new BasicNameValuePair("Controls","[]"));
params.add(new BasicNameValuePair("IsFree","true"));
params.add(new BasicNameValuePair("IsSystem","true"));
答案 0 :(得分:0)
我不太确定日志管理器是否保证打印日志消息;有缓冲区和复杂的事情,也许它会在必要时过滤掉一些消息?
documentation中的以下句子让我感到困惑:“这意味着如果您的日志消息被过滤掉,那么您可能会做大量的工作并产生大量开销。”
尝试使用Log.d以外的其他内容来检查代码是否运行,或者是否可以提高日志级别?
答案 1 :(得分:0)
尝试切换此原始列表参数
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
您还可以使用params创建一个json String,并将其传递给:
nameValuePairs.add(new BasicNameValuePair("data", jsonString));