我想从外部网页检索一些数据。当我在其上导航并单击显示此数据时,我从开发人员控制台(在“网络”下)看到正在进行 http post 调用。如果我打开它,我可以看到我想要从 Android 应用程序检索的数据,并且我想要获得该字符串响应。
但我不知道如何“构建”http post 请求。这是我的代码:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://portus.puertos.es/Portus_RT/portusgwt/rpc");
httppost.addHeader("Connection", "keep-alive");
httppost.addHeader("Content-Length", "172");
httppost.addHeader("X-GWT-Module-Base", "http://portus.puertos.es/Portus_RT/portusgwt/");
httppost.addHeader("X-GWT-Permutation", "3DEDE3A69CBBE62D4C3F58BF7278538F");
httppost.addHeader("Origin", "http://portus.puertos.es");
httppost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36");
httppost.addHeader("Accept", "*/*");
httppost.addHeader("Referer", "http://portus.puertos.es/Portus_RT/?locale=es");
httppost.addHeader("Accept-Encoding", "gzip,deflate");
httppost.addHeader("Accept-Language", "en-US,en;q=0.8,es;q=0.6,ca;q=0.4");
httppost.addHeader("AlexaToolbar-ALX_NS_PH", "AlexaToolbar/alxg-3.3");
httppost.addHeader("Content-Type", "text/x-gwt-rpc; charset=UTF-8");
httppost.addHeader("Host", "portus.puertos.es");
//I think I need to add the payload here
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("", ""));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.d("TAG", EntityUtils.toString(response.getEntity()));
} catch (Exception e) {
e.printStackTrace();
}
我真的不了解http协议(protocol),所以我不知道需要添加http post请求的哪些数据(我可以从开发者控制台看到)。这是我看到的 http post 请求:
我不确定是否需要添加所有 header 以及如何添加有效负载。 即使我不知道我是否能做到这一点,所以我将非常感激任何人都可以指导我一些
谢谢!!
最佳答案
您可以使用类似的函数,并将返回的字符串转换为 JSONObject 或其他形式。如果需要,请根据您的代码进行调整,但我认为它“几乎”是一个通用的解决方案。
private String callServer(List<BasicNameValuePair> nameValuePairs,String path) {
InputStream is = null;
StringBuilder sb = null;
String result = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(path);
//Your headers here
//I'm afraid there are too much headers. Try cleaning and choosing only the neccessary ones.
httppost.setHeader("Connection", "keep-alive");
httppost.setHeader("Content-Length", "172");
httppost.setHeader("X-GWT-Module-Base", "http://portus.puertos.es/Portus_RT/portusgwt/");
httppost.setHeader("X-GWT-Permutation", "3DEDE3A69CBBE62D4C3F58BF7278538F");
httppost.setHeader("Origin", "http://portus.puertos.es");
httppost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36");
httppost.setHeader("Accept", "*/*");
httppost.setHeader("Referer", "http://portus.puertos.es/Portus_RT/?locale=es");
httppost.setHeader("Accept-Encoding", "gzip,deflate");
httppost.setHeader("Accept-Language", "en-US,en;q=0.8,es;q=0.6,ca;q=0.4");
httppost.setHeader("AlexaToolbar-ALX_NS_PH", "AlexaToolbar/alxg-3.3");
httppost.setHeader("Content-Type", "text/x-gwt-rpc; charset=UTF-8");
httppost.setHeader("Host", "portus.puertos.es");
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e1) {
e1.printStackTrace();
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
return result;
}
即使很困难,我还是建议您使用此组件来实现 REST API 和 HTTP 连接:
https://github.com/matessoftwaresolutions/AndroidHttpRestService
看看并评估它是否对您有值(value)。它允许您管理“无连接”、在通话之前和之后显示/隐藏对话框(或其他)以及更多功能。
希望我能帮到你。 ;)
关于java - Http post 获取响应 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26342025/