HttpURIConnection

package com.bawei.httpuriconnection;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private Button but_huc;
    private StringBuffer stringbuffer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initview();
        but_huc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //get请求
                        getservice();
                        //post请求
                        postservice();
                    }
                }).start();
            }
        });

    }
    //post连接第三方服务获取快递下单
    private void postservice() {
/**
 * 字节写入  writeBytes();   字符写入  writeChars();
 */
        try {
            URL url=new URL("http://v.juhe.cn/expressonline/getCarriers.php");
            HttpURLConnection connection= (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            //⑩.post的请求 设置支持输入输出流
            connection.setDoInput(true);
            connection.setDoOutput(true);

            connection.connect();
            //⑤.获取输出流   发送数据
            OutputStream outputStream =  connection.getOutputStream();
            //⑥转换outputStream-->为DataOutputStream
            DataOutputStream dataoutputstrean=new DataOutputStream(outputStream);
            //⑦.拿dataoutputstrean写数据
            dataoutputstrean.writeBytes("key=d68c53144e74286c6af7b8a358ddef43");
            //刷新 关闭
            dataoutputstrean.flush();
            dataoutputstrean.close();

            int code = connection.getResponseCode();
            if(code==HttpURLConnection.HTTP_OK)
            {
                InputStream inputStream = connection.getInputStream();
                BufferedReader  bufferedreader=new BufferedReader(new InputStreamReader(inputStream));

                String len=null;

                stringbuffer= new StringBuffer();
                while((len=bufferedreader.readLine())!=null)
                {
                    stringbuffer.append(len);
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, stringbuffer.toString(), Toast.LENGTH_SHORT).show();
                    }
                });
            }else{
                int errorcode = connection.getResponseCode();
                String errormethod = connection.getRequestMethod();
            }


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    //get连接第三方服务获取快递下单
    private void getservice() {
        //方法一  get方法
        try {
            //①获取URI传入服务地址
            URL url=new URL("http://v.juhe.cn/expressonline/getCarriers.php?key=d68c53144e74286c6af7b8a358ddef43");
            //②创建 HttpURLConnection连接对象
            HttpURLConnection connection= (HttpURLConnection) url.openConnection();
            //③设置请求方式  (get/post都可以)
            connection.setRequestMethod("GET");
            //④.建立连接(连接到第三方服务)
            connection.connect();
            //⑤.获取第三方响应码,根据响应码做相应的操作
            int code = connection.getResponseCode();//响应码
            //根据响应码作出判断    HTTP_OK-->成功
            if (code==HttpURLConnection.HTTP_OK) {
                //⑥.服务器相应成功返回的输入流   获取的数据读取来
                InputStream inputStream=connection.getInputStream();
                //⑦.BufferedReader按行读取  (用输入转换流进行转换)
                BufferedReader  bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                //⑧.读取数据
                String len=null;
                //⑨.如果字符串经常改变用    StringBuffer
                stringbuffer = new StringBuffer();
                 /* String s="";*/
                while ((len=bufferedReader.readLine())!=null)
                {
                      /* s+=len;*/
                    //把数据全部追加到一个String类型里
                    stringbuffer.append(len);
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, stringbuffer.toString(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
            //错误
            else{
                int errorcode = connection.getResponseCode();
                String errormessage = connection.getResponseMessage();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }




    }

    private void initview() {
        but_huc = (Button) findViewById(R.id.but_huc);
    }

}





处理网址有中文问题
 String name = getIntent().getStringExtra("name");
        setTitle(name+"列表");
        StringBuffer url=new StringBuffer();

        try {
               url.append("http://japi.juhe.cn/comic/book?type=
             ")
            .append(URLEncoder.encode(name,"UTF-8"))
            .append("&key=3e09796970b04508f16de756d36d4bb8");

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值