zoukankan      html  css  js  c++  java
  • 数据储存——远程服务器存储——JDK方式

    JDK方式

    API

    一.URL

       1.new  URL("http://网络资源路径")

       2.openConnection(),返回URLConnection对象

    二.HttpURLConnection

       1.setRequestMethod(“GET”/“POST”):设置请求方式

       2.setConnectTimeout(毫秒数):设置连接超时时间

       3.setReadTimeout(毫秒数):设置读超时时间

       4.connect( ):连接服务器,发送请求

       5.getOutputStream( ):得到连接输出流,通过输出流向服务器发送请求体

       6.getResponseCode( ):得到响应状态码,200代表正常响应

       7.getInputStream( ):得到连接输入流,通过输入流接收服务器发送的响应体

       8.disconnect( ):断开连接

     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:layout_weight="1"
    20             android:text="JDK-Post方式"
    21             android:onClick="bt2_OnClick"/>
    22     <Button
    23         android:layout_width="0dp"
    24         android:layout_height="wrap_content"
    25         android:layout_weight="1"
    26         android:text="JDK-Get方式"
    27         android:onClick="bt1_OnClick"/>
    28     </LinearLayout>
    29 <EditText
    30         android:layout_width="match_parent"
    31         android:layout_height="200dp"
    32         android:id="@+id/et_2"/>
    33 
    34 </LinearLayout>
    .xml
      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.util.Log;
      7 import android.view.View;
      8 import android.widget.EditText;
      9 
     10 import java.io.InputStream;
     11 import java.io.OutputStream;
     12 import java.net.HttpURLConnection;
     13 import java.net.URL;
     14 
     15 public class TestActivity3 extends AppCompatActivity {
     16 
     17 
     18     EditText et_2;
     19 
     20     @Override
     21     protected void onCreate(Bundle savedInstanceState) {
     22         super.onCreate(savedInstanceState);
     23         setContentView(R.layout.activity_test3);
     24 
     25         et_2=(EditText)findViewById(R.id.et_2);
     26     }
     27 
     28     //JDK的Get方式
     29 //    public void bt1_OnClick(View v)
     30 //    {
     31 //        //1.进度对话框
     32 //        final ProgressDialog progressDialog = ProgressDialog.show(this,null,"正在加载,请稍后...");
     33 //
     34 //        //2.开启子线程,访问网络
     35 //        new Thread(){
     36 //            public void run()
     37 //            {
     38 //                try {
     39 //                //1.URL
     40 //                URL url = new URL(et_2.getText().toString() + "?name=tom");
     41 //                    //URL url = new URL("http://www.youku.com/" + "?name=tom");
     42 //
     43 //                //2.URL获取连接
     44 //                HttpURLConnection huc = (HttpURLConnection) url.openConnection();
     45 //
     46 //                //请求方式
     47 //                huc.setRequestMethod("GET");
     48 //
     49 //                //设置超时
     50 //                huc.setConnectTimeout(3000);
     51 //                huc.setReadTimeout(3000);
     52 //
     53 //                //连接并发送请求
     54 //                huc.connect();
     55 //
     56 //                //接收
     57 //                //判断返回状态码 200
     58 //                int code = huc.getResponseCode();
     59 //
     60 //                if (code == 200) {
     61 //                    //接收数据
     62 //                    //输入流
     63 //                    InputStream is = huc.getInputStream();
     64 //
     65 //                    //读取流
     66 //
     67 //                    //1.byte数组
     68 //                    byte[] b = new byte[1024];
     69 //
     70 //                    //2.读到数组的长度
     71 //                    int i = 0;
     72 //
     73 //                    //3.数据
     74 //                    final StringBuilder sbl = new StringBuilder();
     75 //
     76 //                    while ((i = is.read(b)) > 0) {
     77 //                        sbl.append(new String(b, 0, i));
     78 //                    }
     79 //
     80 //                    //释放资源
     81 //                    is.close();
     82 //                    huc.disconnect();
     83 //
     84 //                    //通过主线程显示信息和关闭对话框
     85 //                    runOnUiThread(new Runnable() {
     86 //                        @Override
     87 //                        public void run() {
     88 //                            et_2.setText(sbl);
     89 //
     90 //                            progressDialog.dismiss();
     91 //                        }
     92 //                    });
     93 //                } else {
     94 //                    Toast.makeText(TestActivity3.this, "连接错误,返回的状态码=" + code, Toast.LENGTH_SHORT).show();
     95 //                }
     96 //            }
     97 //                catch (Exception e)
     98 //                {
     99 //                    e.printStackTrace();
    100 //
    101 //                    progressDialog.dismiss();
    102 //                }
    103 //            }
    104 //        }.start();
    105 //    }
    106 
    107     //JDK的Get方式
    108 
    109     //显示结果
    110     String str = "";
    111 
    112     public void bt1_OnClick(View v)
    113     {
    114         //1.启动进度对话框
    115         final ProgressDialog pd = ProgressDialog.show(this,null,"正在加载,请稍后...");
    116         //2.启动子线程,访问远程服务器
    117         new Thread(){
    118             @Override
    119             public void run() {
    120 
    121                 //访问远程服务器
    122                 //JDK Get
    123                 HttpURLConnection huc = null;
    124 
    125                 try {
    126                     //1.构造URL对象
    127                     URL url = new URL("http://192.168.0.128:81/index.asp?name=mike&password=456");
    128 
    129                     //2.得到HttpURLConnection
    130                      huc = (HttpURLConnection) url.openConnection();
    131 
    132                     //3.设置HttpURLConnection
    133                     huc.setRequestMethod("GET");
    134                     huc.setConnectTimeout(3000);
    135                     huc.setReadTimeout(3000);
    136 
    137                     //4.连接远程服务器
    138                     huc.connect();
    139 
    140                     //5.接收响应报文的状态码
    141                     int code = huc.getResponseCode();
    142 
    143                     str = "";
    144 
    145                     //6.判断响应状态码是否=200
    146                     if (code == 200) {
    147                         //1.接收数据
    148 
    149                         //2.得到数据流
    150                         InputStream is = huc.getInputStream();
    151 
    152                         //读到的数据
    153                         byte[] b = new byte[1024];
    154 
    155                         //读到的数据长度
    156                         int i = 0;
    157 
    158                         while ((i = is.read(b)) > 0)
    159                         {
    160                             //接收字符串
    161                             str +=new String(b,0,i);
    162                         }
    163 
    164                         is.close();
    165                     }
    166                     else
    167                     {
    168                         str = "响应错误,错误码=" + code;
    169                     }
    170 
    171                     huc.disconnect();
    172 
    173                     //7.处理  显示结果,不能直接跨线程访问主线程的视图
    174                     runOnUiThread(new Runnable() {
    175                         @Override
    176                         public void run() {
    177 
    178                             et_2.setText(str);
    179 
    180 
    181                         }
    182                     });
    183 
    184                     //支持跨线程访问
    185                     pd.dismiss();
    186 
    187 
    188                 }
    189                 catch (Exception e)
    190                 {
    191                     e.printStackTrace();
    192                 }
    193                 finally {
    194                     //8.关闭连接和进度对话框
    195                     //释放资源
    196 
    197                     if (huc !=null)
    198                     {
    199                         huc.disconnect();
    200                     }
    201 
    202                     //支持跨线程访问
    203                     pd.dismiss();
    204                 }
    205             }
    206         }.start();
    207     }
    208 
    209 
    210 
    211 
    212     //Post的Get方式
    213 
    214     //显示结果
    215 
    216 
    217     public void bt2_OnClick(View v)
    218     {
    219         //1.启动进度对话框
    220         final ProgressDialog pd = ProgressDialog.show(this,null,"正在加载,请稍后...");
    221         //2.启动子线程,访问远程服务器
    222         new Thread(){
    223             @Override
    224             public void run() {
    225 
    226                 //访问远程服务器
    227                 //JDK Get
    228                 HttpURLConnection huc = null;
    229 
    230                 try {
    231                     //1.构造URL对象
    232                     URL url = new URL("http://192.168.0.128:81/index.asp");
    233 
    234                     //2.得到HttpURLConnection
    235                     huc = (HttpURLConnection) url.openConnection();
    236 
    237                     //3.设置HttpURLConnection
    238                     huc.setRequestMethod("POST");
    239                     huc.setConnectTimeout(3000);
    240                     huc.setReadTimeout(3000);
    241 
    242 
    243                     //4.连接远程服务器,输出流
    244                     huc.connect();
    245 
    246                     //数据放到请求体里
    247                     //1)得到输出流
    248                     OutputStream os = huc.getOutputStream();
    249 
    250                     String outstr = "name=tom&password=123";
    251 
    252                     os.write(outstr.getBytes("UTF-8"));
    253 
    254                     os.close();
    255 
    256                     Log.e("Tag","发送...");
    257 
    258                     //5.接收响应报文的状态码
    259                     int code = huc.getResponseCode();
    260 
    261                     str = "";
    262 
    263                     //6.判断响应状态码是否=200
    264                     if (code == 200) {
    265 
    266                         Log.e("Tag","接收...");
    267 
    268                         //7.处理
    269                         //1.接收数据
    270 
    271                         //2.得到数据流,输入流
    272                         InputStream is = huc.getInputStream();
    273 
    274                         //读到的数据
    275                         byte[] b = new byte[1024];
    276 
    277                         //读到的数据长度
    278                         int i = 0;
    279 
    280                         while ((i = is.read(b)) > 0)
    281                         {
    282                             //接收字符串
    283                             str +=new String(b,0,i);
    284                         }
    285 
    286                         is.close();
    287                         os.close();
    288                     }
    289                     else
    290                     {
    291                         str = "响应错误,错误码=" + code;
    292                     }
    293 
    294 
    295 
    296                     //显示结果,不能直接跨线程访问主线程的视图
    297                     runOnUiThread(new Runnable() {
    298                         @Override
    299                         public void run() {
    300 
    301                             et_2.setText(str);
    302 
    303 
    304                         }
    305                     });
    306 
    307                     //支持跨线程访问
    308                     pd.dismiss();
    309 
    310 
    311                 }
    312                 catch (Exception e)
    313                 {
    314                     e.printStackTrace();
    315                 }
    316                 finally {
    317                     //8.关闭连接和进度对话框
    318                     //释放资源
    319 
    320                     if (huc !=null)
    321                     {
    322                         huc.disconnect();
    323                     }
    324 
    325                     //支持跨线程访问
    326                     pd.dismiss();
    327                 }
    328             }
    329         }.start();
    330     }
    331 }
    .java

       

    JDK-Get方式

    
    
    JDK-Post方式

    
    
  • 相关阅读:
    numpy 基础 —— np.linalg
    图像旋转后显示不完全
    opencv ---getRotationMatrix2D函数
    PS1--cannot be loaded because the execution of scripts is disabled on this system
    打开jnlp Faild to validate certificate, the application will not be executed.
    BATCH(BAT批处理命令语法)
    oracle vm virtualbox 如何让虚拟机可以上网
    merge 实现
    Windows batch,echo到文件不成功,只打印出ECHO is on.
    python2.7.6 , setuptools pip install, 报错:UnicodeDecodeError:'ascii' codec can't decode byte
  • 原文地址:https://www.cnblogs.com/cycanfly/p/5582384.html
Copyright © 2011-2022 走看看