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();
        
    }
    
}

你可能感兴趣的:(Android Studio访问Http资源之HttpURLConnection的案例(POST方式))