简书手机版下载
做一个解析网站并将网站转换为字符串的小应用,如下图所示
运行结果.jpg
步骤如下:
1、UI布局
2、在MainActivity中
首先找到添加的控件
privatevoidfindViews(){ mEditText = (EditText) findViewById(R.id.editText); mTextView = (TextView) findViewById(R.id.textView); mButton = (Button) findViewById(R.id.button); }
设置点击事件
privatevoidsetListeners(){ mButton.setOnClickListener(this); }
在onClick()事件中添加EditText网址的读取
privateStringgetEditText(){returnmEditText !=null? mEditText.getText().toString() :""; }
在onClick()事件中添加AsyncTask异步任务处理
classRequestNetworkDataTaskextendsAsyncTask{//在后台work之前@Overrideprotectedvoid onPreExecute() {super.onPreExecute();//主线程//UI Loading}@OverrideprotectedStringdoInBackground(String[] params) {//请求网络数据Stringresult = requestData(params[0]);returnresult; }@Overrideprotectedvoid onPostExecute(Stringresult) {super.onPostExecute(result);//设置TextViewmTextView.setText(result); } }
请求网络数据
privateStringrequestData(String urlString){try{ URL url =newURL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(30000); connection.setRequestMethod("GET"); connection.connect();intresponseCode = connection.getResponseCode(); String result =null;if(responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); Reader reader =newInputStreamReader(inputStream,"UTF-8");char[] buffer =newchar[1024]; reader.read(buffer); result =newString(buffer); }else{ }returnresult; }catch(IOException e) { e.printStackTrace(); }returnnull; }
3、在AndroidManifest中添加网络权限
完整项目代码
packagecom.geekband.networkdemo;importandroid.os.AsyncTask;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.TextView;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.Reader;importjava.net.HttpURLConnection;importjava.net.URL;publicclassMainActivityextendsAppCompatActivityimplementsView.OnClickListener{privateEditText mEditText;privateButton mButton;privateTextView mTextView;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViews(); setListeners(); }privatevoidsetListeners(){ mButton.setOnClickListener(this); }privatevoidfindViews(){ mEditText = (EditText) findViewById(R.id.editText); mButton = (Button) findViewById(R.id.button); mTextView = (TextView) findViewById(R.id.textView); }@OverridepublicvoidonClick(View v){switch(v.getId()) {caseR.id.button: String url = getEditText();//申请网络权限// String data = requestData(url);newRequestNetworkDataTask().execute(url);break; } }privateStringgetEditText(){returnmEditText !=null? mEditText.getText().toString() :""; }//异步任务处理classRequestNetworkDataTaskextendsAsyncTask{//在后台work之前@OverrideprotectedvoidonPreExecute(){super.onPreExecute();//主线程//UI Loading}@OverrideprotectedStringdoInBackground(String[] params){//请求网络数据String result = requestData(params[0]);returnresult; }@OverrideprotectedvoidonPostExecute(String result){super.onPostExecute(result);//设置TextViewmTextView.setText(result); } }privateStringrequestData(String urlString){try{ URL url =newURL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(30000); connection.setRequestMethod("GET"); connection.connect();intresponseCode = connection.getResponseCode(); String result =null;if(responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); Reader reader =newInputStreamReader(inputStream,"UTF-8");char[] buffer =newchar[1024]; reader.read(buffer); result =newString(buffer); }else{ }returnresult; }catch(IOException e) { e.printStackTrace(); }returnnull; }}