码迷,mamicode.com
首页 > 其他好文 > 详细

Volley的GET和POST方法

时间:2016-04-21 23:42:17      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:

首先记得加上权限

<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);
    }
}

 

Volley的GET和POST方法

标签:

原文地址:http://www.cnblogs.com/youshashuosha/p/5419201.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!