zoukankan      html  css  js  c++  java
  • JDK方式

    layout文件

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.hanqi.testapp3.TestActivity3"
    11     android:orientation="vertical">
    12 
    13     <LinearLayout
    14         android:layout_width="match_parent"
    15         android:layout_height="wrap_content">
    16     <Button
    17         android:layout_width="0dp"
    18         android:layout_height="wrap_content"
    19         android:text="JDK-Get方式"
    20         android:onClick="bt1_OnClick"
    21         android:layout_weight="1"/>
    22 
    23         <Button
    24             android:layout_width="0dp"
    25             android:layout_height="wrap_content"
    26             android:text="JDK-Post方式"
    27             android:onClick="bt2_OnClick"
    28             android:layout_weight="1"/>
    29     </LinearLayout>
    30 
    31     <EditText
    32         android:layout_width="match_parent"
    33         android:layout_height="200dp"
    34         android:id="@+id/et_2"/>
    35 
    36 </LinearLayout>
    layout

    java代码

      1 package com.hanqi.testapp3;
      2 
      3 import android.app.ProgressDialog;
      4 import android.os.Bundle;
      5 import android.support.v7.app.AppCompatActivity;
      6 import android.view.View;
      7 import android.widget.EditText;
      8 
      9 import java.io.InputStream;
     10 import java.io.OutputStream;
     11 import java.net.HttpURLConnection;
     12 import java.net.URL;
     13 
     14 public class TestActivity3 extends AppCompatActivity {
     15 
     16     EditText et_2;
     17 
     18     @Override
     19     protected void onCreate(Bundle savedInstanceState) {
     20         super.onCreate(savedInstanceState);
     21         setContentView(R.layout.activity_test3);
     22 
     23         et_2 = (EditText)findViewById(R.id.et_2);
     24     }
     25 
     26 
     27     //显示结果
     28     String str = "";
     29 
     30     //JDK的Get方式
     31     public void bt1_OnClick(View v)
     32     {
     33         //1、启动进度对话框
     34         final ProgressDialog pd = ProgressDialog.show(this, null, "请稍后...");
     35 
     36         //2、启动子线程,访问远程服务器
     37         new Thread()
     38         {
     39             @Override
     40             public void run() {
     41 
     42                 //访问远程服务器
     43                 //JDK Get
     44 
     45                 HttpURLConnection huc = null;
     46 
     47                 try {
     48                 //1、构造URL对象
     49                     URL url = new URL("http://192.168.0.105:81/index.asp?name=mike&password=456");
     50 
     51                 //2、得到 HttpURLConnection
     52                     huc = (HttpURLConnection)url.openConnection();
     53 
     54                 //3、设置 HttpURLConnection
     55                     huc.setRequestMethod("GET");
     56                     huc.setConnectTimeout(3000);
     57                     huc.setReadTimeout(3000);
     58 
     59                 //4、连接远程服务器
     60                     huc.connect();
     61 
     62                 //5、接收响应报文的状态码
     63                     int code = huc.getResponseCode();
     64 
     65                     str = "";
     66 
     67                 //6、判断响应状态码是否=200
     68                     if (code == 200)
     69                     {
     70                         //7、处理
     71                         //1 接收数据
     72 
     73                         //2 得到数据流
     74                         InputStream is = huc.getInputStream();
     75 
     76                         //读到的数据
     77                         byte[] b = new byte[1024];
     78 
     79                         //读到的数据长度
     80                         int i = 0;
     81 
     82                         while ((i = is.read(b)) > 0)
     83                         {
     84                             // 接收字符串
     85                             str += new String(b,0,i);
     86                         }
     87 
     88                         is.close();
     89                     }
     90                     else
     91                     {
     92                         str = "响应错误,错误码=" + code;
     93                     }
     94 
     95                     //显示结果,不能直接跨线程访问主线程的视图
     96                     runOnUiThread(new Runnable() {
     97                         @Override
     98                         public void run() {
     99                             et_2.setText(str);
    100 
    101                         }
    102                     });
    103 
    104                 }
    105                 catch (Exception e)
    106                 {
    107                     e.printStackTrace();
    108                 }
    109                 finally {
    110 
    111                     //8、关闭连接和进度对话框
    112                     // 释放资源
    113                     if (huc != null)
    114                     {
    115                         huc.disconnect();
    116                     }
    117 
    118                     // 支持跨线程访问
    119                     pd.dismiss();
    120 
    121                 }
    122             }
    123         }.start();
    124     }
    125 
    126 
    127     //JDK的Post方式
    128     public void bt2_OnClick(View v)
    129     {
    130         //1、启动进度对话框
    131         final ProgressDialog pd = ProgressDialog.show(this, null, "请稍后...");
    132 
    133         //2、启动子线程,访问远程服务器
    134         new Thread()
    135         {
    136             @Override
    137             public void run() {
    138 
    139                 //访问远程服务器
    140                 //JDK Post
    141 
    142                 HttpURLConnection huc = null;
    143 
    144                 try {
    145                     //1、构造URL对象
    146                     URL url = new URL("http://192.168.0.105:81/index.asp");
    147 
    148                     //2、得到 HttpURLConnection
    149                     huc = (HttpURLConnection)url.openConnection();
    150 
    151                     //3、设置 HttpURLConnection
    152                     huc.setRequestMethod("POST");
    153                     huc.setConnectTimeout(3000);
    154                     huc.setReadTimeout(3000);
    155 
    156                     //4、连接远程服务器,输出流
    157                     huc.connect();
    158 
    159                     //数据放到请求体里
    160                     // 1)得到输出流
    161                     OutputStream os = huc.getOutputStream();
    162 
    163                     String outstr = "name=tom&password=123";
    164 
    165                     os.write(outstr.getBytes("UTF-8"));
    166 
    167                     os.close();
    168 
    169                     //5、接收响应报文的状态码
    170                     int code = huc.getResponseCode();
    171 
    172                     str = "";
    173 
    174                     //6、判断响应状态码是否=200
    175                     if (code == 200)
    176                     {
    177                         //7、处理
    178                         //1 接收数据
    179 
    180                         //2 得到数据流,输入流
    181                         InputStream is = huc.getInputStream();
    182 
    183                         //读到的数据
    184                         byte[] b = new byte[1024];
    185 
    186                         //读到的数据长度
    187                         int i = 0;
    188 
    189                         while ((i = is.read(b)) > 0)
    190                         {
    191                             // 接收字符串
    192                             str += new String(b,0,i);
    193                         }
    194 
    195                         is.close();
    196                     }
    197                     else
    198                     {
    199                         str = "响应错误,错误码=" + code;
    200                     }
    201 
    202                     //显示结果,不能直接跨线程访问主线程的视图
    203                     runOnUiThread(new Runnable() {
    204                         @Override
    205                         public void run() {
    206                             et_2.setText(str);
    207 
    208                         }
    209                     });
    210 
    211                 }
    212                 catch (Exception e)
    213                 {
    214                     e.printStackTrace();
    215                 }
    216                 finally {
    217 
    218                     //8、关闭连接和进度对话框
    219                     // 释放资源
    220                     if (huc != null)
    221                     {
    222                         huc.disconnect();
    223                     }
    224 
    225                     // 支持跨线程访问
    226                     pd.dismiss();
    227 
    228                 }
    229             }
    230         }.start();
    231     }
    232 }
    Java代码

    index.asp

     1 <%
     2 name_get = request.querystring("name")
     3 password_get = request.querystring("password")
     4 
     5 name_post = request.form("name")
     6 password_post = request.form("password")
     7 
     8 %>
     9 request:<br>
    10 
    11 name_1=<%=name_get%>
    12 password_1=<%=password_get%>
    13 
    14 name_2=<%=name_post%>
    15 password_2=<%=password_post%>
    index.asp
  • 相关阅读:
    二级JAVA考证笔记
    JAVA异常处理机制
    notepad++安装nppFTP
    nginx conf_ctx ****
    ngx_string()错误分析
    nginx
    char *p[] 和char**的思考
    LeetCode.接雨水
    LeetCode.atoi
    LeetCode.数字转罗马数字
  • 原文地址:https://www.cnblogs.com/future-zhenzhen/p/5592897.html
Copyright © 2011-2022 走看看