深入解析Android网络请求工具类:NetworkUtil

本文将带您深入了解一个实用的Android网络请求工具类——NetworkUtil。该工具类封装了基于HttpsURLConnection的网络请求逻辑,并结合Handler机制实现异步回调,方便开发者进行网络操作。

一、代码解析javapackage com.cainiu.qcgjb.wxapi;

import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;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 javax.net.ssl.HttpsURLConnection;

public class NetworkUtil { class HttpsThread extends Thread { private Handler handler; private String httpsUrl; private int msgTag;

    public HttpsThread(Handler arg1, String arg2, int arg3) {            super();            this.handler = arg1;            this.httpsUrl = arg2;            this.msgTag = arg3;        }

    private static byte[] httpURLConnectionGet(String arg2) throws Exception {            URLConnection v2 = new URL(arg2).openConnection();            if(v2 == null) {                Log.i(NetworkUtil.TAG, 'open connection failed.');            }

        if(((HttpURLConnection)v2).getResponseCode() >= 300) {                ((HttpURLConnection)v2).disconnect();                Log.w(NetworkUtil.TAG, 'dz[httpURLConnectionGet 300]');                return null;            }

        byte[] v0 = HttpsThread.readStream(((HttpURLConnection)v2).getInputStream());            ((HttpURLConnection)v2).disconnect();            return v0;        }

    private static byte[] readStream(InputStream arg4) throws IOException {            byte[] v0 = new byte[0x400];            ByteArrayOutputStream v1 = new ByteArrayOutputStream();            while(true) {                int v2 = arg4.read(v0);                if(v2 == -1) {                    break;                }

            v1.write(v0, 0, v2);            }

        v0 = v1.toByteArray();            v1.close();            arg4.close();            return v0;        }

    public void run() {            Bundle v2;            Message v1;            if(this.msgTag == 5) {                try {                    byte[] v0_1 = HttpsThread.httpURLConnectionGet(this.httpsUrl);                    v1 = Message.obtain();                    v1.what = this.msgTag;                    v2 = new Bundle();                    v2.putByteArray('imgdata', v0_1);                    v1.setData(v2);                    this.handler.sendMessage(v1);                }                catch(Exception v0) {                    Log.e(NetworkUtil.TAG, v0.getMessage());                }

            return;            }

        try {                URLConnection v0_2 = new URL(this.httpsUrl).openConnection();                ((HttpsURLConnection)v0_2).setAllowUserInteraction(false);                ((HttpsURLConnection)v0_2).setInstanceFollowRedirects(true);                ((HttpsURLConnection)v0_2).setRequestMethod('GET');                ((HttpsURLConnection)v0_2).connect();                if(((HttpsURLConnection)v0_2).getResponseCode() != 200) {                    return;                }

            InputStream v0_3 = ((HttpsURLConnection)v0_2).getInputStream();                BufferedReader v1_1 = new BufferedReader(new InputStreamReader(v0_3, 'iso-8859-1'), 8);                StringBuilder v2_1 = new StringBuilder();                while(true) {                    String v3 = v1_1.readLine();                    if(v3 == null) {                        break;                    }

                v2_1.append(v3).append('

'); }

            v0_3.close();                String v0_4 = v2_1.toString();                Log.i(NetworkUtil.TAG, v0_4);                v1 = Message.obtain();                v1.what = this.msgTag;                v2 = new Bundle();                v2.putString('result', v0_4);                v1.setData(v2);                this.handler.sendMessage(v1);            }            catch(Exception v0) {                Log.e(NetworkUtil.TAG, v0.getMessage());            }        }    }

public static final int CHECK_TOKEN = 2;    public static final int GET_IMG = 5;    public static final int GET_INFO = 4;    public static final int GET_TOKEN = 1;    public static final int REFRESH_TOKEN = 3;    private static String TAG = 'MicroMsg.NetworkUtil';

static {    }

public NetworkUtil() {        super();    }

static String access$000() {        return NetworkUtil.TAG;    }

public static void getImage(Handler arg1, String arg2, int arg3) {        new HttpsThread(arg1, arg2, arg3).start();    }

public static void sendWxAPI(Handler arg1, String arg2, int arg3) {        new HttpsThread(arg1, arg2, arg3).start();
Android网络请求工具类:深入解析NetworkUtil

原文地址: http://www.cveoy.top/t/topic/ci2e 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录