码迷,mamicode.com
首页 > Web开发 > 详细

HTTPClient

时间:2015-11-02 18:57:40      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

package com.example.httpclient;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

EditText username;
EditText password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

username = (EditText) findViewById(R.id.et_username);
password = (EditText) findViewById(R.id.et_password);

}


public void getSubmit(View view)
{

new Thread(new Runnable() {

@Override
public void run() {
//1.定义一个客户端 你可以理解为是一个浏览器
HttpClient client = new DefaultHttpClient();

//2. 定义一个get请求,并封装好他的参数
String data = "username=" + username.getText().toString() + "&password=" + password.getText().toString();
HttpGet get = new HttpGet("http://10.0.2.2:8080/TestHttp/HttpServlet?" + data);

//3. 用定义好的浏览器来执行一个地址
try {
HttpResponse response = client.execute(get);
//4. 获取状态码,看返回回来的是否是200
final int statuscode = response.getStatusLine().getStatusCode();
if(200 == statuscode)
{
InputStream in = response.getEntity().getContent();

//处理从服务器端返回回来的数据
final String content = getStringFromStream(in);

runOnUiThread(new Runnable() {

@Override
public void run() {
Toast.makeText(MainActivity.this, "状态码为:" + statuscode +" 内容为:" + content, 0).show();
}
});
}

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally
{
//5. 用完以后关闭浏览器
if(null != client)
{
client.getConnectionManager().shutdown();
}
}
}
}).start();

}

public void postSubmit(View view)
{
new Thread(new Runnable() {

@Override
public void run() {
//1.顶一个一个浏览器对象
HttpClient client = new DefaultHttpClient();
//2. 定义一个post请求的对象
HttpPost post = new HttpPost("http://10.0.2.2:8080/TestHttp/HttpServlet");

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

NameValuePair pair_name = new BasicNameValuePair("username", username.getText().toString());
NameValuePair pair_pwd = new BasicNameValuePair("password", password.getText().toString());

parameters.add(pair_name);
parameters.add(pair_pwd);

//3.执行一个请求
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "utf-8");
post.setEntity(entity);

HttpResponse response = client.execute(post);

final int statuscode = response.getStatusLine().getStatusCode();
if(200 == statuscode)
{
InputStream in = response.getEntity().getContent();

//处理从服务器端返回回来的数据
final String content = getStringFromStream(in);

runOnUiThread(new Runnable() {

@Override
public void run() {
Toast.makeText(MainActivity.this, "状态码为:" + statuscode +" 内容为:" + content, 0).show();
}
});
}


} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}).start();
}

public String getStringFromStream(InputStream in) throws IOException
{
byte[] buffer = new byte[1024];
ByteArrayOutputStream bytearray = new ByteArrayOutputStream();
int len = 0;
while((len = in.read(buffer, 0, 1024)) != -1)
{
bytearray.write(buffer);
}

String content = bytearray.toString();
bytearray.close();

return content;
}
}

HTTPClient

标签:

原文地址:http://www.cnblogs.com/wt-w/p/4930843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!