Volley的网络请求实例

本文介绍如何在Android应用中使用Volley库发起GET、POST请求及JSON数据请求。通过XML布局文件定义按钮触发请求,并在MainActivity中实现具体的请求逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.activity.main.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:orientation="vertical">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <!--发送Get请求-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btn_Request_get"
    android:text="GET"/>
    <TextView
        android:id="@+id/tv_Request_get"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <!--发送Post请求-->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_Request_post"
        android:text="POST"/>
    <TextView
        android:id="@+id/tv_Request_post"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <!--请求一段Json-->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_Request_json"
        android:text="JSON"/>
    <TextView
        android:id="@+id/tv_Request_json"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>
2.MainActivity:

package company.com.volley;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends Activity implements View.OnClickListener {
private RequestQueue requestQueue;
    private StringRequest stringRequest_get;
    private StringRequest stringRequest_post;
    private JsonObjectRequest jsonObjectRequest;
    private Button btn_request_post;
    private TextView tv_request_post;

    private Button btn_request_get;
    private TextView tv_request_get;

    private Button btn_request_json;
    private TextView tv_request_json;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
        addStringRequest();
        addListener();
    }
    private void initview() {
        requestQueue= Volley.newRequestQueue(this);//1. 创建一个RequestQueue对象。
        btn_request_get= (Button) findViewById(R.id.btn_Request_get);
        tv_request_get= (TextView) findViewById(R.id.tv_Request_get);
        btn_request_post= (Button) findViewById(R.id.btn_Request_post);
        tv_request_post= (TextView) findViewById(R.id.tv_Request_post);
        btn_request_json= (Button) findViewById(R.id.btn_Request_json);
        tv_request_json= (TextView) findViewById(R.id.tv_Request_json);
    }
    private void addStringRequest() {
    //2. 创建一个StringRequest对象
        //get请求
        stringRequest_get=new StringRequest("http://www.baidu.com", new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                tv_request_get.setText(s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                tv_request_get.setText(volleyError.getMessage());
            }
        });
        //post请求
        stringRequest_post=new StringRequest(1,"http://www.baidu.com", new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
              tv_request_post.setText(s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
               tv_request_post.setText(volleyError.getMessage());
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map=new HashMap<>();
                  map.put("param1","value");
                map.put("param2","value");
                return map;
            }
        };
        //json请求
        jsonObjectRequest=new JsonObjectRequest("https://www.juhe.cn/docs/api/id/46", null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
            tv_request_json.setText(jsonObject.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            tv_request_json.setText(volleyError.getMessage());
            }
        });
    }
    private void addListener() {
        btn_request_get.setOnClickListener(this);
        btn_request_post.setOnClickListener(this);
        btn_request_json.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
    switch (v.getId()){
        case R.id.btn_Request_get:
            //3. 将StringRequest对象添加到RequestQueue里面。
            requestQueue.add(stringRequest_get);
            break;
        case R.id.btn_Request_post:
            requestQueue.add(stringRequest_post);
            break;
        case  R.id.btn_Request_json:
            requestQueue.add(jsonObjectRequest);
            break;
    }
    }
}
点击下载Volley.jar


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值