1 package com.hanqi.cunchu; 2 3 import android.app.ProgressDialog; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.util.Log; 7 import android.view.View; 8 import android.widget.EditText; 9 10 import java.io.InputStream; 11 import java.io.ObjectStreamClass; 12 import java.io.OutputStream; 13 import java.net.URL; 14 15 import javax.net.ssl.HttpsURLConnection; 16 17 public class TestActivity3 extends AppCompatActivity { 18 EditText et_2; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_test3); 24 et_2=(EditText)findViewById(R.id.et_2); 25 } 26 //显示结果 27 String STR = ""; 28 //jdk的get方式 29 public void bt1_onclick(View v) { 30 //1-启动进度对话框 31 final ProgressDialog PD = ProgressDialog.show(this, null, "请稍等. . . "); 32 //2-启动子线程,访问远程服务器 33 new Thread() { 34 @Override 35 public void run() { 36 //访问远程服务器 37 //JDK-get 38 HttpsURLConnection HUC = null; 39 try { 40 //1-构造URL对象 41 URL url = new URL("http://192.168.31.56:81/index.asp?name=tom&password=456"); 42 //2-得到HttpURLConnection 43 HUC = (HttpsURLConnection) url.openConnection(); 44 //3-设置HttpURLConnection 45 HUC.setRequestMethod("GET"); 46 HUC.setConnectTimeout(3000); 47 HUC.setReadTimeout(3000); 48 //4-连接远程服务器 49 HUC.connect(); 50 //5-接收响应报文的状态 51 int code = HUC.getResponseCode(); 52 //6-判断响应状态码,是否等于200 53 if (code == 200) { 54 //7-处理 55 56 //1-接收数据 57 58 //2-得到数据流 59 InputStream is = HUC.getInputStream(); 60 //读到的数据 61 byte[] b = new byte[1024]; 62 //读到的数据长度 63 int i = 0; 64 while ((i = is.read(b)) > 0) { 65 //接收字符串 66 STR += new String(b, 0, i); 67 } 68 is.close(); 69 70 } else { 71 STR = "响应错误,错误码=" + code; 72 } 73 //显示结果,不能直接跨线程访问主线程的视图 74 runOnUiThread(new Runnable() { 75 @Override 76 public void run() { 77 et_2.setText(STR); 78 } 79 }); 80 } catch (Exception e) { 81 e.printStackTrace(); 82 } finally { 83 //8-关闭连接和进度对话框 84 //释放资源 85 if (HUC != null) { 86 HUC.disconnect(); 87 } 88 //支持跨线程访问 89 PD.dismiss(); 90 } 91 } 92 }.start(); 93 } 94 //jdk的POST方式 95 public void bt2_onclick(View v) 96 { 97 //1-启动进度对话框 98 final ProgressDialog PD=ProgressDialog.show(this, null, "请稍等. . . "); 99 //2-启动子线程,访问远程服务器 100 new Thread() 101 { 102 @Override 103 public void run() { 104 //访问远程服务器 105 //JDK-get 106 HttpsURLConnection HUC = null; 107 try { 108 //1-构造URL对象 109 URL url = new URL("http://192.168.31.56:81/index.asp"); 110 //2-得到HttpURLConnection 111 HUC = (HttpsURLConnection)url.openConnection(); 112 //3-设置HttpURLConnection 113 HUC.setRequestMethod("POST"); 114 HUC.setConnectTimeout(3000); 115 HUC.setReadTimeout(3000); 116 //4-连接远程服务器,输出流 117 HUC.connect(); 118 //数据放到请求体里面 119 //1-得到输出流 120 OutputStream os=HUC.getOutputStream(); 121 String outStr="name=tom&password=123"; 122 os.write(outStr.getBytes("UTF-8")); 123 Log.e("TAG","发送. . ."); 124 //5-接收响应报文的状态 125 int code = HUC.getResponseCode(); 126 //6-判断响应状态码,是否等于200 127 if (code==200) 128 { 129 //7-处理 130 131 //1-接收数据 132 133 //2-得到数据流,输入流 134 InputStream is=HUC.getInputStream(); 135 //读到的数据 136 byte[]b=new byte[1024]; 137 //读到的数据长度 138 int i = 0; 139 while((i=is.read(b))>0) 140 { 141 //接收字符串 142 STR+=new String(b,0,i); 143 } 144 is.close(); 145 os.close(); 146 } 147 else 148 { 149 STR="响应错误,错误码="+code; 150 } 151 //显示结果,不能直接跨线程访问主线程的视图 152 runOnUiThread(new Runnable() { 153 @Override 154 public void run() { 155 et_2.setText(STR); 156 } 157 }); 158 } catch (Exception e) { 159 e.printStackTrace(); 160 } 161 finally { 162 //8-关闭连接和进度对话框 163 //释放资源 164 if (HUC!=null) { 165 HUC.disconnect(); 166 } 167 //支持跨线程访问 168 PD.dismiss(); 169 } 170 } 171 }.start(); 172 } 173 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context="com.hanqi.cunchu.TestActivity3"> 9 <LinearLayout 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:orientation="horizontal"> 13 <Button 14 android:layout_width="0dp" 15 android:layout_weight="1" 16 android:layout_height="wrap_content" 17 android:text="JDK-GET方式" 18 android:onClick="bt1_onclick"/> 19 <Button 20 android:layout_width="0dp" 21 android:layout_weight="1" 22 android:layout_height="wrap_content" 23 android:text="JDK-POST方式" 24 android:onClick="bt2_onclick"/> 25 </LinearLayout> 26 <EditText 27 android:layout_width="match_parent" 28 android:layout_height="200dp" 29 android:id="@+id/et_2"/> 30 </LinearLayout>