首先又打开android网络权限在androidManifest.x ml中:

<uses-permission android:name="android.permission.INTERNEt"/>

 

//javaee工程访问地址
String url = "http://localhost:8080/TestAndroid";
//把要请求的值封装到namevalupair的集合中
NameValuePair nameValuePair1 = new BasicNameValuePair("name","zhangsna");

NameValuePair nameValuePair2 = new BasicNameValuePair("age","12");

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

 nameValuePairs.add(nameValuePair1);
 nameValuePairs.add(nameValuePair2);

try{
HttpEntity httpEntity = new UrlEncodedFormEntity(nameValuePairs);
//通过post请求,把android的值传到javaee工程内处理
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(httpEntity);//将需要提交的数据添加到post请求中
HttpClient httpClient = new DefaultHttpClient(); //执行post请求 HttpResonse httpResponse = httpClient.execute(httpPost); //httpentity即是请求实体有事返回实体 InputStream = httpEntity.getConteant; //读取客服端返回的值 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

//定义字符串循环出reader内容
  String result = "";
  String line = "";
  while((line= reader.readLine())!=null){
   result = result+line;
  }
打印服务器返回的内容
system.out.println(result);
}
catche(Exception e){
}




  

posted on 2012-05-26 08:35  断肠夕阳丿  阅读(338)  评论(0编辑  收藏  举报