安卓-HttpUtil

package com.example.myapplication.utils;

import android.os.Handler;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;


public class HttpUtil {
    private static Handler handler=new Handler();
    public static void get(final String strUrl, final Map strMap, final HttpCallBack callBack){
       new Thread(){
            @Override
            public void run() {
                HttpURLConnection connection=null;
                InputStream is=null;
                try {
                    StringBuilder stringBuffer=new StringBuilder(strUrl);

                    if(strMap !=null){
                        stringBuffer.append("?");
                        for (Object key:strMap.keySet()) {
                            stringBuffer.append(key+"="+strMap.get(key)+"&");
                        }
                        stringBuffer.deleteCharAt(stringBuffer.length()-1);
                    }
                    URL url=new URL(stringBuffer.toString());
                    connection= (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(10*1000);
                    if (connection.getResponseCode()==HttpURLConnection.HTTP_OK){
                        is=connection.getInputStream();
                        final String result=InputStreamToString(is);
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                callBack.onSuccess(result);
                            }
                        });

                    }else{
                        throw new Exception("ResponseCode:"+connection.getResponseCode());
                    }
                } catch (final Exception e) {
                    e.printStackTrace();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callBack.onError(e);
                        }
                    });

                }finally {
                    if (connection!=null)connection.disconnect();
                    if (is!=null) try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callBack.onFinish();
                        }
                    });
                }
            }
        }.start();
    }
    public static void post( final String strUrl,final Map strMap, final HttpCallBack callBack){
        Thread thread=new Thread(){
            @Override
            public void run() {
                HttpURLConnection connection=null;
                OutputStream os=null;
                InputStream is=null;
                try {
                    StringBuilder stringBuilder=new StringBuilder();
                    for (Object key:strMap.keySet()){
                        stringBuilder.append(key+"="+strMap.get(key)+"&");
                    }
                    stringBuilder.deleteCharAt(stringBuilder.length()-1);
                    URL url=new URL(strUrl);
                    connection= (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("POST");
                    connection.setConnectTimeout(10*1000);
                    connection.setDoOutput(true);
                    connection.setDoInput(true);
                    connection.setUseCaches(false);
                    connection.setRequestProperty("Charset","utf-8");
                    connection.connect();
                    os=connection.getOutputStream();
                    os.write(stringBuilder.toString().getBytes());
                    os.flush();
                    if (connection.getResponseCode()==HttpURLConnection.HTTP_OK){
                        is=connection.getInputStream();
                        final String result=InputStreamToString(is);
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                callBack.onSuccess(result);
                            }
                        });
                    }else{
                        throw new Exception("ResponseCode:"+connection.getResponseCode());
                    }
                }catch (final Exception e){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callBack.onError(e);
                        }
                    });
                }finally {
                    if (connection!=null)connection.disconnect();
                    try {
                        if (is!=null) is.close();
                        if(os!=null)os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callBack.onFinish();
                        }
                    });
                }
            }
        };
        thread.start();
    }
    public static String InputStreamToString(InputStream is) throws IOException {
        ByteArrayOutputStream os=new ByteArrayOutputStream();
        byte[] data=new byte[1024];
        int len=-1;
        while((len=is.read(data))!=-1){
            os.write(data,0,len);
        }
        os.flush();
        os.close();
        is.close();
        byte[] lens = os.toByteArray();
        String result=new String(lens,"UTF-8");
        return result;
    }
    public interface HttpCallBack{
        public void onSuccess(String result);
        public void onError(Exception e);
        public void onFinish();
    }
}


使用

    public  void  run(View view){

        HttpUtil.get("http://192.168.1.102:8888/listAll",null, new HttpUtil.HttpCallBack() {
            @Override
            public void onSuccess(String result) {
                Toast.makeText(MainActivity.this,"onSuccess",Toast.LENGTH_SHORT).show();
                Log.i("wccc",result);
            }
            @Override
            public void onError(Exception e) {
                Toast.makeText(MainActivity.this,"onError",Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onFinish() {
                Toast.makeText(MainActivity.this,"onFinish",Toast.LENGTH_SHORT).show();
            }
        });


    }

权限

    <uses-permission android:name="android.permission.INTERNET" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值