1.android的API提供了访问网络的一个类HttpURLConnection
2.通过发送GET请求获取服务器返回的html代码
3.先看看布局文件,如下所示,
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button android:onClick="get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取源码"/>
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#00f"
android:hint="源码显示"/>
</ScrollView></LinearLayout>
布局样子:
4.下来是清单文件,记得连接网络要添加权限
5.1再看java代码,MainActivity的
package com.market.source;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.EditText;import android.widget.TextView;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.ProtocolException;import java.net.URL;import java.net.URLConnection;import butterknife.BindView;import butterknife.ButterKnife;public class MainActivity extends Activity {
@BindView(R.id.et)
EditText et;
@BindView(R.id.tv)
TextView tv;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
} public void get(View vew) throws IOException { final String str = et.getText().toString().trim(); new Thread(){
@Override public void run() { //1.请求地址url
URL url = null; try {
url = new URL(str); //2.获取对这个地址的连接
HttpURLConnection con = (HttpURLConnection) url.openConnection(); //3.设置对这个地址的请求,GET请求
con.setRequestMethod("GET"); //4.设置请求参数
con.setConnectTimeout(5000); //5.获取服务器的响应
int code = con.getResponseCode(); //6.根据响应吗,判断请求成功还是失败,200成功
if(code==200){ //7.成功的话,服务器一流的形式返回数据
InputStream inputStream = con.getInputStream(); //8.这个流是字节流,需要我们转换为字符流才可以认识
final String info = StreamTool.StreamtoString(inputStream);
Log.e("MainActivity",info);
runOnUiThread(new Runnable() {
@Override public void run() {
tv.setText(info);
}
});
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
5.2工具类,用来将一个流转化为字符串
public class StreamTool { public static String StreamtoString(InputStream inputStream) { int len = -1; byte[] buffer = new byte[1024]; //内存数组输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { while((len=inputStream.read(buffer)) != -1){
baos.write(buffer,0,len);
}
String str = new String(baos.toByteArray()); return str;
} catch (IOException e) {
e.printStackTrace();
}finally { try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} return null;
}
}
6.运行效果查看
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。