【问题标题】:Loop HttpPost requests循环 HttpPost 请求
【发布时间】:2011-07-19 16:57:15
【问题描述】:

我需要使用多个不同的发布请求从网页访问数据。现在我使用:

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);

我需要使用变量 ndc 的不同值发送此请求。循环这条线是个好主意吗?如果可以,如何复用 HttpClient 和 HttpPost 变量?

【问题讨论】:

    标签: android http


    【解决方案1】:

    如果 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);
    }
    

    请注意:响应每次都会更改,因此请记住,您应该将响应保存在某处。并且 ndcArray[] 可以替换为任何你想要的结构。

    【讨论】:

    • URL 将在任意 (>1) 次内保持不变,在另一个 URL 最初加载一次之后。
    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多