Android Studio访问Http资源之HttpURLConnection的案例(POST方式)

Http请求由三部分组成,分别是:请求头,消息报头,请求正文。
Http响应也由三部分组成,分别是:状态行,消息报头,响应正文。

  默认已经有服务器(自己本地搭建的或远程的),没有的可以拿个免费接口先来用用,随便一搜就有的。这里使用的接口是
http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=手机号

  xml文件就不放了,只有Button。

public class MainActivity extends AppCompatActivity {

    private Button btn2;

    private String urlAddressFront = "http://tcc.taobao.com";
    private String urlAddressMethod = "/cc/json/mobile_tel_segment.htm";

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

        btn2 = (Button) findViewById(R.id.btn_post);

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doPost("168********");       //手机号
            }
        });

    }

    //doPost
    private void doPost(final String s) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(urlAddressFront + urlAddressMethod);       //这里是服务器地址,不是完整的url地址
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setDoInput(true);         //打开httpURLConnection的输入流
                    httpURLConnection.setDoOutput(true);        //打开httpURLConnection的输出流
                    httpURLConnection.setRequestMethod("POST");         //设置请求方法
                    httpURLConnection.setUseCaches(false);      //设置不使用缓存,POST请求使用缓存可能会出现一些异常
                    httpURLConnection.setRequestProperty("Accept-Charset", "UTF-8");        //设置请求头
                    httpURLConnection.setRequestProperty("Content-Type", "application/x-form-urlencoded");      //设置请求头
                    httpURLConnection.connect();        //连接

                    //把我们的请求参数通过输出流的方式写给服务器
                    DataOutputStream outputStream = new DataOutputStream(httpURLConnection.getOutputStream());
                    String content = "?tel=" + s;     //要给服务器写的参数
                    outputStream.writeBytes(content);       //以字节的形式写出去
                    outputStream.flush();       //刷新
                    outputStream.close();       //关闭流

                    //服务器响应,与doGet相同
                    if (httpURLConnection.getResponseCode() == 200) {        //服务器响应码
                        InputStream inputStream = httpURLConnection.getInputStream();       //输入流
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));     //BufferedReader构造方法
                        StringBuffer stringBuffer = new StringBuffer();     //用来存储读的结果
                        String readLine = "";       //存储每行的结果
                        while ((readLine = bufferedReader.readLine()) != null) {     //按行读取,每行读到的字符串放到readLine中
                            stringBuffer.append(readLine);
                            inputStream.close();
                            bufferedReader.close();
                            httpURLConnection.disconnect();
                            Log.d("TAGPOST", stringBuffer.toString());
                        }
                    } else {
                        Log.d("TAGPOST", "failed");
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        
    }
    
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kpacnB_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值