注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

BCB-DG's Blog

...

 
 
 

日志

 
 

Android与Web通信-Get和Post  

2014-08-25 11:14:14|  分类: Android |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
//转
package net.blogjava.mobile;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Main extends Activity implements OnClickListener
{
 @Override
 public void onClick(View view)
 {
  String url = "http://192.168.17.156:8080/querybooks/QueryServlet";
  TextView tvQueryResult = (TextView) findViewById(R.id.tvQueryResult);
  EditText etBookName = (EditText) findViewById(R.id.etBookName);
  HttpResponse httpResponse = null;
  try
  {
   
   switch (view.getId())
   {
    case R.id.btnGetQuery:
     url += "?bookname=" + etBookName.getText().toString();
     HttpGet httpGet = new HttpGet(url);
     httpResponse = new DefaultHttpClient().execute(httpGet);
     if (httpResponse.getStatusLine().getStatusCode() == 200)
     {
      
      String result = EntityUtils.toString(httpResponse
        .getEntity());
      tvQueryResult.setText(result.replaceAll("\r", ""));
     }
     break;
    case R.id.btnPostQuery:
     HttpPost httpPost = new HttpPost(url);
     List<NameValuePair> params = new ArrayList<NameValuePair>();
     params.add(new BasicNameValuePair("bookname", etBookName
       .getText().toString()));
     httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
     
     httpResponse = new DefaultHttpClient().execute(httpPost);
     if (httpResponse.getStatusLine().getStatusCode() == 200)
     {
      String result = EntityUtils.toString(httpResponse
        .getEntity());
      tvQueryResult.setText(result.replaceAll("\r", ""));
     }
     break;
   }
  }
  catch (Exception e)
  {
   tvQueryResult.setText(e.getMessage());
  }
 }
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Button btnGetQuery = (Button) findViewById(R.id.btnGetQuery);
  Button btnPostQuery = (Button) findViewById(R.id.btnPostQuery);
  btnGetQuery.setOnClickListener(this);
  btnPostQuery.setOnClickListener(this); 
 }
}
  评论这张
 
阅读(377)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017