Android 循环HttpPost请求
我需要使用几个不同的post请求访问网页中的数据。目前我使用:Android 循环HttpPost请求,android,http,Android,Http,我需要使用几个不同的post请求访问网页中的数据。目前我使用: HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("https://myurl"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNa
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://myurl");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("action", "search"));
nameValuePairs.add(new BasicNameValuePair("ndc", ndc));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(“https://myurl");
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“操作”、“搜索”));
添加(新的BasicNameValuePair(“ndc”,ndc));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
我需要使用变量ndc的不同值发送此请求。将这条线循环是个好主意吗?如果是,如何重用HttpClient和HttpPost变量?如果URL需要保持不变,那么您应该只更改需要发送的值
for (int i=0; i<ndcArray.length;i++)
{
if(i==theNumberWhenURLhasToBeChanged) //adjust this condition based on your knowledge when the url has to be changed, lets say if i > theNumberWhenURLhasToBeChanged, then change the url...
{
httppost = new HttpPost(URLs[theNumberWhenURLhasToBeChanged]);
}
nameValuePairs.add(new BasicNameValuePair("ndc", ndcArray[i]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}
for(int i=0;i)更改url时的编号,然后更改url。。。
{
httppost=新的httppost(URL[更改编号]);
}
添加(新的基本名称对(“ndc”,ndcArray[i]);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
}
请注意:响应将每次更改,因此请记住,您应该将响应保存在某个位置。ndcArray[]可以替换为您想要的任何结构。URL将在另一个URL最初加载一次后保持任意(>1)次不变。