• HttpURLConnection基本工作原理


    运行截图
    这里写图片描述
    MainActivity.java

    package csdn.example.com.notification.NetWorkTest;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    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.URL;
    
    import csdn.example.com.notification.R;
    
    public class Main6Activity extends AppCompatActivity implements View.OnClickListener {
        TextView request_text;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main6);
            request_text = (TextView) findViewById(R.id.request_text);
            Button send_Request = (Button) findViewById(R.id.send_request);
            send_Request.setOnClickListener(this);
    
        }
    
    
        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.send_request){
                Log.d("T","没毛病,老铁");
                sendRequestWithHttpURLConnection();
            }
        }
    
        private void sendRequestWithHttpURLConnection() {
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    HttpURLConnection connection = null;
                    BufferedReader reader = null;
                    try {
                        URL url = new URL("http://blog.csdn.net/cccrunner/article/details/76020211");
                        connection = (HttpURLConnection) url.openConnection();
                        connection.setRequestMethod("GET");
                        connection.setConnectTimeout(8000);
                        connection.setReadTimeout(8000);
                        InputStream in = connection.getInputStream();
                        reader = new BufferedReader(new InputStreamReader(in));
                        String line;
                        StringBuilder reaponse = new StringBuilder();
                        Log.d("T","没毛病,老铁2");
                        Integer count = 0;
                        while ((line=reader.readLine())!=null){
                            count++;
                            line = "第"+count+"行代码为: "+line+"
    
    ";
                            reaponse.append(line);
    
                        }
                        showResponse(reaponse.toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }finally {
                        if(reader!=null){
                            try {
                                reader.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if(connection!=null){
                            connection.disconnect();
                        }
                    }
                }
            }).start();
        }
    
        private void showResponse(final String response) {
           runOnUiThread(new Runnable() {
               @Override
               public void run() {
                   Log.d("T","没毛病,老铁1"+response);
                   request_text.setText(response);
               }
           });
        }
    }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/send_request"
            android:text="发送请求"
            />
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/request_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </ScrollView>
    </LinearLayout>

    清单文件AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="csdn.example.com.notification">
    
        <uses-permission android:name="android.permission.VIBRATE" />
        <uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.INTERNET" />
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <provider
                android:name="android.support.v4.content.FileProvider"
                android:authorities="csdn.example.com.notification.CameraAlbumTest"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/file_paths" />
            </provider>
            <activity android:name=".NetWorkTest.Main6Activity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
        </application>
    
    </manifest>
  • 相关阅读:
    记账程序2.0
    安装redHat6.5详细图文教程
    redHat6设置ip地址
    如何开通B站的直播空间
    python实战技巧之两个字典,如何实现键同则值相加【不等长或等长】
    python实战技巧之去除列表中的空值元素
    python实战技巧之两个不等长列表让里面的数字一一对应地相加
    python实战技巧之两个列表实例中,如何让里面的数字一一对应地相加【对于两个列表是等长的情况】
    java基础_网络编程
    java基础_String
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11781945.html
Copyright © 2020-2023  润新知