zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    css样式之补充
    css样式之属性操作
    css之操作属性
    前端基础之css样式(选择器)
    mysql之视图,触发器,事务等
    pymysql模块
    MySQL之IDE工具介绍及数据备份
    mysql老是停止运行该怎么解决
    mysql之索引原理与慢查询优化
    MySQL 5.7.9版本sql_mode=only_full_group_by问题
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11781946.html
Copyright © 2011-2022 走看看