字節流解析Json

package com.bwei.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;



public class NetWorkUtils {

    private static ByteArrayOutputStream baos;
   static String path="http://www.meirixue.com/api.php?c=index&a=index";
    public static String getjson(){

                try {
                    URL url = new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    int responseCode = connection.getResponseCode();

                    baos = new ByteArrayOutputStream();

                    if(responseCode==200){
                        InputStream inputStream = connection.getInputStream();
                        int len;
                        byte[] arr = new byte[1024];
                        while ((len=inputStream.read(arr))!=-1){
                            baos.write(arr,0,len);
                        }

                    }
                    return baos.toString();
                } catch (Exception e) {

                    e.printStackTrace();
                }

        return "";
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章