HttpUrlConnection 下的post, get , 和图片下载(工具类)

HttpURLConnection 下的网络请求

  • 1.IsNet->网络判断->HttpAsync中发起网络请求前使用
    2.HttpGetUtils->get请求工具类(发起请求获取服务器返回值)->
    HttpAsync->doinbackground->耗时联网操作
    • 3.HttpAsync->异步请求工具类->开启子线程耗时操作->网络判断;网络请求
    • 问题:HttpAsync获取数据无法传递给Mainactivity?
    • 回调方式解决
    • 4.NetCallBack->回调接口->回调数据
    • 5.MainActivity实现回调接口->给Netcallback接口类型赋值
    • 6.MainActivity对象传递给HttpAsync->a.上下文;b.回调接口对象
    • 7.httpAsync onPostExcute方法中使用回调传递数据给Mainactivity
    • 8.MainActivity去resultstr处理字符串操作;resultBitmap显示图片;erro提示用户错误消息
    • 9.AsyncContro控制产生HttpAsync对象以及调用excute方法开启异步类

网络判断工具类

package com.sxt.day05_network;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;

/** * 网络判断工具类 * */

public class IsNet {

    private Context ctx;//上下文

    public IsNet(Context ctx){
        this.ctx = ctx;
    }

    //返回值为true有网,为false情况下无网
    public boolean IsNetWork(){
        //ConnectivityManager->手机连接管理器
        //getSystemService->获取系统服务
        ConnectivityManager manager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
        //网络状态封装类
        NetworkInfo info;
        //网络状态
        State state;
        //TYPE_MOBILE->移动网络类型
        info = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if(info !=null){
            state = info.getState();
            //判断状态是否连接
            if(state == State.CONNECTED){
                return true;
            }
        }
        info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if(info!=null){
            state = info.getState();
            if(state == State.CONNECTED){
                return true;
            }
        }
        return false;
    }

}

http—-post 请求

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/** * POST请求工具类 * */
public class HttpPostUtils {

    //urlPath->请求地址
    //Params->请求参数
    public String PostStr(String urlPath,String Params){
        String result = null;
        try {
            URL url = new URL(urlPath);
            HttpURLConnection httpc = (HttpURLConnection) url.openConnection();
            httpc.setRequestMethod("POST");
            httpc.setConnectTimeout(60*1000);
            httpc.setReadTimeout(60*1000);
            httpc.setDoInput(true);
            httpc.setDoOutput(true);
            OutputStream out = httpc.getOutputStream();//获取上传参数的输出流
            out.write(Params.getBytes());
            out.flush();
            out.close();
            if(httpc.getResponseCode() == 200){
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(
                                httpc.getInputStream()));
                StringBuffer sb = new StringBuffer();
                String line;
                while((line = br.readLine())!=null){
                    sb.append(line);
                }
                br.close();
                result = sb.toString();
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

}

Http—–get

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.util.Log;

public class HttpGetUtils {


    /** * Get方式请求服务器数据方法 * urlPath:完整请求url地址 * result:服务器响应数据 * */
    public String getStr(String urlPath){
        String result = null;
        try {
            Log.e("","查看请求地址是否错误"+urlPath);
            //URL->服务器地址封装打开urlconnection(请求配置,发起请求,获取相应状态,获取返回流)
            URL url = new URL(urlPath);
            HttpURLConnection httpc = (HttpURLConnection) url.openConnection();
            //请求方式设定
            httpc.setRequestMethod("GET");
            //连接超时
            httpc.setConnectTimeout(60*1000);
            //读取超时
            httpc.setReadTimeout(60*1000);
            //允许读入
            httpc.setDoInput(true);
            //获取响应状态码->获取响应状态时发送请求->200 请求成功
            if(httpc.getResponseCode() == 200){
                //服务器数据读取到应用中
                //服务器返回流转化为字符流 
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(
                                httpc.getInputStream()));
                StringBuffer sb = new StringBuffer();
                String line;
                while((line = br.readLine()) != null){
                    sb.append(line);
                }
                br.close();
                result = sb.toString();
                Log.e("","查看返回值"+result);
            }
        } catch (MalformedURLException e) {
            //url生成异常
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

}

Http—–下载图片

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/** * 获取服务器图片工具类 * */
public class HttpBitmapUtils {

    public Bitmap downBitmap(String urlPath){
        Bitmap bitmap = null;
        try {
            URL url = new URL(urlPath);
            HttpURLConnection httpc = (HttpURLConnection) url.openConnection();
            httpc.setConnectTimeout(60*1000);
            httpc.setReadTimeout(60*1000);
            if(httpc.getResponseCode() == 200){
                InputStream in = httpc.getInputStream();
                //BitmapFactory->不同方式读取图片进入程序中
                bitmap = BitmapFactory.decodeStream(in);
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return bitmap;
    }

}

最后附上实现的效果

demo源码

你可能感兴趣的:(网络,图片,异步)