Android与Servlet之间的通信

通信分为两种形式
一种是get 一种是post

1.android get 方式通过url传参

String line = "";
String temp = "";
String urls = "http://10.31.5.22:8080/temp2/helloWorld";
URL url = new URL(urls);
BufferedReader br = new BufferedReader(new InputStreamReader(
                                    url.openStream()));

while ((temp = br.readLine()) != null) {
    line = line + temp;
}
lineRead = line;

最后lineRead读出来的是servlet里doGet中print出来的东西

2.post方法

HttpPost httpPost = new HttpPost(urls);
List<NameValuePair>params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("action", "download"));
params.add(new BasicNameValuePair("package", "89dcb664-50a7-4bf2-aeed-49c08af6a58a"));
params.add(new BasicNameValuePair("uuid", "test_01"));
httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));

HttpResponse httpResponse = null;                           
httpResponse = new DefaultHttpClient().execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode() == 200){
    String result = EntityUtils.toString(httpResponse.getEntity());
     System.out.println("result"+result);
}

在params中随便传几个参数
servlet只需get一下就得到了
得到的result是servlet中doPost方法中print的东西

你可能感兴趣的:(Android,servlet,android,通信)