我想使用wp-api插件为我的wordpress网站构建一个android应用程序。我如何发送HttpRequest(GET)并在Json中接收响应?如何发送HttpRequest并在android中获取Json响应?
回答
尝试下面的代码从URL中获得JSON
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget= new HttpGet(URL);
HttpResponse response = httpclient.execute(httpget);
if(response.getStatusLine().getStatusCode()==200){
String server_response = EntityUtils.toString(response.getEntity());
Log.i("Server response", server_response);
} else {
Log.i("Server response", "Failed to get server response");
}
在哪里可以找到HttpClient?我必须包含哪些软件包? – Tarion
@Tarion只需在'android'中的应用级别build.gradle文件中添加'useLibrary'org.apache.http.legacy''在defaultConfig之上。 –
try {
String line, newjson = "";
URL urls = new URL(url);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(urls.openStream(), "UTF-8"))) {
while ((line = reader.readLine()) != null) {
newjson += line;
// System.out.println(line);
}
// System.out.println(newjson);
String json = newjson.toString();
JSONObject jObj = new JSONObject(json);
}
} catch (Exception e) {
e.printStackTrace();
}
使用此函数从URL获取JSON。
public static JSONObject getJSONObjectFromURL(String urlString) throws IOException, JSONException {
HttpURLConnection urlConnection = null;
URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.setDoOutput(true);
urlConnection.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
String jsonString = sb.toString();
System.out.println("JSON: " + jsonString);
return new JSONObject(jsonString);
}
不要忘了在你的清单
<uses-permission android:name="android.permission.INTERNET" />
添加Internet权限,然后使用它是这样的:
try{
JSONObject jsonObject = getJSONObjectFromURL(urlString);
//
// Parse your json here
//
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
很好的解决方案,不需要导入apache http客户端! – Jlange
这里至少有两处重大错误。 1.'urlConnection.setDoOutput(true);'将请求更改为'POST'方法。 2.它有效地执行了两个请求,'new InputStreamReader(url.openStream())'再一次打开'url',不考虑'urlConnection'及其所有属性。''sb.append(line +“\ n”)'构建一个多余的字符串 –
- 1. 如何获取发送API请求并获取Json响应c#
- 2. 如何在iPhone中发送请求并获取文件响应?
- 3. 如何使用httprequest获取完整的json响应
- 4. 如何将xml发送到IP并在android中获取xml响应?
- 5. 如何发送请求并从android中获得响应soap
- 6. 如何发送请求并通过JAVA(android)接收响应(JSON)?
- 7. 通过AJAX发送数据并获取JSON响应
- 8. 如何发送HttpRequest,并返回Json数据并显示它
- 9. 如何获取android应用发送的http响应?
- 10. 如何在ajax中获取json响应?
- 11. 获取JSON响应Android
- 12. 如何在php中发送json响应时删除响应头?
- 13. 如何通过XML在服务器上发送数据,并获取响应android
- 14. 发送json响应
- 15. 如何在python中发送POST请求并获得正确的json响应?
- 16. 如何制作HTTP Post请求并获取响应为JSON Android
- 17. 在rails中发送响应在json中
- 18. 如何发送POST请求并获取文件响应?
- 19. 如何发送请求并从网站获取响应(http)
- 20. 如何发送请求并使用system.io.pipes获取响应
- 21. 如何发送http文件使用Android获取内容并捕获(显示)响应(json-data)
- 22. CakePHP3.4:如何发送json对象响应?
- 23. 如何在node.js中发送邮件通知以获取响应
- 24. 如何从JSON响应中获取值
- 25. android httprequest压缩响应
- 26. 在json中发送编码响应
- 27. 如何快速响应httpRequest?
- 28. PHP:如何从JSON获取变量(从Swift iOS应用发送)并用JSON响应
- 29. 如何从android中的Json响应数组中获取值?
- 30. res.json不发送JSON响应
http://developer.android.com/training/volley/index.html –
您可以使用此http://stackoverflow.com/a/8655039 – user1140237