标签:
VOLLEY-GAT方式:
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 import android.widget.Toast; 10 11 import java.io.InputStream; 12 import java.io.ObjectStreamClass; 13 import java.io.OutputStream; 14 import java.net.URL; 15 import java.util.HashMap; 16 import java.util.Map; 17 18 import com.android.volley.*; 19 import com.android.volley.toolbox.StringRequest; 20 import com.android.volley.toolbox.Volley; 21 22 import javax.net.ssl.HttpsURLConnection; 23 24 public class TestActivity3 extends AppCompatActivity { 25 EditText et_2; 26 //请求队列 27 RequestQueue RQ; 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_test3); 32 et_2=(EditText)findViewById(R.id.et_2); 33 //创建Volley的请求队列 34 //在子线程 35 RQ= Volley.newRequestQueue(this); 36 } 37 //volleyde get方式 38 public void bt3_onclick(View v) 39 { 40 //1-启动进度对话框 41 final ProgressDialog PD = ProgressDialog.show(this, null, "请稍等. . . "); 42 //构建StringRequest 43 StringRequest SR = new StringRequest("http://192.168.0.101:81/index.asp?name=tom&password=456", 44 new Response.Listener<String>() { 45 @Override 46 public void onResponse(String s) { 47 //处理正常请求 48 //在主线程运行 49 et_2.setText(s); 50 PD.dismiss(); 51 } 52 },//这是逗号. 53 new Response.ErrorListener() { 54 @Override 55 public void onErrorResponse(VolleyError volleyError) { 56 //处理错误请求 57 PD.dismiss(); 58 Toast.makeText(TestActivity3.this, "响应异常,响应状态码="+volleyError.networkResponse.statusCode,Toast.LENGTH_LONG).show(); 59 } 60 }); 61 //加入队列 62 RQ.add(SR); 63 } 64 }
VOLLEY-POST方式:
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 import android.widget.Toast; 10 11 import java.io.InputStream; 12 import java.io.ObjectStreamClass; 13 import java.io.OutputStream; 14 import java.net.URL; 15 import java.util.HashMap; 16 import java.util.Map; 17 18 import com.android.volley.*; 19 import com.android.volley.toolbox.StringRequest; 20 import com.android.volley.toolbox.Volley; 21 22 import javax.net.ssl.HttpsURLConnection; 23 24 //volleyde get方式 25 public void bt4_onclick(View v) 26 { 27 //1-启动进度对话框 28 final ProgressDialog PD = ProgressDialog.show(this, null, "请稍等. . . "); 29 //构建StringRequest 30 StringRequest SR = new StringRequest(Request.Method.POST,"http://192.168.0.101:81/index.asp", 31 new Response.Listener<String>() { 32 @Override 33 public void onResponse(String s) { 34 //处理正常请求 35 //在主线程运行 36 et_2.setText(s); 37 PD.dismiss(); 38 } 39 },//这是逗号. 40 new Response.ErrorListener() { 41 @Override 42 public void onErrorResponse(VolleyError volleyError) { 43 //处理错误请求 44 PD.dismiss(); 45 Toast.makeText(TestActivity3.this, "响应异常,响应状态码="+volleyError.networkResponse.statusCode,Toast.LENGTH_LONG).show(); 46 } 47 }){ 48 //继承重写get方法 49 @Override 50 protec
ted Map<String, String> getParams() throws AuthFailureError { 51 Map<String,String>rtn = new HashMap<>(); 52 rtn.put("name","postvolley"); 53 rtn.put("password","567"); 54 return rtn; 55 } 56 }; 57 //加入队列 58 RQ.add(SR); 59 } 60 }
界面文件代码:
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 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_weight="1" 37 android:layout_height="wrap_content" 38 android:text="Volley-GET方式" 39 android:onClick="bt3_onclick"/> 40 <Button 41 android:layout_width="0dp" 42 android:layout_weight="1" 43 android:layout_height="wrap_content" 44 android:text="Volley-POST方式" 45 android:onClick="bt4_onclick"/> 46 </LinearLayout> 47 </LinearLayout>
标签:
原文地址:http://www.cnblogs.com/TENOKAWA/p/5585911.html