UTIL

MEIMEI_BK發表於2018-05-31
package com.bwei.month_lianxi3.httputil;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;


public class HttpUtils {
    private static final String TAG = "HttpUtils---";
    private MyHandler myHandler = new MyHandler();
    private static final int SUCCESS = 0;
    private static final int ERROR = 1;

    //單利
    private static HttpUtils httpUtils = new HttpUtils();
    private HttpUtilListener httpUtilsListener;

    private HttpUtils() {
    }//構造方法私有化

    public static HttpUtils getInstance() {
        if (httpUtils == null) {
            httpUtils = new HttpUtils();
        }
        return httpUtils;
    }

    //封裝get
    public void get(final String url) {
        new Thread() {
            @Override
            public void run() {
                try {
                    URL u = new URL(url);
                    HttpURLConnection connection = (HttpURLConnection) u.openConnection();
                    connection.setConnectTimeout(3000);
                    if (connection.getResponseCode() == 200) {
                        InputStream inputStream = connection.getInputStream();
                        String json = inputStream2String2(inputStream);
                        //傳送資料
                        Message message = myHandler.obtainMessage();
                        message.what = SUCCESS;
                        message.obj = json;
                        myHandler.sendMessage(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Message message = myHandler.obtainMessage();
                    message.what = ERROR;
                    message.obj = e.getMessage();
                    myHandler.sendMessage(message);
                }
            }
        }.start();
    }

    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SUCCESS:
                    //成功
                    String json = (String) msg.obj;
                    Log.d(TAG, "工具類資料: " + json);
                    httpUtilsListener.getSuccess(json);
                    break;
                case ERROR:
                    //失敗
                    String error = (String) msg.obj;
                    Log.d(TAG, "handleMessage: " + error);
                    httpUtilsListener.getError(error);
                    break;
            }
        }
    }

    //1.定義介面
    public interface HttpUtilListener {
        void getSuccess(String json);

        void getError(String error);
    }

    //2.外部訪問的方法
    public void setHttpUtilsListener(HttpUtilListener httpUtilsListener) {
        this.httpUtilsListener = httpUtilsListener;
    }



    //將流--字串  使用字元流--部分亂碼
    public String inputStream2String2(InputStream inputStream) {
        InputStreamReader reader = new InputStreamReader(inputStream);
        BufferedReader br = new BufferedReader(reader);
        StringBuffer sr = new StringBuffer();
        try {

            String s = null;
            while ((s = br.readLine()) != null) {
                sr.append(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sr.toString();
    }


}

app

package com.bwei.month_lianxi3.myapp;

import android.app.Application;
import android.graphics.Bitmap;
import android.os.Handler;

import com.bwei.month_lianxi3.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

public class MyApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        initImageLoader();

    }

    private void initImageLoader() {
        //建立預設的ImageLoader 配置引數
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);

        ImageLoader.getInstance().init(configuration);
    }


    public static DisplayImageOptions getOptions() {
        DisplayImageOptions Options = new DisplayImageOptions.Builder()

                .cacheInMemory(true) // default
                .cacheOnDisk(true) // default
                .bitmapConfig(Bitmap.Config.RGB_565) // default
                .displayer(new SimpleBitmapDisplayer()) // default
                .displayer(new FadeInBitmapDisplayer(500))
                .handler(new Handler()) // default
                .build();

        return Options;

    }
}

相關文章