Android Studio开发-day1 使用网络连接

2017-08-03  本文已影响0人  凌勇

Android 网络开发

Android Studio初步配置

下载安装完AS之后,使用红米note3作为测试用机,需要将开发者模式打开,即在系统设置里面找到MIUI版本那一行,点击多下后即可开启,点击上一行Android版本还会出一个小彩蛋。
打开开发者模式,选上USB调试和USB安装功能,再连接上USB线,选择USB模式为文件传输,这样初步配置完成。如果到这里AS中没发现设备的话可以先使用某些手机助手先连接一下,之后AS顺利连接。

关于网络连接开发这一块,有几个小问题,第一个是AS默认不支持org.apache.http下面的包,所以引这其中的包会报错,这里要选中File-Project Structrue-app-dependence,在搜索框中搜索Apache.http,即可找到相关包。第二个(2017.8) Android不支持在OnCreate方法中连接网络,所以需要加上两行代码: 效果拔群

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()  
        .detectDiskReads().detectDiskWrites().detectNetwork()  
        .penaltyLog().build());  
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()  
        .detectLeakedSqlLiteObjects().detectLeakedClosableObjects()  
        .penaltyLog().penaltyDeath().build());  

到这里问题就差不多解决了,下面给出网络连接的代码,主要是在一个Activity中点击按钮,获取百度的html文档。也可以在Android上利用这个学习爬虫。

Manifest.xml

要在其中注册获取网络连接的请求,主要需要增加user-permission这一行

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.demoapp">
    <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">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="hello"
        />
    <Button
        android:text="Button"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </Button>
    <TextView
        android:text="TextView"
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </TextView>
</LinearLayout>
布局图片

MainActivity

package com.example.administrator.demoapp;

import android.app.Activity;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainActivity extends Activity {
    public Button btn;
    public TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork()
                .penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
                .penaltyLog().penaltyDeath().build());
        btn = (Button) findViewById(R.id.button1);
        tv = (TextView) findViewById(R.id.textView1);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new getInternetMessage().run();
            }
        });


    }
    class getInternetMessage extends Thread{
        private String content = null;
        public void run(){
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet("http://www.baidu.com");
            try {
                HttpResponse response = client.execute(request);
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        response.getEntity().getContent()));

                for(String s = reader.readLine(); s != null; s = reader.readLine()){
                    content += s;
                }

                tv.setText(content);


            } catch (IOException e) {
                e.printStackTrace();
            }


        }

    }

}

上一篇 下一篇

猜你喜欢

热点阅读