zoukankan      html  css  js  c++  java
  • Volley的GET和POST方法

    首先记得加上权限

    <uses-permission android:name="android.permission.INTERNET"/>

    XML代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.dell.fuwuqicunchu.fuwuqicunchu"
        android:orientation="vertical">
    
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:id="@+id/et_1"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="volleyget"
                android:onClick="volleyget"
                android:layout_weight="1"/>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="volleypost"
                android:onClick="volleypost"
                android:layout_weight="1"/>
        </LinearLayout>
    
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et_2"/>
    </LinearLayout>

    JAVA代码

    package com.example.dell.myfuwuqi;
    
    import android.app.ProgressDialog;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;
    
    import com.android.volley.AuthFailureError;
    import com.android.volley.Request;
    import com.android.volley.RequestQueue;
    import com.android.volley.Response;
    import com.android.volley.VolleyError;
    import com.android.volley.toolbox.StringRequest;
    import com.android.volley.toolbox.Volley;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class webActivity extends AppCompatActivity {
    
        EditText et_1;
        EditText et_2;
    
        RequestQueue requestQueue;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_web);
    
            et_1=(EditText)findViewById(R.id.et_1);
            et_1.setText("http://192.168.1.101:8080/Testweb/testweb.jsp");
            et_2=(EditText)findViewById(R.id.et_2);
    
            requestQueue =Volley.newRequestQueue(this);
    
        }
    
        public void volleyget(View view)
        {
            final ProgressDialog progressDialog = new ProgressDialog(this);
    
            StringRequest str = new StringRequest(et_1.getText().toString(), new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
    
                    et_2.setText(s);
    
                    progressDialog.dismiss();
    
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(webActivity.this, "失败", Toast.LENGTH_SHORT).show();
                    progressDialog.dismiss();
                }
            });
    
            requestQueue.add(str);
    
        }
    
    
    
        public void volleypost(View v)
        {
            final ProgressDialog dialog = new ProgressDialog(this);
    
            StringRequest str = new StringRequest(Request.Method.POST, et_1.getText().toString(), new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
    
                    et_2.setText(s);
    
                    dialog.dismiss();
    
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
    
                    Toast.makeText(webActivity.this, "失败", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
            }){
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String,String> map = new HashMap<String,String>();
    
                    map.put("name","volley");
                    return map;
                }
            };
    
            requestQueue.add(str);
        }
    }
  • 相关阅读:
    Perl Resources
    使用Perl管理Oracle数据库
    Tomcat 发布部署jsp网站—-使用自己的ip访问jsp网站
    Lode's Computer Graphics Tutorial Image Filtering
    Java Image Filters
    ZPhotoEngine超级算法库
    ps亮度对比度算法
    滤镜艺术---新海诚(你的名字)滤镜特效的实现解密
    Cocoa-GL-Tutorial
    Learning Cocoa with Objective-C
  • 原文地址:https://www.cnblogs.com/youshashuosha/p/5419201.html
Copyright © 2011-2022 走看看