概述
在 Android 上发送 HTTP 请求的方式一般有两种,HttpURLConnection 和 HttpClient。下面是 HttpURLConnection 的实例:
直接贴代码了,在布局文件中是一个按钮和一个TextView,按钮用来发送请求,TextView用来接收服务器返回的消息:
public class FirstActivity extends Activity implements OnClickListener{
public static final int SHOW_RESPONSE = 0; //把这个操作事件化,当作一个事件来处理
private Button send_request;
private TextView responseText;
//
处理返回的Message
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_RESPONSE:
String response = (String) msg.obj;
// 在这里进行UI操作,将结果显示到界面上
responseText.setText(response);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
send_request = (Button) findViewById(R.id.btn_send_request);
responseText = (TextView) findViewById(R.id.tv_response);
send_request.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.btn_send_request){
sendRequestWithHttpURLConnection();
}
}
private void sendRequestWithHttpURLConnection() {
// 开启线程来发起网络请求
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
HttpURLConnection connection = null;
try {
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_RESPONSE:
String response = (String) msg.obj;
// 在这里进行UI操作,将结果显示到界面上
responseText.setText(response);
}
}
};
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
send_request = (Button) findViewById(R.id.btn_send_request);
responseText = (TextView) findViewById(R.id.tv_response);
send_request.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.btn_send_request){
sendRequestWithHttpURLConnection();
}
}
// 开启线程来发起网络请求
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
HttpURLConnection connection = null;
try {
//
1,获取URL和
HttpURLConnection
对象
URL url = new URL(" http://www.baidu.com");
connection = (HttpURLConnection) url.openConnection();
URL url = new URL(" http://www.baidu.com");
connection = (HttpURLConnection) url.openConnection();
//
2,确定请求方法
connection.setRequestMethod("GET");
connection.setRequestMethod("GET");
//
3,确定连接的各种属性
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
//
4,得到和读取连接得到的输入流,并进行处理
InputStream in = connection.getInputStream();
// 下面对获取到的输入流进行读取
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
InputStream in = connection.getInputStream();
// 下面对获取到的输入流进行读取
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
//
逐行读取赋给line,然后再添加给response字符串
while((line = reader.readLine())!=null){
response.append(line);
}
Message message = new Message();
message.what = SHOW_RESPONSE;
// 将服务器返回的结果存放到Message中
message.obj = response.toString();
handler.sendMessage(message);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}
}
while((line = reader.readLine())!=null){
response.append(line);
}
message.what = SHOW_RESPONSE;
// 将服务器返回的结果存放到Message中
message.obj = response.toString();
handler.sendMessage(message);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}
}
具体的代码解释已不再赘述,关键的流程还是很简单的,共同进步。
最后
以上就是哭泣帅哥为你收集整理的android菜鸟进阶之路—— HttpURLConnection 的实例的全部内容,希望文章能够帮你解决android菜鸟进阶之路—— HttpURLConnection 的实例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复