HttpURLConnection
2016-07-13 16:36
363 查看
第一行代码上的内容
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends Activity {
private WebView wv;
private URL url;
private HttpURLConnection connection;
private Button b1;
private InputStream in;
private TextView tv1;
private BufferedReader reader;
private StringBuilder sb;
public static final int SHOW_RESPONSE = 0;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_RESPONSE:
String str = (String) msg.obj;
tv1.setText(str);
break ;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.b1);
tv1 = (TextView) findViewById(R.id.response);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fun3();
}
});
}
private void fun3() {
new Thread(new Runnable() {
@Override
public void run() {
connection = null;
try {
url = new URL("http://www.baidu.com");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
in = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
sb = new StringBuilder();
String line;
while ((line = reader.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();
}
}
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends Activity {
private WebView wv;
private URL url;
private HttpURLConnection connection;
private Button b1;
private InputStream in;
private TextView tv1;
private BufferedReader reader;
private StringBuilder sb;
public static final int SHOW_RESPONSE = 0;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_RESPONSE:
String str = (String) msg.obj;
tv1.setText(str);
break ;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.b1);
tv1 = (TextView) findViewById(R.id.response);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fun3();
}
});
}
private void fun3() {
new Thread(new Runnable() {
@Override
public void run() {
connection = null;
try {
url = new URL("http://www.baidu.com");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
in = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
sb = new StringBuilder();
String line;
while ((line = reader.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();
}
}
相关文章推荐
- HTTP协议详解
- 使用OkHttp包在Android中进行HTTP头处理的教程
- WIFI密码破解与网络数据抓包
- java-网络编程-大文件搬运
- 【iOS】网络操作与AFNetworking
- 原生网络连接方式
- http post 请求
- IIS7.5 Error Code 0x8007007e HTTP 错误 500.19
- HTTP学习之使用HTTPS访问百度
- 组策略映射网络驱动器
- Volley 分析
- 吉林省长春市经济技术开发区招聘网络管理员(桌面工程师)
- Springmvc ModelAndView踩过的坑之HttpServletResponse response
- http://www.shouce.ren/ 手册网
- httpd属性和配置
- Android的OkHttp包处理用户认证的代码实例分享
- 登录密码与HTTP Request
- Android Volley完全解析(二),使用Volley加载网络图片
- 《图解HTTP》学习笔记
- Java网络编程学习笔记(一)基本网络概念