Skip to content

Instantly share code, notes, and snippets.

@Tikitoo
Created July 3, 2015 10:00
Show Gist options
  • Save Tikitoo/1075faa4368aa28deec2 to your computer and use it in GitHub Desktop.
Save Tikitoo/1075faa4368aa28deec2 to your computer and use it in GitHub Desktop.
Android http request response.
package com.tikitoo.android.http.activity;
// ... import
public class MyActivity extends Activity {
private Button httpBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
httpBtn = (Button) findViewById(R.id.button_http_id);
httpBtn.setOnClickListener(new MyBtnListener());
}
// 在主线程中调用Network Thread
class MyBtnListener implements View.OnClickListener {
public void onClick(View v) {
new NetWorkThread().start();
}
}
// 访问网络的线程
class NetWorkThread extends Thread {
@Override
public void run() {
// 创建HttpClient 对象
HttpClient httpClient = new DefaultHttpClient();
// 创建代表请求的对象,参数是访问服务器地址,
HttpGet httpGet = new HttpGet("http://www.marschen.com/data1.html");
// 自定义请求内容,如果不设置,请求头获取的内容为空
httpGet.setHeader("name", "Tikitoo");
httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.6,en;q=0.4");
// 获取请求头(Request Headers)的内容
Header[] reqHeaders = httpGet.getAllHeaders();
// 请求获取服务器返回的相应对象
for(Header hd : reqHeaders) {
String name = hd.getName();
String value = hd.getValue();
System.out.println(name + ": " + value);
}
try {
HttpResponse resp = httpClient.execute(httpGet);
// 检查相应的状态是否正常,状态码为200,则为正常
// 获取响应头(Response Headers)的内容
Header[] respHeanders = resp.getAllHeaders();
for(Header hd : respHeanders) {
String name = hd.getName();
String value = hd.getValue();
System.out.println(name + ": " + value);
}
int code = resp.getStatusLine().getStatusCode();
if (code == 200) {
// 从相应对象取出数据
HttpEntity entity = resp.getEntity();
InputStream in = entity.getContent();
// 使用缓存流读取数据
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = br.readLine();
Log.e("Http", "Data: " + line);
// 关闭流资源
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 使用Post 方式提交
class PostThread extends Thread {
String name;
String pwd;
public PostThread(String name, String pwd) {
this.name = name;
this.pwd = pwd;
}
@Override
public void run() {
// 构造请求对象
HttpClient httpClient = new DefaultHttpClient();
// 获取到数据,使用Post 方式请求
String url = "http://192.168.56.1:8081/MyWeb/mail.jsp";
HttpPost httpPost = new HttpPost(url);
// NameValuePair 对象代表一个需要啊发往服务器的键值对
NameValuePair namePair = new BasicNameValuePair("name", name);
NameValuePair pwdPair = new BasicNameValuePair("pwd", pwd);
// 将准备好的键值对对象 添加到List
ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(namePair);
pairs.add(pwdPair);
try {
// 创建代表请求体的对象
HttpEntity reqEntity = new UrlEncodedFormEntity(pairs); // 可能会出现编码错误异常
// 将请求体放在在请求对象当中
httpPost.setEntity(reqEntity);
try {
HttpResponse response = httpClient.execute(httpPost);
// 获取状态码,200 表示成功
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
HttpEntity httpEntity = response.getEntity();
InputStream in = httpEntity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = null; //br.readLine();
// 为了避免第一行为空,读取不到数据,所有使用while 读取所有数据
while ((line = br.readLine()) != null) {
Log.e("Resp", line);
}
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}// run end
}
package com.tikitoo.android.http.activity;
// ... import
public class ReqActivity extends Activity {
private EditText nameText, pwdText;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.req_layout);
nameText = (EditText) findViewById(R.id.edit_text_req_name_id);
pwdText = (EditText) findViewById(R.id.edit_text_req_pwd_id);
button = (Button) findViewById(R.id.button_req_id);
// 按钮绑定监听器
button.setOnClickListener(new MyBtnListener());
}
// 定义监听器
class MyBtnListener implements View.OnClickListener {
@Override
public void onClick(View v) {
// 获取文本输入框的内容,并将数据传入GetThread
String name = nameText.getText().toString();
String pwd = pwdText.getText().toString();
System.out.println("name = " + name + "; pwd = " + pwd);
GetThread gt = new GetThread(name, pwd);
gt.start();
}
}
class GetThread extends Thread {
String name;
String pwd;
public GetThread(String name, String pwd) {
this.name = name;
this.pwd = pwd;
}
@Override
public void run() {
HttpClient httpClient = new DefaultHttpClient();
// 获取到数据,使用Get 方式请求,并将两个字段传入
// http://192.168.56.1:8081/MyWeb/mail.jsp?name=tikitoo&pwd=dfa
String url = "http://192.168.56.1:8081/MyWeb/mail.jsp?name=" + name + "&pwd=" + pwd;
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(httpGet);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
HttpEntity httpEntity = response.getEntity();
InputStream in = httpEntity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = null; //br.readLine();
// 为了避免第一行为空,读取不到数据,所有使用while 读取所有数据
while ((line = br.readLine()) != null) {
Log.e("Resp", line);
}
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment