HttpURLConnection网络请求的封装

public class Helper {
private final int HTTP_SUCCESS=100;//成功
private final int HTTP_FAIL=101;//失败

public Helper(){}


//get请求
public Helper get(String url){
    doHttp(0,url,"");
    return this;
}

//网络请求
private void doHttp(final int type, final String url, final String string){
    new Thread(){
        @Override
        public void run() {
            super.run();
            try {
                URL mUrl=new URL(url);
                HttpURLConnection connection= (HttpURLConnection) mUrl.openConnection();
                String method="GET";
                if(type==1){
                    method="POST";
                }
                connection.setRequestMethod(method);//设置请求方法
                connection.setConnectTimeout(5000);//设置请求超时
                if(type==1){
                    //post请求添加参数
                    PrintWriter writer=new PrintWriter(connection.getOutputStream());
                    writer.write(string);
                    writer.flush();
                    writer.close();
                }


                connection.connect();
                int code=connection.getResponseCode();
                Message msg=Message.obtain();
                if(code==HttpURLConnection.HTTP_OK){
                    InputStream inputStream=connection.getInputStream();
                    String data=convertStream2String(inputStream);
                    msg.obj=data;
                    msg.what=HTTP_SUCCESS;
                }else{
                    msg.what=HTTP_FAIL;
                }

                handler.sendMessage(msg);

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.start();
}

private Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

        switch (msg.what){
            case HTTP_SUCCESS://成功
               String data=(String) msg.obj;
                listener.success(data);
                break;
            case HTTP_FAIL://失败
                listener.fail();
                break;
        }
    }
};

//post请求
public Helper post(String url,String string){
    doHttp(1,url,string);
    return this;
}


//流转字符串
private   String convertStream2String(InputStream input){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();// 自带缓存的输出流
    int len=-1;
    byte [] buffer = new byte[512];
    try {
        while((len = input.read(buffer))!=-1){
            baos.write(buffer, 0, len); // 将读到的字节,写入baos

        }
        return new String(baos.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}


private HttpListener listener;
//传递接口
public void result(HttpListener listener){
    this.listener=listener;
}

public interface HttpListener{
void success(String data);//成功
void fail();//失败
}

}

猜你喜欢

转载自blog.csdn.net/zhao_15121/article/details/82667578