1、学习了Android网络基本框架,了解了UI的get和post用法的区别
2、AsyncTask异步任务类,必须通过此类来实现
3、使用这两种用法获取内容
以下程序为get和post方法的具体使用:
package com.example.administrator.jreduch07; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class HttpActivity extends AppCompatActivity { private EditText et; private Button search; private Button search1; private TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_http); et=(EditText)findViewById(R.id.et); search=(Button)findViewById(R.id.search); search1=(Button)findViewById(R.id.search1); show=(TextView)findViewById(R.id.show); search.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String url="http://192.168.23.1:8080/HttpTest/index.jsp?" +"option=getUser&uName=jerehedu"; new MyGetJob().execute(url); } }); search1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String[] arg=new String[2]; arg[0]="http://192.168.23.1:8080/HttpTest/index.jsp"; arg[1]="option=getUser&uName=jerehedu"; new MyPostJob().execute(arg); } }); } public class MyPostJob extends AsyncTask<String,Void,String>{ @Override protected String doInBackground(String... strings) { HttpURLConnection con = null; InputStream is = null; StringBuilder sbd = new StringBuilder(); try { URL url = new URL(strings[0]); con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(5*1000); con.setReadTimeout(5*1000); con.setRequestMethod("POST"); con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setRequestProperty("Charset","UTF-8"); con.setRequestProperty("Content-type","application/x-www-form-urlencoded"); //params应该是这样的样式 => option=getUser&uName=jerehedu String params = strings[1]; OutputStream os = con.getOutputStream(); os.write(params.getBytes()); os.flush(); os.close(); if(con.getResponseCode()==200){ is = con.getInputStream(); int next = 0 ; byte[] b = new byte[1024]; while ((next = is.read(b))>0){ sbd.append(new String(b,0,next)); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(con!=null){ con.disconnect(); } } return sbd.toString(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); show.setText("POST请求结果:"+s); } } /* * AsyncTask异步任务类(耗时的操作)有五个方法(主线程3个方法,子线程2个方法) * 异步任务类的第一个参数会传到doInBackgroung方法中 * 第三个参数是 * #指定doInBackgroung的返回值 * #doInBackgroung的返回值会被onPostExecute接收 * */ public class MyGetJob extends AsyncTask<String,Void,String >{ //onPreExecute在主线程中执行命令,进度条的初始化 @Override protected void onPreExecute() { super.onPreExecute(); } // doInBackground在子线程中执行命令 @Override protected String doInBackground(String... strings) { HttpURLConnection con=null;//访问网络 InputStream is=null; StringBuilder sbd=new StringBuilder(); try { URL url=new URL(strings[0]); con= (HttpURLConnection) url.openConnection(); con.setConnectTimeout(5*1000); con.setReadTimeout(5*1000); /*http 响应吗 * 200:成功 * 404:未找到 * 500:发生错误 */ if(con.getResponseCode()==200){ is=con.getInputStream(); int next=0; byte[] bt=new byte[1024]; while((next=is.read(bt))>0){ sbd.append(new String(bt,0,next)); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } if(con!=null){ con.disconnect(); } } return sbd.toString(); } // onPostExecute在UI线程中执行命令 @Override protected void onPostExecute(String s) { super.onPostExecute(s); show.setText(s); } } }