zoukankan      html  css  js  c++  java
  • 远程服务器存储之框架方式的get和post请求方式

    一.Volley

       1.特点

        1-轻量级的Android网络通信库

        2-适合数量不大但通信频繁的场景

       2.API

        1-RequestQueue

           1>请求队列

           2>Volley.newRequestQueue(context)  :创建或得到请求队列并且是单例模式

           3>add(Request):添加请求到队列中

        2-Request

           1>封装请求的基类

           2>子类

             1>-StringRequest:返回字符串的请求

                  构造方法:1-new StringRequest(get_path,正常响应监听,异常响应监听):实现GET方法

                                2-new StringRequest(Request.Method.Post,get_path,正常响应监听,异常响应监听){重写Map<String, String> getParams()}:实现POST方法

             2>-JsonRequest:返回Json的请求

             3>-ImageRequest:返回图片请求

           3>Request.Method:GET和POST

    二.加载方法

       1.把jar包放到模块的libs目录下(切换到project视图模式)

       2.在jar包上鼠标右键单击,选择“add as lib”,加入到开发环境

    代码展示:

     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 
    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 
    31     <!--<LinearLayout-->
    32         <!--android:layout_width="match_parent"-->
    33         <!--android:layout_height="wrap_content">-->
    34 
    35         <!--<Button-->
    36             <!--android:layout_width="0dp"-->
    37             <!--android:layout_height="wrap_content"-->
    38             <!--android:layout_weight="1"-->
    39             <!--android:text="Android-Get方式"-->
    40             <!--android:onClick="bt3_OnClick"/>-->
    41         <!--<Button-->
    42             <!--android:layout_width="0dp"-->
    43             <!--android:layout_height="wrap_content"-->
    44             <!--android:layout_weight="1"-->
    45             <!--android:text="Android-Post方式"-->
    46             <!--android:onClick="bt4_OnClick"/>-->
    47     <!--</LinearLayout>-->
    48 
    49     <LinearLayout
    50         android:layout_width="match_parent"
    51         android:layout_height="wrap_content">
    52 
    53         <Button
    54             android:layout_width="0dp"
    55             android:layout_height="wrap_content"
    56             android:layout_weight="1"
    57             android:text="Volley-Get方式"
    58             android:onClick="bt5_OnClick"/>
    59         <Button
    60             android:layout_width="0dp"
    61             android:layout_height="wrap_content"
    62             android:layout_weight="1"
    63             android:text="Volley-Post方式"
    64             android:onClick="bt6_OnClick"/>
    65     </LinearLayout>
    66     <EditText
    67         android:layout_width="match_parent"
    68         android:layout_height="200dp"
    69         android:id="@+id/et_2"/>
    70 
    71 </LinearLayout>
    .xml
      1 package com.hanqi.testapp3;
      2 
      3 import android.app.ProgressDialog;
      4 import android.net.Uri;
      5 import android.support.annotation.RequiresPermission;
      6 import android.support.v7.app.AppCompatActivity;
      7 import android.os.Bundle;
      8 import android.util.Log;
      9 import android.view.View;
     10 import android.widget.EditText;
     11 import android.widget.Toast;
     12 
     13 
     14 import java.io.InputStream;
     15 import java.io.OutputStream;
     16 import java.net.HttpURLConnection;
     17 import java.net.URL;
     18 import java.util.HashMap;
     19 import java.util.Map;
     20 
     21 import com.android.volley.*;
     22 import com.android.volley.toolbox.StringRequest;
     23 import com.android.volley.toolbox.Volley;
     24 
     25 public class TestActivity3 extends AppCompatActivity {
     26 
     27     EditText et_2;
     28 
     29     //请求队列
     30     RequestQueue requestQueue;
     31 
     32 
     33     @Override
     34     protected void onCreate(Bundle savedInstanceState) {
     35         super.onCreate(savedInstanceState);
     36         setContentView(R.layout.activity_test3);
     37 
     38         et_2 = (EditText) findViewById(R.id.et_2);
     39 
     40         // 创建Volley的请求队列
     41         // 在子线程
     42         requestQueue= Volley.newRequestQueue(this);
     43 
     44     }
     45 
     46     //Volley的Get方式
     47     public void bt5_OnClick(View v)
     48     {
     49 
     50         //1.启动进度对话框
     51         final ProgressDialog pd = ProgressDialog.show(this, null, "请稍候...");
     52 
     53         //构建StringRequest
     54         StringRequest stringRequest=new StringRequest("Http://192.168.0.107:81/index.asp?name=volley&password=456",
     55                 new Response.Listener<String>() {
     56                     @Override
     57                     public void onResponse(String s) {
     58 
     59                         //处理正常响应
     60                         //在主线程运行
     61                         et_2.setText(s);
     62 
     63                         pd.dismiss();
     64                     }
     65                 },
     66                 new Response.ErrorListener() {
     67             @Override
     68             public void onErrorResponse(VolleyError volleyError) {
     69 
     70                 //处理异常响应
     71                 volleyError.printStackTrace();
     72 
     73                 pd.dismiss();
     74                 Toast.makeText(TestActivity3.this, "响应异常 响应状态码="+volleyError.networkResponse.statusCode, Toast.LENGTH_SHORT).show();
     75             }
     76         });
     77 
     78         //加入队列
     79         requestQueue.add(stringRequest);
     80     }
     81 
     82 
     83     //Volley的Post方式
     84     public void bt6_OnClick(View v)
     85     {
     86 
     87         //1.启动进度对话框
     88         final ProgressDialog pd = ProgressDialog.show(this, null, "请稍候...");
     89 
     90         //构建StringRequest
     91         StringRequest stringRequest=new StringRequest(Request.Method.POST,"Http://192.168.0.107:81/index.asp",
     92                 new Response.Listener<String>() {
     93                     @Override
     94                     public void onResponse(String s) {
     95 
     96                         //处理正常响应
     97                         //在主线程运行
     98                         et_2.setText(s);
     99 
    100                         pd.dismiss();
    101                     }
    102                 },
    103                 new Response.ErrorListener() {
    104                     @Override
    105                     public void onErrorResponse(VolleyError volleyError) {
    106 
    107                         //处理异常响应
    108                         volleyError.printStackTrace();
    109 
    110                         pd.dismiss();
    111                         Toast.makeText(TestActivity3.this, "响应异常 响应状态码="+volleyError.networkResponse.statusCode, Toast.LENGTH_SHORT).show();
    112                     }
    113                 }){
    114 
    115             //重写  得到参数的方法
    116             @Override
    117             protected Map<String, String> getParams() throws AuthFailureError {
    118 
    119                 Map<String,String> rtn=new HashMap<>();
    120 
    121                 rtn.put("name","postVolley");
    122                 rtn.put("password","567");
    123 
    124                 return rtn;
    125             }
    126         };
    127 
    128         //加入队列
    129         requestQueue.add(stringRequest);
    130     }
    131 
    132 }
    .java

     

  • 相关阅读:
    NSURLConnection实现文件上传和AFNetworking实现文件上传
    scrollToItemAtIndexPath: atScrollPosition: animated:
    UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning
    界面传值时数组赋值问题
    安装cocoapods遇到两大坑-Ruby版本升级和Podfile的配置
    iOS通知的整理笔记
    iOS简单实现毛玻璃效果
    iOS模态弹出半透明视图控制器
    友盟SDK实现分享
    iOS 本地存储四种方法
  • 原文地址:https://www.cnblogs.com/arxk/p/5585682.html
Copyright © 2011-2022 走看看