Android笔记(二十二) HttpURLConnection

一、 HttpURLConnection用法

  1. 获取 HttpURLConnection实例
  2. 设置 HTTP 请求所使用的方法
  3. 调用 getInputStream()方法获取服务器返回的输入流
  4. 对输入流进行读取
  5. 将连接关闭

二、具体实例

1.建立布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >

    <Button  android:id="@+id/send_request" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send Request" />

    <ScrollView  android:layout_width="match_parent" android:layout_height="match_parent" >

        <TextView  android:id="@+id/response" android:layout_width="match_parent" android:layout_height="wrap_content" />
    </ScrollView>

</LinearLayout>

2.添加权限

uses-permission android:name="android.permission.INTERNET"

3.MainActivity

public class MainActivity extends ActionBarActivity implements OnClickListener {

    public static final int SHOW_RESPONSE = 0;
    private Button button;
    private TextView responseText;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch (msg.what) {
            case SHOW_RESPONSE:
                String response = (String) msg.obj;
                responseText.setText(response);
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.send_request);
        responseText = (TextView) findViewById(R.id.response);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.send_request) {
            sendRequestWithHttpURLConnection();
        }
    }

    private void sendRequestWithHttpURLConnection() {
        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 reader = new BufferedReader(
                            new InputStreamReader(in));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                    Message msg = Message.obtain();
                    msg.what = SHOW_RESPONSE;
                    msg.obj = response.toString();
                    handler.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                }

            }

        }).start();
    }
    }

你可能感兴趣的:(android,网络)