簡體   English   中英

Http發布得到響應Android

[英]Http post get response Android

我想從外部網頁檢索一些數據。 當我在其上導航並單擊以顯示此數據時,我從開發人員控制台(在“網絡”下)看到正在進行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發布請求的哪些數據(可以從開發人員控制台中看到)。 這是我看到的http發布請求: 在此處輸入圖片說明

我不確定是否需要添加所有標頭以及如何添加有效負載。 甚至我都不知道我是否能做到,所以我將非常感激我能指導我一些

謝謝!!

您可以使用類似的函數,然后轉換JSONObject中返回的String或其他內容。 如果需要,可以將其調整為適合您的代碼,但我認為它“幾乎”是通用解決方案。

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

看看並評估它是否值得。 它允許您管理“無連接”,在呼叫之前或之后顯示/隱藏對話框(或任何其他內容)以及更多功能。

希望能對您有所幫助。 ;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM