private String baseUrl="http://192.168.1.100:8080/serverside/name"; private String name,age; private HttpResponse httpResponse; private HttpEntity httpEntity; private InputStream inputStream; public void onClick(View v){ //把请求的数据,添加到NameValuePair中 NameValuePair nameValuePair1=new BasicNameValuePair("name", name); NameValuePair nameValuePair2=new BasicNameValuePair("age", age); //将其添加到list列表中 List list=new ArrayList(); list.add(nameValuePair1); list.add(nameValuePair2); try { //将列表添加到HttpEntity中 HttpEntity requesthttpEntity=new UrlEncodedFormEntity(list); //生成一个请求对象 HttpPost httpPost=new HttpPost(baseUrl); //httpPost设置Entity ==把数据添加到了httpPost中 httpPost.setEntity(requesthttpEntity); //生成一个http客户端对象 HttpClient httpClient=new DefaultHttpClient(); httpResponse=httpClient.execute(httpPost); httpEntity=httpResponse.getEntity(); inputStream=httpEntity.getContent(); BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream)); String result=""; String line=""; while((line=bufferedReader.readLine()) != null){ result=result+line; } System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } |