package com.sumzom.teach.httpurlconnection;
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 java.net.URLConnection;
import com.example.com.sumzom.getrequest.R;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView mTextView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
String url = "http://www.weather.com.cn/data/sk/101010100.html";
initWithView();
new DownloadTextTask().execute(url);
}
private void initWithView() {
// TODO Auto-generated method stub
mTextView = (TextView) findViewById(R.id.http_get);
}
/**
* @author 欧博泰克
* @time 2015年9月23日
* @contact QQ:2356066132
* @instructions 采用httpUrlConnection进行网络传输;
* */
private InputStream openHttpCennection(String urlString) throws IOException{
InputStream is = null;//建立/声明/创建/实例化一个输入流(这四种讲法都可以)
int response = -1;//响应编码
URL url = new URL(urlString);
Log.i("openHttpCennection", urlString);
//使用httpurlconnection打开网络连接;
URLConnection urlConn = url.openConnection();
HttpURLConnection httpConn = null;
if (!(urlConn instanceof HttpURLConnection)) {
throw new IOException("无效网络连接");
}
try {
httpConn = (HttpURLConnection)urlConn;
httpConn.setAllowUserInteraction(false);//设置允许用户交互
httpConn.setInstanceFollowRedirects(true);//集实例遵循重定向
httpConn.setRequestMethod("GET");
//httpConn.setRequestProperty(field, newValue) //设置访问报头,简称报文
httpConn.connect();
response = httpConn.getResponseCode();//响应码,如果等于200则连接成功
/**
* resposeCode有两种表达方式:
* 1:response == 200;
* 2:response == HttpURLConnection;
* */
if (response == HttpURLConnection.HTTP_OK) {
is = httpConn.getInputStream();//接受输入流
}
} catch (Exception e) {
throw new IOException("网络连接错误");
}
return is;
}
/**
* i/o流是伴随着数据的读写产生的
* 1、获取输入流
* 2、读取输入流
* 3、将输入流写入buffer(作用,数据缓冲区)
* 4、从数据流从buffer一行行读出
* 5、返回读出的数据流
* */
private String downLoadText(String url){
Log.i("downLoadText", "downLoadText");
String resultData = "";
try {
InputStream is = openHttpCennection(url);
//读取输入流
InputStreamReader in = new InputStreamReader(is,"UTF-8");
//为输入创建bufferedreader
BufferedReader buffer = new BufferedReader(in);
String inputLine = null;
//循环读取网络获取数据
//buffer缓冲区
while ( (inputLine=buffer.readLine())!=null) {
resultData += inputLine + "\n";
Log.i("循环流读取", resultData);
}
//关闭流的读取(千万不能忘记)
in.close();
} catch (Exception e) {
System.out.println("循环流读取"+resultData);
e.printStackTrace();
}
return resultData;
}
private class DownloadTextTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
return downLoadText(arg0[0]);
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
mTextView.setText(result);
}
}
}