获取网络数据(Json串和图片)

来源:互联网 发布:淘宝开店如何找货源 编辑:程序博客网 时间:2024/06/07 05:16

权限

<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  • 1
  • 2
  • 3
  • 4

依赖

compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'compile 'com.google.code.gson:gson:2.2.4'compile 'com.jakewharton:butterknife:8.8.1'annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'implementation project(':xlistviewlibrary')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

工具类

import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.util.Log;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;/** * Created by liqiang on 2017/11/2. */public class Utils {    /**     * 获取手机网络状态     * -1 :无网络     * 0 :  蜂窝网络;     * 1  : wifi网络     *     * @param context 上下文对象     * @return     */    public static int getNetType(Context context) {        int mState = -1;// 代表无网络        //获取android系统提供的服务, 转换成链接管理类,这个类专门处理链接相关的东西        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);        //NetworkInfo封装了网络链接的信息        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();        if (activeNetworkInfo == null) {            return mState;        }        int type = activeNetworkInfo.getType();        if (type == ConnectivityManager.TYPE_WIFI) {//代表现在是wifi网络            mState = 1;        } else if (type == ConnectivityManager.TYPE_MOBILE) {            mState = 0;        }        return mState;    }    private static String tag = "TAG";    public static String getNetJson(String urlString) {        try {            //url对象封装接口字符串            URL url = new URL(urlString);            //用url打开连接, 返回值我们用HttpURLConnection            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();            urlConnection.setConnectTimeout(8000);//设置链接超时时间            int responseCode = urlConnection.getResponseCode(); //获取状态码            if (responseCode == 200) {                InputStream inputStream = urlConnection.getInputStream();                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));                //可拼接的字符串                StringBuilder stringBuilder = new StringBuilder();                String temp = "";                while ((temp = bufferedReader.readLine()) != null) {                    stringBuilder.append(temp);                }                String jsonString = stringBuilder.toString();                return jsonString;            } else {            }        } catch (MalformedURLException e) {            e.printStackTrace();            Log.e(tag, "getNetJson: " + e.toString());        } catch (IOException e) {            e.printStackTrace();            Log.e(tag, "getNetJson: " + e.toString());        }        return "";    }    /**     * 获取网络图片     *     * @param urlString     * @return     */    public static Bitmap getNetBitmap(String urlString) {        try {            URL url = new URL(urlString);            HttpURLConnection httpurlConnection = (HttpURLConnection) url.openConnection();            httpurlConnection.setConnectTimeout(8000);//设置链接超时时间            int responseCode = httpurlConnection.getResponseCode();            if (responseCode == 200) {                InputStream inputStream = httpurlConnection.getInputStream();                //BitmapFactory:  android提供好的加工图片的工具类,                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);                return bitmap;            } else {                //do nothing            }        } catch (MalformedURLException e) {            e.printStackTrace();            Log.e(tag, "getNetBitmap: " + e.toString());        } catch (IOException e) {            e.printStackTrace();            Log.e(tag, "getNetBitmap: " + e.toString());        }        return null;    }    public static String getNetClient(String urlString) {        //得到httpclient对象        DefaultHttpClient defaultHttpClient = new DefaultHttpClient();        //封装get请求        HttpGet httpGet = new HttpGet(urlString);        try {            //获取返回的response            HttpResponse response = defaultHttpClient.execute(httpGet);            //获取状态码            int statusCode = response.getStatusLine().getStatusCode();            if (statusCode == 200) {                HttpEntity entity = response.getEntity();                String json = EntityUtils.toString(entity);                Log.e("httpclient", "run:----- "+json );                return json;            }else {            }        } catch (IOException e) {            e.printStackTrace();        }        return "";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155