JDK-GET、JDK-PSOT、Volley-GET、Volley-POST

Java类代码:

  1 package com.example.dbwater.myapplication;
  2 
  3 import android.app.DownloadManager;
  4 import com.android.volley.*;
  5 import com.android.volley.toolbox.StringRequest;
  6 import com.android.volley.toolbox.Volley;
  7 
  8 import android.app.ProgressDialog;
  9 import android.app.VoiceInteractor;
 10 import android.support.v7.app.AppCompatActivity;
 11 import android.os.Bundle;
 12 import android.util.Log;
 13 import android.view.View;
 14 import android.widget.EditText;
 15 import android.widget.Toast;
 16 
 17 import java.io.InputStream;
 18 import java.io.OutputStream;
 19 import java.lang.ref.ReferenceQueue;
 20 import java.net.HttpURLConnection;
 21 import java.net.URL;
 22 import java.util.HashMap;
 23 import java.util.Map;
 24 
 25 public class Get_Post extends AppCompatActivity {
 26 
 27     EditText et_1;
 28     ProgressDialog pd;
 29     String str="";
 30     //请求队列
 31     RequestQueue requestQueue;
 32     @Override
 33     protected void onCreate(Bundle savedInstanceState) {
 34         super.onCreate(savedInstanceState);
 35         setContentView(R.layout.activity_get__post);
 36         et_1=(EditText)findViewById(R.id.et_1);
 37         //创建Volley的请求队列,在子线程
 38         requestQueue= Volley.newRequestQueue(this);
 39     }
 40     //JDK-Get
 41     public void bt1_onClick(View view)
 42     {
 43         pd=ProgressDialog.show(this,null,"正在加载请稍候。。。");
 44         new Thread(){
 45             @Override
 46             public void run() {
 47                 HttpURLConnection huc=null;
 48                 try {
 49                     URL url = new URL("http://192.168.3.167:80/a.asp");
 50                     huc=(HttpURLConnection)url.openConnection();
 51                     huc.setRequestMethod("GET");
 52                     huc.setConnectTimeout(3000);
 53                     huc.setReadTimeout(3000);
 54                     huc.connect();
 55                     int code=huc.getResponseCode();
 56                     str="";
 57                     if (code==200)
 58                     {
 59                         InputStream is=huc.getInputStream();
 60                         byte[] b =new byte[1024];
 61                         int i = 0;
 62                         while ((i=is.read(b))>0)
 63                         {
 64                             str +=new String(b,0,1);
 65                         }
 66                         Log.e("TAG","接受...");
 67                         is.close();
 68                     }
 69                     else{
 70                         str="响应错误,错误码="+code;
 71                     }
 72                     runOnUiThread(new Runnable() {
 73                         @Override
 74                         public void run() {
 75                             et_1.setText(str);
 76                         }
 77                     });
 78                 }
 79                 catch (Exception e)
 80                 {
 81                     e.printStackTrace();
 82                 }
 83                 finally {
 84                     if (huc!=null)
 85                     {
 86                         huc.disconnect();
 87                     }
 88                     pd.dismiss();
 89                 }
 90             }
 91         }.start();
 92     }
 93     //JDK方式Post
 94     public void bt2_onClick(View view)
 95     {
 96         pd=ProgressDialog.show(this,null,"请稍候。。。");
 97         new Thread(){
 98             @Override
 99             public void run() {
100                 HttpURLConnection huc=null;
101                 try{
102                     //构造URL对象
103                     URL url=new URL("http://192.168.3.167:80/a.asp?name=tom&pw=123");
104                     //得到连接对象HttpURLConnection
105                     huc=(HttpURLConnection)url.openConnection();
106                     huc.setRequestMethod("POST");
107                     huc.setConnectTimeout(3000);
108                     huc.setReadTimeout(3000);
109                     //连接远程服务器,输出流
110                     huc.connect();
111                     //得到输出流
112                     OutputStream os=huc.getOutputStream();
113                     String outstr="name=tom&password=123";
114                     os.write(outstr.getBytes("UTF-8"));
115                     //接受相应报文的状态码
116                     int code=huc.getResponseCode();
117                     str="";
118                     //判断状态码兵得到输入数据流处理
119                     if (code==200)
120                     {
121                         InputStream is=huc.getInputStream();
122                         byte[] b=new byte[1024];
123                         int i=0;
124                         while ((i=is.read(b))>0)
125                         {
126                             str+=new String(b,0,i);
127                         }
128                         is.close();
129                         os.close();
130                     }
131                     else {
132                         str="响应错误码,错误码="+code;
133                     }
134                     //显示结果,不能跨线程
135                     runOnUiThread(new Runnable() {
136                         @Override
137                         public void run() {
138                             et_1.setText(str);
139                         }
140                     });
141 
142                 }
143                 catch (Exception e)
144                 {
145                     e.printStackTrace();
146                 }
147                 finally {
148                     //关闭连接和进度对话框,释放资源
149                     if (huc!=null)
150                     {
151                         huc.disconnect();
152                     }
153                     pd.dismiss();
154                 }
155             }
156         }.start();
157     }
158     //Volley-Get
159     public void bt3_onClick(View view)
160     {
161         final ProgressDialog pd=ProgressDialog.show(this,null,"请稍候。。。");
162         //构建StringRequest
163         StringRequest sr=new StringRequest("http://192.168.3.167:80/a.asp",
164                 new Response.Listener<String>() {
165                     @Override
166                     public void onResponse(String s) {
167                         //处理正常响应,在主线程里运行
168                         et_1.setText(s);
169                         pd.dismiss();
170                     }
171                 }, new Response.ErrorListener() {
172             @Override
173             public void onErrorResponse(VolleyError volleyError) {
174                 pd.dismiss();
175                 Toast.makeText(Get_Post.this, "响应异常,响应状态码"+volleyError.networkResponse.statusCode, Toast.LENGTH_SHORT).show();
176             }
177         });
178         //加入队列
179         requestQueue.add(sr);
180     }
181     //Volley-Post
182     public void bt4_onClick(View view)
183     {
184         pd=ProgressDialog.show(this,null,"请稍候。。。");
185         //构建StringRequest
186         StringRequest sr=new StringRequest(Request.Method.POST, "http://192.168.3.167:80/a.asp",
187                 new Response.Listener<String>() {
188                     @Override
189                     public void onResponse(String s) {
190                         //处理正常响应,在主线程里运行
191                         et_1.setText(s);
192                         pd.dismiss();
193                     }
194                 }, new Response.ErrorListener() {
195             @Override
196             public void onErrorResponse(VolleyError volleyError) {
197                 pd.dismiss();
198                 Toast.makeText(Get_Post.this, "响应异常,响应状态码="+volleyError.networkResponse.statusCode, Toast.LENGTH_SHORT).show();
199             }
200         }){
201             @Override
202             protected Map<String,String> getParams()throws AuthFailureError{
203                 Map<String,String>rtn=new HashMap<>();
204                 rtn.put("name","postvolley");
205                 rtn.put("password","567");
206                 return rtn;
207             }
208         };
209         //加入队列
210         requestQueue.add(sr);
211     }
212 
213 }
View Code

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     android:orientation="vertical"
11     tools:context="com.example.dbwater.myapplication.Get_Post">
12 
13     <LinearLayout
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:orientation="horizontal">
17         <Button
18             android:layout_width="0dp"
19             android:layout_height="wrap_content"
20             android:layout_weight="1"
21             android:text="JDK-GET方式"
22             android:onClick="bt1_onClick"/>
23         <Button
24             android:layout_width="0dp"
25             android:layout_height="wrap_content"
26             android:layout_weight="1"
27             android:text="JDK-Post方式"
28             android:onClick="bt2_onClick"/>
29     </LinearLayout>
30     <LinearLayout
31         android:layout_width="match_parent"
32         android:layout_height="wrap_content"
33         android:orientation="horizontal">
34         <Button
35             android:layout_width="0dp"
36             android:layout_height="wrap_content"
37             android:layout_weight="1"
38             android:text="VOLLEY-GET方式"
39             android:onClick="bt3_onClick"/>
40         <Button
41             android:layout_width="0dp"
42             android:layout_height="wrap_content"
43             android:layout_weight="1"
44             android:text="VOLLEY-Post方式"
45             android:onClick="bt4_onClick"/>
46     </LinearLayout>
47     <EditText
48         android:layout_width="match_parent"
49         android:layout_height="200dp"
50         android:id="@+id/et_1"/>
51 </LinearLayout>
View Code

原文地址:https://www.cnblogs.com/beens/p/5753107.html