我有一个可以联系的 Web 服务,但我必须向该 Web 服务添加一个 Skip 和 Take 整数,以限制我返回的结果数量,因为会有数百个地址。下面发布的是我联系网络服务的 android 方法,但是我如何更改下面的 android 应用程序的 Activity 代码,以便我可以限制说十个结果并跳过 0 第一遍,然后连续十个。
我的 Android Activity :
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;
}
我的网络服务
最佳答案
如果您的 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));
关于java - 从 Android 应用程序将整数传递到 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16177253/