HttpClient 请求网络数据的get post方法
创建请求的工具类 此类中的请求数据的方法 需在 线程中运行
public class MyHttpClient {
public String Get(String userName,String passWord){
System.out.println("11111111111111");
//请求服务器地址
String path="http://169.254.226.103:8080//LogServer/servlet/LogServlet?username="+userName+"&password="+passWord;
System.out.println("22222222222");
//创建HttpClient实例对象
HttpClient client=new DefaultHttpClient();
System.out.println("44444444444");
//创建请求方式对象
HttpGet get=new HttpGet(path);
System.out.println("555555555555");
try {
//发送请求 获取一个 结果对象
HttpResponse response = client.execute(get);
System.out.println("7777777777");
//获取请求行中的 结果吗
int code=response.getStatusLine().getStatusCode();
System.out.println("66666666666666");
if(code==200){
//得到服务器返回的流
InputStream is = response.getEntity().getContent();
//将流读取为字符串
byte[] arr=new byte[1024];
int len;
StringBuilder builder=new StringBuilder();
while((len=is.read(arr))!=-1){
builder.append(new String(arr, 0,len));
}
return builder.toString();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("33333333333");
return null;
}
public String Post(String userName,String passWord){
//请求服务器地址
String path="http://169.254.226.103:8080//LogServer/servlet/LogServlet";
//创建Client对象
HttpClient client=new DefaultHttpClient();
//创建Post
HttpPost post=new HttpPost(path);
List<NameValuePair> parameters=new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("username", userName));
parameters.add(new BasicNameValuePair("password", passWord));
try {
HttpEntity entity=new UrlEncodedFormEntity(parameters);
post.setEntity(entity);
HttpResponse response = client.execute(post);
int code=response.getStatusLine().getStatusCode();
if(code==200){
InputStream is = response.getEntity().getContent();
//将流读取为字符串
byte[] arr=new byte[1024];
int len;
StringBuilder builder=new StringBuilder();
while((len=is.read(arr))!=-1){
builder.append(new String(arr, 0,len));
}
return builder.toString();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
从Activit中调用上边求数据的方法 如下
public class MainActivity extends Activity {
private EditText ed2;
private EditText ed1;
private String result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
ed2 = (EditText) findViewById(R.id.et2);
ed1 = (EditText) findViewById(R.id.et1);
}
/*
* Get 请求方式
* */
public void LogGet(View view){
final String userName=ed2.getText().toString().trim();
final String passWord=ed1.getText().toString().trim();
new Thread(){
public void run(){
result = new MyHttpClient().Get(userName, passWord);
if(result==null){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "访问服务器失败", 0).show();
}
});
}else{
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this,result , 0).show();
}
});
}
}
}.start();
}
/*
* Post 请求方式
* */
public void LogPost(View view){
final String userName=ed2.getText().toString().trim();
final String passWord=ed1.getText().toString().trim();
new Thread(){
public void run(){
result = new MyHttpClient().Post(userName, passWord);
if(result==null){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "访问服务器失败", 0).show();
}
});
}else{
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this,result , 0).show();
}
});
}
}
}.start();
}
}