0

我有一个可以联系的 Web 服务,但我必须向 Web 服务添加一个 Skip 和 Take 整数,以限制我返回的结果数量,因为会有数百个地址。所以下面发布的是我联系webservice的android方法,但是我如何为我的android应用程序更改下面的活动代码,这样我就可以限制说十个结果,第一次跳过0,然后连续跳过十个。

我的安卓活动:

public List<FuelStops> getFuelStops() throws URISyntaxException, ClientProtocolException, IOException, ParserConfigurationException, SAXException{
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    BufferedReader in = null;
    String page;
    fuelStopList = new ArrayList<FuelStops>();
    try {

        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI("http://google.com/Service1.asmx/GetFuelStops"));

        HttpResponse response = client.execute(request);
        in = new BufferedReader
        (new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);

        }
        in.close();
        page = sb.toString();
        FuelStops fuelStop=new FuelStops();
        StringBuilder addressStrBlder = new StringBuilder();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(new StringReader(page)));
        // normalize the document
        doc.getDocumentElement().normalize();
        // get the root node
        NodeList nodeList = doc.getElementsByTagName("FuelStop");
        Node node=nodeList.item(0);
        // the  node has three child nodes
        for (int n = 0; n < nodeList.getLength(); n++) {


            for (int i = 0; i < node.getChildNodes().getLength(); i++) {
                Node temp=node.getChildNodes().item(i);
                if(temp.getNodeName().equalsIgnoreCase("Physical_Address_Street")){
                    addressStrBlder.append(temp.getTextContent());
                }
                else if(temp.getNodeName().equalsIgnoreCase("Physical_Address_Local")){
                    addressStrBlder.append(", " + temp.getTextContent());
                }
                else if(temp.getNodeName().equalsIgnoreCase("Physical_Address_State")){
                    addressStrBlder.append(", " + temp.getTextContent());
                }
                else if(temp.getNodeName().equalsIgnoreCase("Physical_Address_Zip")){
                    addressStrBlder.append(", " + temp.getTextContent());
                    fuelStop.setAddress(addressStrBlder.toString());
                }
                else if(temp.getNodeName().equalsIgnoreCase("Phone_Number")){
                    fuelStop.setPhoneNum(temp.getTextContent());
                    fuelStopList.add(fuelStop);
                }

            }
            //Log.e("Fuel Stop", fuelStop.toString());
        }

        //System.out.println(page);
        } finally {
        if (in != null) {
            try {
                in.close();
                } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return fuelStopList;
}

我的网络服务 在此处输入图像描述

4

1 回答 1

1

如果您的 Web 服务采用标准 HTTP 请求参数,您只需将它们添加到 URL:

request.setURI(new URI("http://google.com/Service1.asmx/GetFuelStops?skip=10&take=10"));

这适用于 GET 或 POST,您需要使用 HttpPost 类(而不是 HttpGet)并执行以下操作:

HttpPost request = new HttpPost("http://google.com/Service1.asmx/GetFuelStops");

// Add your data
List<NameValuePair> nvps = new ArrayList<NameValuePair>(2);
nvps.add(new BasicNameValuePair("skip", "10"));
nvps.add(new BasicNameValuePair("take", "10"));
request.setEntity(new UrlEncodedFormEntity(nvps));
于 2013-04-23T19:13:28.253 回答