android studio 开启网络,Android Studio开发-day1 使用网络连接

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这一行

package="com.example.administrator.demoapp">

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">

布局

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="hello"

/>

android:text="Button"

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:text="TextView"

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

6fa355e7ef0a

布局图片

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();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值