如何使用HttpPost获得第二个响应

时间:2018-09-26 09:14:51

标签: android http-post apache-httpclient-4.x

我正在使用HttpPost来获取线程中的Web搜索结果。 主要问题是来自第一个httppost请求的响应是否需要等待网络处理。 而且我不知道下一步该怎么做才能获得完全的响应结果。

请给我一些提示或想法。预先感谢。

issue response piccorrect response pic

下面是我的源代码。

    try {
            String _url = "https://www.airfrance.com.tw/cgi-bin/AF/TW/zh/local/process/standardbooking/DisplayFlightPageAction.do";

            HttpPost httppost = new HttpPost(_url);
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);
            if (response.getStatusLine().getStatusCode() == 200) {
                String sessionID = response.getFirstHeader("Set-Cookie").getValue();
                cookiestore = httpclient.getCookieStore();
            }                
        } catch (Exception ex) {
        }

        try {
            DefaultHttpClient client = new DefaultHttpClient();

            // POST
            HttpPost post = new HttpPost(_url);

            client.setCookieStore(cookiestore);
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("typeTrip", "2"));
            params.add(new BasicNameValuePair("departure", "TPE"));
            params.add(new BasicNameValuePair("departure", "CDG"));
            params.add(new BasicNameValuePair("departureType", "AIRP"));
            params.add(new BasicNameValuePair("departureType", "AIRP"));
            params.add(new BasicNameValuePair("arrival", "CDG"));
            params.add(new BasicNameValuePair("arrival", "TPE"));
            params.add(new BasicNameValuePair("arrivalType", "AIRP"));
            params.add(new BasicNameValuePair("arrivalType", "AIRP"));
            params.add(new BasicNameValuePair("dayDate", "28"));
            params.add(new BasicNameValuePair("dayDate", "10"));
            params.add(new BasicNameValuePair("yearMonthDate", "201906"));
            params.add(new BasicNameValuePair("yearMonthDate", "201907"));
            params.add(new BasicNameValuePair("nbPassenger", "1"));
            params.add(new BasicNameValuePair("paxTypoList", "ADT"));
            params.add(new BasicNameValuePair("selectCabin", "on"));
            params.add(new BasicNameValuePair("plusOptions", ""));
            params.add(new BasicNameValuePair("nbAdults", "1"));
            params.add(new BasicNameValuePair("nbChildren", "0"));
            params.add(new BasicNameValuePair("nbEnfants", "0"));
            params.add(new BasicNameValuePair("nbBebes", "0"));
            params.add(new BasicNameValuePair("cabin", "Y"));
            params.add(new BasicNameValuePair("subCabin", "MCHER"));
            params.add(new BasicNameValuePair("haul", "LH"));
            params.add(new BasicNameValuePair("familyTrip", "NON"));
            params.add(new BasicNameValuePair("calendarSearch", "1"));
            params.add(new BasicNameValuePair("flyingBlueMember", "false"));
            params.add(new BasicNameValuePair("partnerRequest", "false"));
            params.add(new BasicNameValuePair("corporateMode", "false"));
            params.add(new BasicNameValuePair("isUM", "false"));
            params.add(new BasicNameValuePair("optionalUM", "false"));
            params.add(new BasicNameValuePair("mandatoryUM", "true"));
            params.add(new BasicNameValuePair("standardMandatory", "true"));
            params.add(new BasicNameValuePair("subscriberHOPContext", "false"));
            params.add(new BasicNameValuePair("hopCardOption", "NO_ANSWER"));
            params.add(new BasicNameValuePair("hopCardType", ""));
            params.add(new BasicNameValuePair("notFromFlight", "true"));


            UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
            post.setEntity(ent);
            HttpResponse responsePOST = client.execute(post);

            int code = responsePOST.getStatusLine().getStatusCode();
            Log.e(mTAG, "web code = " + code);
            if (responsePOST.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntityPOST = responsePOST.getEntity();
                POSTresult = EntityUtils.toString(resEntityPOST);

            }
            mOTAFileThread = null;
        } catch (Exception e) {
            e.printStackTrace();
        }

1 个答案:

答案 0 :(得分:1)

您可以将自定义ResponseHandler传递到execute方法中,以便可以在其中链接第二个请求。

    // Create a custom response handler
    ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

        @Override
        public String handleResponse(
                final HttpResponse response) throws ClientProtocolException, IOException {
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                // The response of first request is available here
                HttpEntity entity = response.getEntity();
                return entity != null ? EntityUtils.toString(entity) : null;
            } else {
                throw new ClientProtocolException("Unexpected response status: " + status);
            }
        }

    };
    String responseBody = httpclient.execute(httpget, responseHandler);
    System.out.println("----------------------------------------");
    System.out.println(responseBody);

参考:

Official Example of ResponseHandler

Doc of Execute function

除此之外,okhttp / Retrofit在Android开发方面非常受欢迎且功能强大HttpClient。请随时查看。

OkHttp

Retrofit