• 远程存储程序


    程序清单文件:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.hanqi.cunchu">
     4 
     5     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
     6     <!-- 操作文件权限 -->
     7     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
     8 
     9     <uses-permission android:name="android.permission.INTERNET"/>
    10 
    11     <application
    12         android:allowBackup="true"
    13         android:icon="@mipmap/ic_launcher"
    14         android:label="@string/app_name"
    15         android:supportsRtl="true"
    16         android:theme="@style/AppTheme">
    17         <activity android:name=".MainActivity" />
    18         <activity android:name=".lianxi" />
    19         <activity android:name=".shujuku">
    20 
    21         </activity>
    22         <activity android:name=".TestActivity3">
    23             <intent-filter>
    24                 <action android:name="android.intent.action.MAIN" />
    25                 <category android:name="android.intent.category.LAUNCHER" />
    26             </intent-filter>
    27         </activity>
    28     </application>
    29 
    30 </manifest>

    界面文件代码

     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     <Button
    10         android:layout_width="match_parent"
    11         android:layout_height="wrap_content"
    12         android:text="JDK-GET方式"
    13         android:onClick="bt1_onclick"/>
    14     <EditText
    15         android:layout_width="match_parent"
    16         android:layout_height="200dp"
    17         android:id="@+id/et_2"/>
    18 </LinearLayout>

    程序运行代码

     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.view.View;
     7 import android.widget.EditText;
     8 import android.widget.Toast;
     9 
    10 import java.io.InputStream;
    11 import java.net.URL;
    12 
    13 import javax.net.ssl.HttpsURLConnection;
    14 
    15 public class TestActivity3 extends AppCompatActivity {
    16     EditText et_2;
    17     @Override
    18     protected void onCreate(Bundle savedInstanceState) {
    19         super.onCreate(savedInstanceState);
    20         setContentView(R.layout.activity_test3);
    21 
    22         et_2=(EditText)findViewById(R.id.et_2);
    23     }
    24     //JDK的get方式
    25     public void bt1_onclick(View v)
    26     {
    27         //1-进度对话框
    28         final ProgressDialog progressDialog = ProgressDialog
    29                 .show(this, null, "正在加载中. . .");
    30         //2-开启子线程,访问网络
    31         new Thread()
    32         {
    33             public void run()
    34             {
    35                 try
    36                 {
    37                     //1-URL
    38                     URL url = new URL(et_2.getText().toString()+"?name=tom");
    39                     //2-URL获取连接
    40                     HttpsURLConnection httpsURLConnection = (HttpsURLConnection)url.openConnection();
    41                     //请求方式
    42                     httpsURLConnection.setRequestMethod("GET");
    43                     //设置超时
    44                     httpsURLConnection.setConnectTimeout(3000);
    45                     httpsURLConnection.setReadTimeout(3000);
    46                     //连接并发送请求
    47                     httpsURLConnection.connect();
    48                     //接收:判断返回状态码-200
    49                     int code = httpsURLConnection.getResponseCode();
    50                     if (code==2000)
    51                     {
    52                         //接收数据
    53                         //输入流:
    54                         InputStream inputStream = httpsURLConnection.getInputStream();
    55                         //读取流
    56                         //1-byte数组
    57                         byte[]bytes = new byte[1024];
    58                         //2-读到数组的长度
    59                         int i = 0;
    60                         //3-数据
    61                         final StringBuffer stringBuffer1 = new StringBuffer();
    62                         while((i=inputStream.read(bytes))>0)
    63                         {
    64                             stringBuffer1.append(new String(bytes,0,i));
    65                         }
    66                         //释放资源
    67                         inputStream.close();
    68                         httpsURLConnection.disconnect();
    69 
    70                         //通过主线程显示信息和关闭对话框
    71                         runOnUiThread(new Runnable() {
    72                             @Override
    73                             public void run() {
    74                                 et_2.setText(stringBuffer1);
    75                                 progressDialog.dismiss();
    76                             }
    77                         });
    78                     }
    79                     else
    80                     {
    81                         Toast.makeText(TestActivity3.this, "连接错误,返回的状态码="+code, Toast.LENGTH_SHORT).show();
    82                     }
    83                 }
    84                 catch (Exception e)
    85                 {
    86                     e.printStackTrace();
    87                     progressDialog.dismiss();
    88                 }
    89             }
    90         }.start();
    91     }
    92 }
  • 相关阅读:
    C# 监测每个方法的执行次数和占用时间(测试1)
    C# 依赖注入那些事儿
    SQL Server GROUP BY 后 拼接 字符串
    C# 根据Excel生成树
    C# 反射赋值
    C# 集合转换为DataTable
    Oracle 未能加载文件或程序集Oracle.DataAccess
    MySQL 各种主流 SQLServer 迁移到 MySQL 工具对比
    平衡树
    数据结构优化dp
  • 原文地址:https://www.cnblogs.com/TENOKAWA/p/5578856.html
Copyright © 2020-2023  润新知