HttpURLConnection, Android访问网络,实用demo(1)



    class MyAsyncTask extends AsyncTask<String,Void,String> {



        @Override

        protected String doInBackground(String... strings) {

//            http://61.145.196.120:8080/wangbaApp/appUnitRegister?renCode=114419010199

            String url = "http://61.145.196.120:8080/wangbaApp/appUnitRegister";

            Log.i("lgq","ssshhh==onPostExecute====tttt==="+url);

            List<Pair<String, String>> pairs = new ArrayList<Pair<String, String>>();

            pairs.add(new Pair<String, String>(PARAMETER_KEY_REN_CODE, strings[0]));

            String result = doPost(url, pairs);

            return result;

        }



        @Override

        protected void onPostExecute(String ss) {

            super.onPostExecute(ss);

            //隐藏progressBar

            try {

                JSONObject object = new JSONObject(ss);

//                    Log.i("lgq", "re==logtest=====" + s);

            } catch (JSONException e) {

                e.printStackTrace();

            }

        }

    }

2、工具方法:



    public static String doPost(String url, List<Pair<String, String>> params) {

        return doRequest(url, params, HTTP_METHOD_POST);

    }



    private static String doRequest(String url, List<Pair<String, String>> params, String type){

//        MLog.d(TAG, "request url : " + url + " method is : " + type);

        InputStream in = null;

        try {



            HttpURLConnection conn = getHttpURLConnection(url);

            conn.setRequestMethod(type);



            if(params != null && !params.isEmpty()){

                setParams(conn, params);

            }



            conn.connect();



//            MLog.d(TAG,"ResponseCode : " + conn.getResponseCode());

            if(HttpURLConnection.HTTP_OK == conn.getResponseCode() ){

                in = conn.getInputStream();

                return readStream(in);

            }else {

                return "网络连接失败"+conn.getURL();

//                throw new NetWorkException("ResponseCode : " + conn.getResponseCode());

            }



        }  catch (IOException e) {

            e.printStackTrace();

//            MLog.w(TAG, e.getMessage(), e);

            return "网络连接失败222";

//            throw new NetWorkException("net work fail: " + e);

        }finally {

            if(in != null){

                try {

                    in.close();

                } catch (IOException e) {

//                    MLog.w(TAG, "io close fail", e);

                }

            }

        }



    }



    private static String readStream(InputStream in) throws IOException {

        char[] buffer = new char[1024];



        BufferedReader reader = new BufferedReader(new InputStreamReader(in, CHARSET));



        int len = 0;

        StringBuilder strBuilder= new StringBuilder();

        while ((len = reader.read(buffer)) != -1){



            strBuilder.append(buffer, 0, len);

        }

        return strBuilder.toString();

    }



    private static HttpURLConnection getHttpURLConnection(String url) throws IOException {

        URL mUrl = new URL(url);



        HttpURLConnection conn = (HttpURLConnection) mUrl.openConnection();

        conn.setReadTimeout(10000);

        conn.setConnectTimeout(15000);



        conn.setDoInput(true);

        conn.setDoOutput(true);



        return conn;

    }



    private static void setParams(HttpURLConnection conn, List<Pair<String, String>> params) throws IOException {



        BufferedWriter writer = null;

        try {



            writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), CHARSET));



            StringBuilder result = new StringBuilder();

            boolean first = true;

            for (Pair pair : params)

            {

                if (first)

                    first = false;

                else

                    result.append("&");

                result.append(URLEncoder.encode(pair.first.toString(), CHARSET));

                result.append("=");

                result.append(URLEncoder.encode(pair.second.toString(), CHARSET));

            }



//            MLog.d(TAG,"URL:"+result.toString());

            writer.write(result.toString());

            writer.flush();



            Log.i("Lgq","......"+result.toString());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值