service文件模板:
String url="http://59.78.93.208:9097/Order?id="+id+"&value="+value;
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 3000 );
//HttpPost request;
HttpPost request;
try {
request=new HttpPost(new URI(url));
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (entity != null) {
String out = EntityUtils.toString(entity);
JSONArray jsonArray=new JSONArray(out);
return jsonArray;
}
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
实例
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class OrderService{ public static JSONArray getOrderList(int value,int id){ String url="http://59.78.93.208:9097/Order?id="+id+"&value="+value; HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000); client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 3000 ); //HttpPost request; HttpPost request; try { // request = new HttpPost(new URI(url)); request=new HttpPost(new URI(url)); // List<NameValuePair> params=new ArrayList<>(); // params.add(new BasicNameValuePair("value",String.valueOf(value))); // params.add(new BasicNameValuePair("id",String.valueOf(lastindex))); // request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { String out = EntityUtils.toString(entity); JSONArray jsonArray=new JSONArray(out); return jsonArray; } } } catch (Exception e) { e.printStackTrace(); return null; } return null; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class AdviceService { public int sendAdvise(String sex,String grade,String advise,String w,String h,String android_version,String mobile_model,String density){ String url="http://59.78.93.208:9092/AdviseInsert"; HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000); client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 3000); HttpPost request; try { request = new HttpPost(url); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair ("sex", sex)); params.add(new BasicNameValuePair ("grade", grade)); params.add(new BasicNameValuePair ("text", advise)); params.add(new BasicNameValuePair ("w", w)); params.add(new BasicNameValuePair ("h", h)); params.add(new BasicNameValuePair ("android_version", android_version)); params.add(new BasicNameValuePair ("mobile_model", mobile_model)); params.add(new BasicNameValuePair ("density", density)); request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { String out = EntityUtils.toString(entity); int result=Integer.parseInt(out); return result; } } } catch (Exception e) { e.printStackTrace(); } //return null; return -2; } }