第一个项目关于HttpUrlConnection的get方法。
HttpUrlConnection是java.net包下的。是java自带的。
<pre name="code" class="java">package com.example.dugaolong.httpurlconnectiondemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* 标准Java接口(java.net) ----HttpURLConnection,可以实现简单的基于URL请求、响应功能;
* <p>
* HttpUrlConnection
* get
* <p>
*/
public class MainActivity extends Activity {
private final String TAG = this.getClass().getSimpleName();
private TextView textView;
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//在ui线程中定义handler
mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what == 1){
textView.setText(msg.obj.toString());
}
}
};
Button button = (Button) findViewById(R.id.bt_get);
textView = (TextView) findViewById(R.id.tv_message);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getHttpUrlConnection();
}
});
}
/**
* get方式请求网络
*/
private void getHttpUrlConnection() {
final String getUrl = "http://172.16.150.71:8080/http/servlet/Http?sex=girl";
//请求网络必须要开新的子线程
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(getUrl);//URL对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();//使用URL打开一个链接
conn.setDoInput(true);
//接收返回结果
if (conn.getResponseCode() == 200) {
//读取返回的流信息
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
String readLine = "";
while ((readLine = br.readLine()) != null) {//一行一行读取
sb.append(readLine);
}
//另外一种读取流内容的方法
// InputStream in = conn.getInputStream();
// byte[] b = new byte[4096];
// StringBuffer out = new StringBuffer();
// for (int n; (n = in.read(b)) != -1; ) {
// out.append(new String(b, 0, n));
// }
// textView.setText(sb.toString());
//关闭流
is.close();
br.close();
conn.disconnect();
Message msg = new Message();
msg.what=1;
msg.obj=sb.toString();
mHandler.sendMessage(msg);
Log.i(TAG, "reaponse:" + sb.toString());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
//另外一种写法
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1){
textView.setText(msg.obj.toString());
}
}
}
}
activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/bt_get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="doGet"/>
<TextView
android:id="@+id/tv_message"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
记得在manifest文件中添加权限。
<uses-permission android:name="android.permission.INTERNET"/>
HttpUrlConnection的get方式简单只要在url中拼接参数就ok。