Android框架Volley使用之Post請求實(shí)現(xiàn)方法
首先我們在項目中導(dǎo)入這個框架:
implementation 'com.mcxiaoke.volley:library:1.0.19'
在AndroidManifest文件當(dāng)中添加網(wǎng)絡(luò)權(quán)限:
<uses-permission android:name="android.permission.INTERNET"/>
下面是我們的首頁布局:
在這個布局當(dāng)中我們將Volley框架的所有功能都做成了一個按鈕,按下按鈕之后就會在“顯示結(jié)果”下面顯示結(jié)果,顯示結(jié)果下面使用了一個ScrollView,并在ScrollView下面嵌套了一個Textview和Imageview,用于把我們加載成功之后的圖片和文字進(jìn)行顯示。

下面是首頁布局的代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/get" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Get請求"/> <Button android:id="@+id/post" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Post請求"/> <Button android:id="@+id/json" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="請求JSON"/> <Button android:id="@+id/ImageRquest" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ImageRquest加載圖片"/> <Button android:id="@+id/ImageLoader" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ImageLoader加載圖片"/> <Button android:id="@+id/NetWorkImageView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="NetWorkImageView加載圖片"/> <TextView android:text="顯示結(jié)果" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:visibility="gone" android:id="@+id/iv_volley" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <com.android.volley.toolbox.NetworkImageView android:id="@+id/NetWork" android:visibility="gone" android:layout_width="200dp" android:layout_height="200dp" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_volley_result" android:layout_width="match_parent" android:layout_height="match_parent" /> </ScrollView> </LinearLayout>
為了實(shí)現(xiàn)POST請求,進(jìn)行POST請求一共需要三步,分別是:
1.創(chuàng)建一個請求隊列
2.創(chuàng)建一個請求
3.將創(chuàng)建的請求添加到請求隊列當(dāng)中
在創(chuàng)建請求的時候,必須同時寫兩個監(jiān)聽器,一個是實(shí)現(xiàn)請求,正確接受數(shù)據(jù)的回調(diào),另一個是發(fā)生異常之后的回調(diào)。這里我們準(zhǔn)備了json數(shù)據(jù),是在gank.io的官網(wǎng)上找的,大家可以自行百度一下,這里就直接采用了網(wǎng)址:
http://api.m.mtime.cn/PageSubArea/TrailerList.api
當(dāng)中的json數(shù)據(jù)進(jìn)行POST請求了,只要我們在文本顯示區(qū)返回的數(shù)據(jù)和這個網(wǎng)站上面的數(shù)據(jù)顯示相同,則請求成功。如果不同也會顯示出錯誤的原因。
實(shí)現(xiàn)的核心代碼如下:
post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 1 創(chuàng)建一個請求隊列
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
// 2 創(chuàng)建一個post請求
String url = "http://api.m.mtime.cn/PageSubArea/TrailerList.api";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
tv_volley_result.setText(s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
tv_volley_result.setText("請求失敗" + volleyError);
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
// map.put("value1","param1");
return map;
}
};
// 3 將post請求添加到隊列中
requestQueue.add(stringRequest);
}
});
全部主活動的Java代碼如下:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
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.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private Button get;
private Button post;
private Button json;
private Button imagerequest;
private Button imageload;
private Button netWorkImageView;
private ImageView iv;
private NetworkImageView network;
private TextView tv_volley_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
initListener();
}
public void initview()//把需要初始化的控件的邏輯都寫在這里是一個很好的編程范式
{
get=findViewById(R.id.get);
post=findViewById(R.id.post);
json=findViewById(R.id.json);
imagerequest=findViewById(R.id.ImageRquest);
imageload=findViewById(R.id.ImageLoader);
netWorkImageView=findViewById(R.id.NetWorkImageView);
iv=findViewById(R.id.iv_volley);
network=findViewById(R.id.NetWork);
tv_volley_result=findViewById(R.id.tv_volley_result);
}
public void initListener()
{
get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//創(chuàng)建一個請求隊列
RequestQueue requestQueue=Volley.newRequestQueue(MainActivity.this);
//創(chuàng)建一個請求
String url="http://gank.io/api/xiandu/category/wow";
StringRequest stringRequest=new StringRequest(url, new Response.Listener<String>() {
//正確接受數(shù)據(jù)之后的回調(diào)
@Override
public void onResponse(String response) {
tv_volley_result.setText(response);
}
}, new Response.ErrorListener() {//發(fā)生異常之后的監(jiān)聽回調(diào)
@Override
public void onErrorResponse(VolleyError error) {
tv_volley_result.setText("加載錯誤"+error);
}
});
//將創(chuàng)建的請求添加到請求隊列當(dāng)中
requestQueue.add(stringRequest);
}
});
post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 1 創(chuàng)建一個請求隊列
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
// 2 創(chuàng)建一個post請求
String url = "http://api.m.mtime.cn/PageSubArea/TrailerList.api";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
tv_volley_result.setText(s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
tv_volley_result.setText("請求失敗" + volleyError);
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
// map.put("value1","param1");
return map;
}
};
// 3 將post請求添加到隊列中
requestQueue.add(stringRequest);
}
});
json.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 1 創(chuàng)建一個請求隊列
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
// 2 創(chuàng)建一個請求
String url = "http://gank.io/api/xiandu/category/wow";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
tv_volley_result.setText(jsonObject.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
tv_volley_result.setText("請求失敗" + volleyError);
}
});
// 3 將創(chuàng)建的請求添加到請求隊列中
requestQueue.add(jsonObjectRequest);
}
});
imagerequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
imageload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
netWorkImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
}
總結(jié)
以上所述是小編給大家介紹的Android框架Volley使用之Post請求實(shí)現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
- Android框架Volley之利用Imageloader和NetWorkImageView加載圖片的方法
- Android框架Volley使用之Json請求實(shí)現(xiàn)
- Android框架Volley使用:ImageRequest請求實(shí)現(xiàn)圖片加載
- Android中volley封裝實(shí)踐記錄(二)
- Android中volley封裝實(shí)踐記錄
- Android Volley擴(kuò)展實(shí)現(xiàn)支持進(jìn)度條的文件上傳功能
- Android使用Volley框架定制PostUploadRequest上傳文件
- Android使用Volley實(shí)現(xiàn)上傳文件功能
- 解析Android框架之Volley源碼
相關(guān)文章
利用Android實(shí)現(xiàn)光影流動特效的方法詳解
Flutter 的畫筆類 Paint 提供了很多圖形繪制的配置屬性,來供我們繪制更豐富多彩的圖形。本篇我們引入一個 Paint 類新的屬性:maskFilter,再結(jié)合之前的 shader 和動畫,制作出光影流動特效,感興趣的可以嘗試一下2022-07-07
Android實(shí)現(xiàn)二維碼掃描和生成的簡單方法
這篇文章主要介紹了Android實(shí)現(xiàn)二維碼掃描和生成的簡單方法的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
Android標(biāo)題欄上添加多個Menu按鈕的實(shí)例
這篇文章主要介紹了Android標(biāo)題欄上添加多個Menu按鈕的實(shí)例的相關(guān)資料,這里提供簡單實(shí)例說明如何添加多個menu按鈕的方法,需要的朋友可以參考下2017-09-09
android開發(fā)教程之使用線程實(shí)現(xiàn)視圖平滑滾動示例
這篇文章主要介紹了android使用線程實(shí)現(xiàn)視圖平滑滾動示例,需要的朋友可以參考下2014-03-03
Flutter學(xué)習(xí)教程之Route跳轉(zhuǎn)以及數(shù)據(jù)傳遞
這篇文章主要給大家介紹了關(guān)于Flutter學(xué)習(xí)教程之Route跳轉(zhuǎn)以及數(shù)據(jù)傳遞的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Android圓形頭像拍照后“無法加載此圖片”的問題解決方法(適配Android7.0)
這篇文章主要介紹了Android圓形頭像拍照后“無法加載此圖片”的問題解決方法(適配Android7.0) ,需要的朋友可以參考下2017-10-10

