HttpUrlConnection Post請求

來源:互聯網
上載者:User

標籤:

  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請求" />

 

HttpUrlConnection Post請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.