標籤:
get方式:
package com.itheima.getmethod;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLDecoder;import java.net.URLEncoder;import com.itheima.htmlviewer.utils.Utils;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { Toast.makeText(MainActivity.this, (String)msg.obj, 0).show(); } }; public void click(View v){ EditText et_name = (EditText) findViewById(R.id.et_name); EditText et_pass = (EditText) findViewById(R.id.et_pass); final String name = et_name.getText().toString(); final String pass = et_pass.getText().toString(); Thread t = new Thread(){ @Override public void run() { //提交的資料需要經過url編碼(utf-8編碼),英文和數字編碼後不變,如果是瀏覽器會自動編碼,用戶端要手動編碼 @SuppressWarnings("deprecation") String path = "http://192.168.13.13/Web2/servlet/LoginServlet?name=" + URLEncoder.encode(name) + "&pass=" + pass; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); if(conn.getResponseCode() == 200){ InputStream is =conn.getInputStream(); String text = Utils.getTextFromStream(is); Message msg = handler.obtainMessage(); msg.obj = text; handler.sendMessage(msg); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; t.start(); }}
package com.itheima.htmlviewer.utils;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;public class Utils { public static String getTextFromStream(InputStream is){ byte[] b = new byte[1024]; int len = 0; //建立位元組數組輸出資料流,讀取輸入資料流的文本資料時,同步把資料寫入數組輸出資料流 ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { while((len = is.read(b)) != -1){ bos.write(b, 0, len); } //把位元組數組輸出資料流裡的資料轉換成位元組數組 String text = new String(bos.toByteArray()); return text; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }}
windowsmore編碼是gbk,安卓預設編碼是utf-8.
post方式:
package com.itheima.getmethod;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLDecoder;import java.net.URLEncoder;import com.itheima.htmlviewer.utils.Utils;import com.itheima.postmethod.R;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { Toast.makeText(MainActivity.this, (String)msg.obj, 0).show(); } }; public void click(View v){ EditText et_name = (EditText) findViewById(R.id.et_name); EditText et_pass = (EditText) findViewById(R.id.et_pass); final String name = et_name.getText().toString(); final String pass = et_pass.getText().toString(); Thread t = new Thread(){ @Override public void run() { //提交的資料需要經過url編碼,英文和數字編碼後不變 @SuppressWarnings("deprecation") String path = "http://192.168.13.13/Web2/servlet/LoginServlet"; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); //拼接出要提交的資料的字串 String data = "name=" + URLEncoder.encode(name) + "&pass=" + pass; //添加post請求的兩行屬性 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length", data.length() + ""); //設定開啟輸出資料流 conn.setDoOutput(true); //拿到用戶端像服務端的輸出資料流 OutputStream os = conn.getOutputStream(); //使用輸出資料流往伺服器提交資料, os.write(data.getBytes());//這個時候還沒有產生網路互動資料 if(conn.getResponseCode() == 200){//建立串連,產生網路互動資料 InputStream is = conn.getInputStream();//得到伺服器返回的輸入資料流 String text = Utils.getTextFromStream(is); Message msg = handler.obtainMessage(); msg.obj = text; handler.sendMessage(msg); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; t.start(); }}
android 76 使用get post方式提交資料