HttpUrlConnection Post请求

  1 package com.example.administrator.myapplication.activity;
  2 
  3 import android.os.Bundle;
  4 import android.os.Handler;
  5 import android.os.Message;
  6 import android.support.v7.app.AppCompatActivity;
  7 import android.view.View;
  8 import android.webkit.WebView;
  9 import android.widget.Button;
 10 import android.widget.Toast;
 11 
 12 import com.example.administrator.myapplication.R;
 13 
 14 import java.io.IOException;
 15 import java.io.InputStream;
 16 import java.io.OutputStream;
 17 import java.net.HttpURLConnection;
 18 import java.net.MalformedURLException;
 19 import java.net.URL;
 20 import java.nio.charset.Charset;
 21 
 22 public class HttpUrlConnectionPostActivity extends AppCompatActivity {
 23     WebView webView;
 24     Button postBtn;
 25     @Override
 26     protected void onCreate(Bundle savedInstanceState) {
 27         super.onCreate(savedInstanceState);
 28         setContentView(R.layout.activity_http_url_connection_post);
 29         webView = (WebView) findViewById(R.id.webView);
 30         postBtn = (Button) findViewById(R.id.post);
 31         postBtn.setOnClickListener(new View.OnClickListener() {
 32             @Override
 33             public void onClick(View v) {
 34                 new Thread(new Runnable() {
 35                     @Override
 36                     public void run() {
 37                         HttpUrlConnectionPost();
 38                     }
 39                 }).start();
 40             }
 41         });
 42 
 43     }
 44 
 45     private void HttpUrlConnectionPost(){
 46         try {
 47             URL url = new URL("https://www.baidu.com/");
 48             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
 49             connection.setConnectTimeout(5*1000);
 50             connection.setReadTimeout(5*1000);
 51             //设置是否向httpURLConnection写入内容
 52             //post请求必须设置为true,因为post请求参数是否写在http正文中
 53             connection.setDoOutput(true);
 54             //设置是否从HttpURLConnection读入内容,默认为true
 55             connection.setDoInput(true);
 56             //设置是否使用缓存,post请求不使用缓存
 57             connection.setUseCaches(false);
 58             //设置请求方法  注意大小写!
 59             connection.setRequestMethod("POST");
 60             //设置长连接
 61             //connection.setRequestProperty("Connection","keep-Alive");
 62             //设置字符集
 63             connection.setRequestProperty("Charset","utf-8");
 64             //connection.setRequestProperty("Content-type","application/x-www-");
 65 
 66             //!!!重点部分,设置参数
 67             String params = "page = 1 & num = 10";
 68             OutputStream os = connection.getOutputStream();
 69             os.write(params.getBytes());
 70             os.flush();
 71             os.close();
 72             if (connection.getResponseCode() == HttpURLConnection.HTTP_OK){
 73                 InputStream is = connection.getInputStream();
 74                 StringBuilder sb = new StringBuilder();
 75                 byte[] bytes = new byte[1024];
 76                 int i = 0;
 77                 while ((i = is.read(bytes)) != -1){
 78                     sb.append(new String(bytes,0,i,"utf-8"));
 79                 }
 80                 is.close();
 81 
 82                 Message message = handler.obtainMessage(1,sb.toString());
 83                 handler.sendMessage(message);
 84             }
 85         } catch (MalformedURLException e) {
 86             e.printStackTrace();
 87         } catch (IOException e) {
 88             e.printStackTrace();
 89         }
 90     }
 91     Handler handler = new Handler(){
 92         @Override
 93         public void handleMessage(Message msg) {
 94             super.handleMessage(msg);
 95             if (msg != null && msg.what == 1){
 96                 String s = (String) msg.obj;
 97                 String data = new String(s.getBytes(), Charset.forName("utf-8"));
 98                 webView.getSettings().setDefaultTextEncodingName("utf-8");
 99                 webView.getSettings().setJavaScriptEnabled(true);
100                 webView.loadDataWithBaseURL(null,data,"text/html","utf-8",null);
101                 Toast.makeText(getApplication(),"post请求成功!",Toast.LENGTH_SHORT).show();
102             }
103         }
104     };
105 }
 1  <WebView
 2         android:id="@+id/webView"
 3         android:layout_width="match_parent"
 4         android:layout_height="400dp"></WebView>
 5 
 6     <Button
 7         android:id="@+id/post"
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:text="HttpUrlConnection Post请求" />

 

posted @ 2016-09-20 17:49  me丶不是好人  阅读(5344)  评论(0编辑  收藏  举报