使用HttpURLConnection发送HTTP请求
2016-02-01 14:47
525 查看
import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.ref.WeakReference; import java.net.HttpURLConnection; import java.net.URL; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ public static final int SHOW_RESPONSE = 0; private Button sendResquest; private TextView responseText; MyHandler handler = new MyHandler(this); static class MyHandler extends Handler{ WeakReference<MainActivity> mActivity; MyHandler(MainActivity mainActivity){ mActivity = new WeakReference<MainActivity>(mainActivity); } @Override public void handleMessage(Message msg) { MainActivity activity = mActivity.get(); switch (msg.what){ case SHOW_RESPONSE: String response = (String)msg.obj; activity.responseText.setText(response); } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendResquest = (Button)findViewById(R.id.send_requset); responseText = (TextView)findViewById(R.id.response_text); sendResquest.setOnClickListener(this); } @Override public void onClick(View v) { if (v.getId()==R.id.send_requset){ sendRequsetWithHttpURLConnection(); } } private void sendRequsetWithHttpURLConnection(){ new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; try { URL url = new URL("http://www.baidu.com"); connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); InputStream in = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); StringBuilder sb = new StringBuilder(); String line; while((line = br.readLine())!=null){ sb.append(line); } Message message = new Message(); message.what = SHOW_RESPONSE; message.obj = sb.toString(); handler.sendMessage(message); }catch (Exception e){ e.printStackTrace(); } finally { if (connection!=null){ connection.disconnect(); } } } }).start(); } }
相关文章推荐
- java-用HttpURLConnection发送Http请求.
- Android封装的http请求实用工具类
- Android程序开发通过HttpURLConnection上传文件到服务器
- Android 中HttpURLConnection与HttpClient使用的简单实例
- Android通过HttpURLConnection和HttpClient接口实现网络编程
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- php 利用socket发送HTTP请求(GET,POST)
- 谈谈Java利用原始HttpURLConnection发送POST数据
- 使用Fiddler监听Java HttpURLConnection请求的小技巧
- 如何使用 java.net.URLConnection 处理 HTTP 请求?
- Android 网络通信框架Volley简介
- 微信开发者-主动请求-准备工作-(1)发送get、post请求,上传文件(C#)
- HttpURLConnection的使用
- 用Android的imageVew显示网络和本地的图片
- HttpURLConnection getInputStream异常的解决
- http封装
- android 各种简单的获取网页源码方式
- android之HttpURLConnection
- 使用HttpUrlConnection执行Post请求,出现EOFException
- Android 文件上传(客户端+服务端)