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



我正在使用HttpPost在线程中获取网络搜索结果。 主要问题是来自第一个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();
}

可以将自定义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);

参考:

响应处理程序的官方示例

执行功能的文档

除此之外,okhttp / Retrofit在安卓开发方面非常受欢迎和强大的HttpClient。请随时查看。

OkHttp

改造

相关内容

  • 没有找到相关文章

最新更新