• JDK的get请求方式


    layout:

    <?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.hanqi.testapp3.TestActivity2"
        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>
    View Code

    Activity:

    package com.hanqi.testapp3;
    
    import android.app.ProgressDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    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 TestActivity2 extends AppCompatActivity {
    
        EditText et_2;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test2);
            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"+"?name=tom");
    
                        //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();
                                }
                            });
                        }
                        else
                        {
                            Toast.makeText(TestActivity2.this, "连接错误,返回的状态码 = "+code, Toast.LENGTH_SHORT).show();
                        }
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                        progressDialog.dismiss();
                    }
                }
            }.start();
        }
    }
    View Code
  • 相关阅读:
    无人转会申请
    【Alpha】“北航社团帮”小程序v1.0项目展示
    【Alpha】“北航社团帮”小程序v1.0测试报告
    【Alpha】“北航社团帮”小程序v1.0发布声明
    【Alpha】Scrum meeting 10
    编译原理:文法和语言总结与梳理
    编译原理:语法树,短语,直接短语,句柄
    编译原理:理解文法和语文
    编译原理:了解编译原理
    MySQL8版本的安装与卸载教程
  • 原文地址:https://www.cnblogs.com/beens/p/5579394.html
Copyright © 2020-2023  润新知