美文网首页
JSON解析(给个网址求网站东西)

JSON解析(给个网址求网站东西)

作者: 谜之龙 | 来源:发表于2017-05-17 15:43 被阅读0次

public class InIntent {

private static List<IntentEntiy> data;
private static ByteArrayOutputStream bos;

public static List<IntentEntiy> getIntent(String web,int length){
    data = new ArrayList<>();
    try {
        URL url=new URL(web);
        HttpURLConnection connection= (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(3000);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("accept","*/*");
        connection.setRequestProperty("connection","Keep-Alive");
        connection.setRequestProperty("Aaccept-Charset","utf-8");
        int code = connection.getResponseCode();
        if (code==200){
            BufferedReader output=new BufferedReader(new InputStreamReader
                    (connection.getInputStream(),"UTF-8"));
            String s = output.readLine();
            JSONObject object= new JSONObject(s);
            JSONArray array=object.getJSONArray("list");
            for(int i=0;i<length;i++){
                JSONObject sub=array.getJSONObject(i);
                String title= sub.getString("title");
                String time= sub.getString("time");
                String docurl= sub.getString("docurl");
                String img= sub.getString("imgurl");
                Bitmap bitmap=getImg(img);
                IntentEntiy listBean=new IntentEntiy(title,time,docurl,bitmap,img);
                data.add(listBean);
            }
        }else {
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

  return data;
}
 //这里是根据图片的网站求图片
public static Bitmap getImg(String web){
    try {
        URL url=new URL(web);
        HttpURLConnection connection= (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(3000);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("accept","*/*");
        connection.setRequestProperty("connection","Keep-Alive");
        connection.setRequestProperty("Aaccept-Charset","utf-8");
        int code = connection.getResponseCode();
        if (code==200){
            InputStream input=connection.getInputStream();
            byte[] buffer=new byte[1024];
            int len=0;
            bos = new ByteArrayOutputStream();
            while ((len=input.read(buffer))!=-1){
              bos.write(buffer,0,len);
            }

        }else {
        }
    } catch (Exception e) {
        e.printStackTrace();

    }finally {
        try {
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    byte[] data=bos.toByteArray();
    Bitmap bitmap= BitmapFactory.decodeByteArray(data,0,data.length);
    return bitmap;
}

}

相关文章

网友评论

      本文标题:JSON解析(给个网址求网站东西)

      本文链接:https://www.haomeiwen.com/subject/ojqtxxtx.html