Http帖子获得Android响应

时间:2014-10-13 14:09:18

标签: java android http post

我想从外部网页检索一些数据。当我在其上导航并点击以显示此数据时,我从开发者控制台(在"网络"下)看到正在进行http post调用。如果我打开它,我可以看到我想从我的Android应用程序检索的数据,我想得到该字符串响应。

但我不知道如何构建" http发布请求。这是我的代码:

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协议,所以我不知道我需要添加的http post请求的数据(我可以从开发者控制台看到)。这是我看到的http post请求: enter image description here

我不确定我是否需要添加所有标头以及如何添加有效负载。 即使我不知道我是否可以这样做,所以我将非常感激任何人都可以指导我一些

谢谢!

1 个答案:

答案 0 :(得分:1)

你可以使用这样的函数,并转换JSONObject中返回的String等等。如果您需要,请根据您的代码进行调整,但我认为它几乎是&#34;通用解决方案。

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;

    }

即使很难,我建议您将此组件用于其他API和HTTP连接:

https://github.com/matessoftwaresolutions/AndroidHttpRestService

看看并评估它是否值得。它允许您管理&#34;无连接&#34;,显示/隐藏呼叫前后的对话(或其他)以及更多功能。

我希望我会帮助你。 ;)