android的httpUtils

一个android的httpUtils类

package com.kblw.client04.http;

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

/**用于json解析的工具类 获取服务端返回数据 并且转为字符串
 * Created by KBLW on 2016/5/28.
 */
public class HttpUtils {
    public  static String getJsonStringContent(String url_path){
    try{
        URL url = new URL(url_path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(3000);
        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        int code = connection.getResponseCode();
        if (code==200){
        return changeInputStream(connection.getInputStream());
        }
    }catch (Exception e) {
        e.printStackTrace();
        return "";
    }
        return "";
    }

    private static String changeInputStream(InputStream inputStream) {
        String jsonString = "";
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int len = 0;
        byte[] data = new byte[1024];
        try {
            while ((len=inputStream.read(data))!=-1){
                outputStream.write(data,0,len);
            } jsonString = new String(outputStream.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonString;
    }

}

先放这 后面再弄

posted @ 2016-07-08 13:07  KBLW  阅读(760)  评论(0编辑  收藏  举报