Android 如何使用HttpPost获取第二个响应

Android 如何使用HttpPost获取第二个响应,android,http-post,apache-httpclient-4.x,Android,Http Post,Apache Httpclient 4.x,我正在使用HttpPost获取线程中的web搜索结果。 主要问题是第一个httppost请求的响应是否需要等待web处理。 我不知道下一步如何才能得到完全的响应结果 请给我一些提示或想法。提前谢谢 ,, 下面是我的源代码 try { String _url = "https://www.airfrance.com.tw/cgi-bin/AF/TW/zh/local/process/standardbooking/DisplayFlightPageAction.d

我正在使用HttpPost获取线程中的web搜索结果。 主要问题是第一个httppost请求的响应是否需要等待web处理。 我不知道下一步如何才能得到完全的响应结果

请给我一些提示或想法。提前谢谢

,,

下面是我的源代码

    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();
        }
试试看{
字符串url=”https://www.airfrance.com.tw/cgi-bin/AF/TW/zh/local/process/standardbooking/DisplayFlightPageAction.do";
HttpPost HttpPost=新的HttpPost(_url);
DefaultHttpClient httpclient=新的DefaultHttpClient();
HttpResponse response=httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()==200){
字符串sessionID=response.getFirstHeader(“设置Cookie”).getValue();
cookiestore=httpclient.getCookieStore();
}                
}捕获(例外情况除外){
}
试一试{
DefaultHttpClient=新的DefaultHttpClient();
//职位
HttpPost=新的HttpPost(_url);
客户端:setCookieStore(cookiestore);
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“typeTrip”、“2”);
参数添加(新的BasicNameValuePair(“出发”、“TPE”);
参数添加(新的BasicNameValuePair(“出发”、“CDG”);
参数添加(新的BasicNameValuePair(“departureType”、“AIRP”);
参数添加(新的BasicNameValuePair(“departureType”、“AIRP”);
参数添加(新的BasicNameValuePair(“到达”、“CDG”);
参数添加(新的BasicNameValuePair(“到达”、“TPE”);
参数添加(新的BasicNameValuePair(“arrivalType”、“AIRP”);
参数添加(新的BasicNameValuePair(“arrivalType”、“AIRP”);
参数添加(新的BasicNameValuePair(“dayDate”、“28”);
参数添加(新的BasicNameValuePair(“dayDate”、“10”);
参数添加(新的BasicNameValuePair(“yearMonthDate”、“201906”);
参数添加(新的BasicNameValuePair(“yearMonthDate”、“201907”);
参数添加(新的BasicNameValuePair(“nbPassenger”,“1”);
参数添加(新的BasicNameValuePair(“paxTypoList”、“ADT”);
参数添加(新的BasicNameValuePair(“selectCabin”、“on”);
添加(新的BasicNameValuePair(“plusOptions”和“”);
参数添加(新的BasicNameValuePair(“1”);
参数add(新的BasicNameValuePair(“nbChildren”,“0”);
参数添加(新的BasicNameValuePair(“nbEnfants”,“0”);
参数添加(新的BasicNameValuePair(“nbBebes”,“0”);
参数添加(新的基本名称对(“CAB”、“Y”);
参数添加(新的BasicNameValuePair(“Subbabin”、“MCHER”);
参数添加(新的基本名称对(“拖运”、“左侧”);
参数添加(新的BasicNameValuePair(“familyTrip”、“NON”);
添加(新的BasicNameValuePair(“日历搜索”,“1”));
添加参数(新的BasicNameValuePair(“flyingBlueMember”、“false”);
添加参数(新的BasicNameValuePair(“partnerRequest”、“false”);
参数add(新的BasicNameValuePair(“corporateMode”、“false”);
参数添加(新的BasicNameValuePair(“isUM”、“false”);
参数添加(新的BasicNameValuePair(“optionalUM”、“false”);
参数添加(新的BasicNameValuePair(“mandatoryUM”、“true”);
参数添加(新的BasicNameValuePair(“标准强制”、“真实”);
添加(新的BasicNameValuePair(“subscriberHOPContext”、“false”);
参数添加(新的BasicNameValuePair(“hopCardOption”、“无应答”);
添加(新的BasicNameValuePair(“hopCardType”和“”);
参数添加(新的BasicNameValuePair(“notFromFlight”、“true”);
UrlEncodedFormEntity ent=新的UrlEncodedFormEntity(params,HTTP.UTF_8);
邮政实体(ent);
HttpResponse responsePOST=client.execute(post);
int code=responsePOST.getStatusLine().getStatusCode();
Log.e(mTAG,“web代码=”+代码);
if(responsePOST.getStatusLine().getStatusCode()=200){
HttpEntity-PresentyPost=responsePOST.getEntity();
POSTresult=EntityUtils.toString(当前Post);
}
mOTAFileThread=null;
}捕获(例外e){
e、 printStackTrace();
}

您可以将自定义的
响应句柄
传递到
执行
方法中,以便将第二个请求链接到其中

    // 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);
//创建自定义响应处理程序
ResponseHandler ResponseHandler=新ResponseHandler(){
@凌驾
公共字符串句柄响应(
最终HttpResponse响应)抛出ClientProtocolException,IOException{
int status=response.getStatusLine().getStatusCode();
如果(状态>=200&&status<300){
//第一个请求的响应在这里可用
HttpEntity=response.getEntity();
返回实体!=null?EntityUtils.toString(实体):null;
}否则{
抛出新的ClientProtocolException(“意外响应状态:+状态”);
}
}
};
字符串responseBody=httpclient.execute(httpget,responseHandler);
System.out.println(“--------------------------------------------------------”;
系统输出打印LN(响应库);
参考:

除此之外,
okhttp/reformation
在android开发方面非常流行和强大。请随意查看


您是否要链接请求?你需要第一个响应来启动第二个响应吗?@MichaelLam,事实上,我不知道如何在一个请求中链接第一个和第二个请求。我尝试使用一个线程获得第一个响应,然后使用第二个线程