网络基础框架

1、学习了Android网络基本框架,了解了UI的get和post用法的区别

2AsyncTask异步任务类,必须通过此类来实现

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

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值