开发学院手机开发Android 开发 Android HttpClient网络通信 阅读

Android HttpClient网络通信

 2010-07-13 21:24:00 来源:WEB开发网   
核心提示:btn_get.setOnClickListener(new Button.OnClickListener(){public void onClick(View v) {// TODO Auto-generated method stubString httpUrl = "http://192.168.0.1

btn_get.setOnClickListener(new Button.OnClickListener(){

public void onClick(View v) {

// TODO Auto-generated method stub

String httpUrl = "http://192.168.0.132:8080/Android/httpreq.jsp?par=request-get";

HttpGet request = new HttpGet(httpUrl);

HttpClient httpClient = new DefaultHttpClient();

try {

HttpResponse response = httpClient.execute(request);

if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){

String str = EntityUtils.toString(response.getEntity());

tv_rp.setText(str);

}else{

tv_rp.setText("请求错误");

}

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

btn_post.setOnClickListener(new Button.OnClickListener(){

public void onClick(View v) {

// TODO Auto-generated method stub

String httpUrl = "http://192.168.0.132:8080/Android/httpreq.jsp";

HttpPost request = new HttpPost(httpUrl);

List params = new ArrayList();

params.add(new BasicNameValuePair("par","request-post"));

try {

HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");

request.setEntity(entity);

HttpClient client = new DefaultHttpClient();

HttpResponse response = client.execute(request);

if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){

String str = EntityUtils.toString(response.getEntity());

tv_rp.setText(str);

}else{

tv_rp.setText("请求错误");

}

} catch (UnsupportedEncodingException e) {

上一页  1 2 3 4 5 6 7 8  下一页

Tags:Android HttpClient 网络通信

编辑录入:coldstar [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接