从Android应用程序传入整数到web服务

时间:2013-04-23 19:05:41

标签: java android web-services

我有一个可以联系的Web服务,但我必须在Web服务中添加一个Skip和Take整数,以限制我收到的结果数量,因为会有数百个地址。所以下面发布的是我的android方法,它联系web服务,但我如何更改我的Android应用程序下面的活动代码,所以我可以限制十个结果,并跳过0第一次传递,然后连续十次。

我的Android活动:

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

我的网络服务 enter image description here

1 个答案:

答案 0 :(得分:1)

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

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

哪个适用于GET,或者你需要使用HttpPost类(而不是HttpGet)的POST,并执行以下操作:

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