android 網(wǎng)絡(luò)請(qǐng)求庫(kù)volley方法詳解
使用volley進(jìn)行網(wǎng)絡(luò)請(qǐng)求:需先將volley包導(dǎo)入androidstudio中
File下的Project Structrue,點(diǎn)加號(hào)導(dǎo)包

volley網(wǎng)絡(luò)請(qǐng)求步驟:
1. 創(chuàng)建請(qǐng)求隊(duì)列 RequestQueue queue = Volley.newRequestQueue(this);
2.創(chuàng)建請(qǐng)求對(duì)象(3種)
StringRequest request = new StringRequest(“請(qǐng)求方法”,“請(qǐng)求的網(wǎng)絡(luò)地址”,“成功的網(wǎng)絡(luò)回調(diào)”,“失敗的網(wǎng)絡(luò)回調(diào)”);
ImageRequest request = new ImageRequest(“圖片路徑”,“成功的回調(diào)函數(shù)”,“圖片寬度”,“圖片高度”,“圖片的顏色屬性”,“失敗的網(wǎng)絡(luò)回調(diào)”);
Jsonrequest request = new Jsonrequest();
3.把請(qǐng)求對(duì)象放入請(qǐng)求隊(duì)列 queue.add(request);
// 注銷請(qǐng)求:重寫onstop方法
@Override
protected void onStop() {
super.onStop();
/*取消當(dāng)前請(qǐng)求隊(duì)列的所有請(qǐng)求*/
queue.cancelAll(this);
/*取消當(dāng)前請(qǐng)求隊(duì)列tag為get的請(qǐng)求*/
queue.cancelAll("get");
/*取消當(dāng)前請(qǐng)求隊(duì)列tag為post的請(qǐng)求*/
queue.cancelAll("post");
}
//設(shè)置當(dāng)前請(qǐng)求的優(yōu)先級(jí):重寫getPriority方法
@Override
public Priority getPriority() {
return Priority.LOW;
}
//設(shè)置請(qǐng)求頭:重寫GetHeader方法
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> map = new HashMap<String, String>();
map.put("apikey","fc642e216cd19906f642ee930ce28174");
return map;
}
//傳遞參數(shù):重寫GetParams方法
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map = new HashMap<String, String>();
map.put("num","10");
map.put("page","1");
map.put("word","%E6%9E%97%E4%B8%B9");
return map;
}
代碼部分:
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" android:orientation="vertical" tools:context="com.jredu.helloworld.activity.VolleyActivity"> <WebView android:id="@+id/volleyWebView" android:layout_width="match_parent" android:layout_height="300dp"> </WebView> <ImageView android:id="@+id/img" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/volleyButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:textAllCaps="false" android:text="Volley"/> <Button android:id="@+id/imgButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:textAllCaps="false" android:text="Volley獲取圖片"/> </LinearLayout>
activity文件:
package com.jredu.helloworld.activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
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.HttpHeaderParser;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.jredu.helloworld.R;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
public class VolleyActivity extends AppCompatActivity {
WebView webView;
Button button;
Button imgButton;
ImageView img;
RequestQueue queue = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_volley);
queue = Volley.newRequestQueue(this);
webView = (WebView) findViewById(R.id.volleyWebView);
img = (ImageView) findViewById(R.id.img);
button = (Button) findViewById(R.id.volleyButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doStringVolley2();
doStringVolley();
}
});
imgButton = (Button) findViewById(R.id.imgButton);
imgButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GetImg();
}
});
}
/*get方法*/
public void doStringVolley(){
/*創(chuàng)建請(qǐng)求隊(duì)列*/
//RequestQueue queue = Volley.newRequestQueue(this);
/*創(chuàng)建請(qǐng)求對(duì)象*/
StringRequest request = new StringRequest(
Request.Method.GET,
"http://apis.baidu.com/txapi/tiyu/tiyu?num=10&page=1&word=%E6%9E%97%E4%B8%B9",
/*"http://www.baidu.com",*/
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String s = response;
webView.getSettings().setDefaultTextEncodingName("utf-8");
webView.getSettings().setJavaScriptEnabled(true);
webView.loadDataWithBaseURL(null,s,"text/html","utf-8",null);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
webView.loadDataWithBaseURL(null,"網(wǎng)絡(luò)連接失敗!!!","text/html","utf-8",null);
}
}
){
/*設(shè)置請(qǐng)求頭*/
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> map = new HashMap<String, String>();
map.put("apikey","fc642e216cd19906f642ee930ce28174");
return map;
}
/*解析網(wǎng)絡(luò)請(qǐng)求結(jié)果的方法*/
@Override
protected Response<String> parseNetworkResponse(
NetworkResponse response) {
try {
String jsonObject = new String(
new String(response.data, "UTF-8"));
return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (Exception je) {
return Response.error(new ParseError(je));
}
}
/*設(shè)置當(dāng)前請(qǐng)求的優(yōu)先級(jí)*/
@Override
public Priority getPriority() {
return Priority.LOW;
}
};
request.setTag("get");
/*把請(qǐng)求對(duì)象放入請(qǐng)求隊(duì)列*/
queue.add(request);
}
/*post方法*/
public void doStringVolley2(){
/*創(chuàng)建請(qǐng)求隊(duì)列*/
//RequestQueue queue = Volley.newRequestQueue(this);
/*創(chuàng)建請(qǐng)求對(duì)象*/
StringRequest request = new StringRequest(
Request.Method.POST,
"http://www.baidu.com",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String s = response;
webView.getSettings().setDefaultTextEncodingName("utf-8");
webView.getSettings().setJavaScriptEnabled(true);
webView.loadDataWithBaseURL(null,s,"text/html","utf-8",null);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
webView.loadDataWithBaseURL(null,"網(wǎng)絡(luò)連接失敗!!!","text/html","utf-8",null);
}
}
){
/*重寫params方法寫參數(shù)*/
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map = new HashMap<String, String>();
map.put("num","10");
map.put("page","1");
map.put("word","%E6%9E%97%E4%B8%B9");
return map;
}
/*設(shè)置請(qǐng)求對(duì)象優(yōu)先級(jí)*/
@Override
public Priority getPriority() {
return Priority.HIGH;
}
};
request.setTag("post");
/*把請(qǐng)求對(duì)象放入請(qǐng)求隊(duì)列*/
queue.add(request);
}
/*獲取圖片*/
public void GetImg(){
ImageRequest request = new ImageRequest(
"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png",
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
img.setImageBitmap(response);
}
},
5000,
5000,
Bitmap.Config.ARGB_8888,
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
img.setImageResource(R.drawable.first5);
}
}
);
queue.add(request);
}
/*重寫onStop方法,用來注銷請(qǐng)求*/
@Override
protected void onStop() {
super.onStop();
/*取消當(dāng)前請(qǐng)求隊(duì)列的所有請(qǐng)求*/
queue.cancelAll(this);
/*取消當(dāng)前請(qǐng)求隊(duì)列tag為get的請(qǐng)求*/
queue.cancelAll("get");
/*取消當(dāng)前請(qǐng)求隊(duì)列tag為post的請(qǐng)求*/
queue.cancelAll("post");
}
}
以上就是android 網(wǎng)絡(luò)請(qǐng)求庫(kù)volley方法 的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!
- Android 中Volley二次封裝并實(shí)現(xiàn)網(wǎng)絡(luò)請(qǐng)求緩存
- Android中volley封裝實(shí)踐記錄
- Android Volley框架全面解析
- Android Volley框架使用方法詳解
- Android的HTTP類庫(kù)Volley入門學(xué)習(xí)教程
- Android Volley框架使用源碼分享
- Android中Volley框架下保持會(huì)話方法
- Android 開發(fā)中Volley詳解及實(shí)例
- Android 網(wǎng)絡(luò)請(qǐng)求框架Volley實(shí)例詳解
- Android中volley封裝實(shí)踐記錄(二)
相關(guān)文章
Android UI實(shí)現(xiàn)SlidingMenu側(cè)滑菜單效果
這篇文章主要為大家詳細(xì)介紹了Android UI實(shí)現(xiàn)SlidingMenu側(cè)滑菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Android編程視頻播放API之MediaPlayer用法示例
這篇文章主要介紹了Android編程視頻播放API之MediaPlayer用法,結(jié)合實(shí)例形式分析了基于Android API實(shí)現(xiàn)視頻播放功能的多媒體文件讀取、判斷、事件響應(yīng)及流媒體播放等相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-08-08
Android 簡(jiǎn)單實(shí)現(xiàn)一個(gè)流式布局的示例
本篇文章主要介紹了Android 簡(jiǎn)單實(shí)現(xiàn)一個(gè)流式布局的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02
Java4Android開發(fā)教程(一)JDK安裝與配置
本文是Android開發(fā)系列教程的第一篇,主要為大家?guī)淼氖情_發(fā)環(huán)境的準(zhǔn)備工作,JDK安裝與配置圖文教程,非常的詳細(xì),有需要的朋友可以參考下2014-10-10
Android應(yīng)用隱私合規(guī)檢測(cè)實(shí)現(xiàn)方案詳解
這篇文章主要介紹了Android應(yīng)用隱私合規(guī)檢測(cè)實(shí)現(xiàn)方案,我們需要做的就是提前檢測(cè)好自己的應(yīng)用是否存在隱私合規(guī)問題,及時(shí)整改過來,下面提供Xposed Hook思路去檢測(cè)隱私合規(guī)問題,建議有Xposed基礎(chǔ)的童鞋閱讀,需要的朋友可以參考下2022-07-07
Android實(shí)現(xiàn)關(guān)機(jī)與重啟的幾種方式(推薦)
這篇文章主要介紹了Android實(shí)現(xiàn)關(guān)機(jī)與重啟的幾種方式(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Android gradle插件打印時(shí)間戳的方法詳解
這篇文章主要給大家介紹了關(guān)于Android gradle插件打印時(shí)間戳的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09

