JDK的get请求方式

package com.example.wang.testapp3;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class TestActivity3 extends AppCompatActivity {

    EditText et_2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test3);

        et_2=(EditText)findViewById(R.id.et_2);

    }

    //JDK的Get方式

        public void bt1_OnClick(View v)
        {
            //1.进度对话框
            final ProgressDialog progressDialog = ProgressDialog.show(this, null, "正在加载,请稍候...");

            //2.开启子线程,访问网络
            new Thread(){

                public void run()
                {
                    try {
                        // 1 - URL
                        URL url = new URL("http://www.baidu.com");

                        // 2- URL获取连接
                        HttpURLConnection huc = (HttpURLConnection)url.openConnection();

                        //请求方式
                        huc.setRequestMethod("GET");
                        //设置超时
                        huc.setConnectTimeout(3000);
                        huc.setReadTimeout(3000);

                        //连接并发送请求
                        huc.connect();

                        //接收:
                        // 判断返回状态码 200
                        int code = huc.getResponseCode();

                        if (code == 200) {
                            // 接收数据

                            // 输入流:
                            InputStream is = huc.getInputStream();

                            //读取流

                            //1-byte数组
                            byte[] b = new byte[1024];

                            //2-读到数组的长度
                            int i = 0;

                            //3-数据
                            final StringBuilder sb1 = new StringBuilder();

                            while((i = is.read(b)) > 0)
                            {
                                sb1.append(new String(b, 0, i));
                            }

                            // 释放资源

                            is.close();
                            huc.disconnect();

                            // 通过主线程显示信息和关闭对话框

                            runOnUiThread(new Runnable() {
                                              @Override
                                              public void run() {

                                                  et_2.setText(sb1);

                                                  progressDialog.dismiss();

                                                  Log.e("TAG", "连接错误11");

                                              }
                                          }
                            );
                        }
                        else
                        {
                            Toast.makeText(TestActivity3.this, "连接错误,返回的状态码 = " + code, Toast.LENGTH_SHORT).show();
                            Log.e("TAG","连接错误");
                        }
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();

                        progressDialog.dismiss();
                    }
                }

            }.start();
        }

}
java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.wang.testapp3.TestActivity3"
    android:orientation="vertical">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="JDK-Get方式"
        android:onClick="bt1_OnClick"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/et_2"/>


</LinearLayout>
xml

posted @ 2016-06-12 22:49  1011042043  阅读(148)  评论(0编辑  收藏  举报