我有点喜欢Volley框架,但我仍然对它有些怀疑.
例如,Volley如何与Loader模式对齐?由于它的请求是以异步方式处理的,因此在后台调用它并没有多大意义.另一方面,如果我们忽略Loader模式,我们将取消加载并重新加载必要的资源,这有点浪费.
Volley框架如何与Android中的Loaders一起使用?
我的问题应该听起来像傻瓜,但我只是从Asynktask跳到RxJava.所以:
可以使用RxJava Observable和Volley Requests吗?这意味着,使用未来的请求.
我问这个问题,因为像改造这样的另一个httpClient 使用RxJava非常好,但是个人喜欢Volley,所以它可能吗?
编辑
基于第一个答案,我知道这是可能的.
你能分享一些展示如何做到这一点的样本吗?
我在SO和其他内容中也经历了很多帖子.但我无法获得任何最新的官方或其他帖子,其中不包含任何弃用的代码,用于使用volley上传多个图像.我开始了解Apache HTTP Client删除和相关的新的Android M和首选使用下面的代替.
android {
useLibrary 'org.apache.http.legacy'
}
Run Code Online (Sandbox Code Playgroud)
那么,任何人都可以通过新的更新已弃用的减少排球类来帮助我进行多个图像上传吗?
是否可以在Google的Volley lib上修改缓存的到期策略?我相信你可以实现自己的缓存,但是有一种简单的方法可以使用默认实现吗?
我正在查看示例和代码,但我没有看到任何实现.这个阶段有可能吗?
我需要执行一个Volley请求并等待响应解析它并返回它,但不知道如何实现这一点.有人可以帮忙吗?
我现在拥有的是:
public String getLink() {
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
shortenURL = response.getString("url");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
queue.add(jsObjRequest);
return shortenURL;
}
Run Code Online (Sandbox Code Playgroud) 嗨,我想模块化排球请求,所以我不会将活动演示代码与排球请求混在一起.我看到的所有样本,凌空请求都被放置 - 例如 - 来自活动按钮的OnClick事件.
我的意思是这段代码(取自差异源代码):
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
// display response
Log.d("Response", response.toString());
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", response);
}
}
);
// add it to the RequestQueue
queue.add(getRequest);
Run Code Online (Sandbox Code Playgroud)
我的观点是如何将所有请求代码发送到另一个类,然后实例化该类并调用makeRequest.我已经尝试了这个,但它失败了.我不知道它是否与Context相关但却失败了......
我这样做了:
public void onClick(View v) {
try{
Utils varRequest = new Utils(getApplicationContext());
String url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
varRequest.makeRequest(url);
mitexto.setText(varRequest.miError);
}
catch(Exception excepcion) {
System.out.println(excepcion.toString());
}
} …Run Code Online (Sandbox Code Playgroud) 我想尝试将Volley与OkHttp结合使用,但是Volley缓存系统和OkHttp都依赖于HTTP规范中定义的HTTP缓存.那么如何禁用OkHttp的缓存以保留一份HTTP缓存?
编辑:我做了什么
public class VolleyUtil {
// http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/
private volatile static RequestQueue sRequestQueue;
/** get the single instance of RequestQueue **/
public static RequestQueue getQueue(Context context) {
if (sRequestQueue == null) {
synchronized (VolleyUtil.class) {
if (sRequestQueue == null) {
OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(new StethoInterceptor());
client.setCache(null);
sRequestQueue = Volley.newRequestQueue(context.getApplicationContext(), new OkHttpStack(client));
VolleyLog.DEBUG = true;
}
}
}
return sRequestQueue;
}
}
Run Code Online (Sandbox Code Playgroud)
这OkHttpClient是从引用https://gist.github.com/bryanstern/4e8f1cb5a8e14c202750
我正在使用Volley与API进行交互.我需要向返回JSON数组的服务发送一个post请求(带参数).
JsonObjectRequest有一个构造函数,它接受一个方法和一组参数
JsonObjectRequest(int method, java.lang.String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener)
Run Code Online (Sandbox Code Playgroud)
但是JSONArrayRequest(我需要的那个)只有一个表单构造函数
JsonArrayRequest(java.lang.String url, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener)
Run Code Online (Sandbox Code Playgroud)
如何使用数据发送POST请求?
在使用第二个网络请求后Volley,我总是收到此错误.我输入的网址是什么似乎并不重要. Volley总是声称它是畸形的.
08-04 20:16:26.885 14453-14470/com.thredup.android E/Volley? [994] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL
java.lang.RuntimeException: Bad URL
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:127)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)
Caused by: java.net.MalformedURLException: Protocol not found:
at java.net.URL.<init>(URL.java:176)
at java.net.URL.<init>(URL.java:125)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:101)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
Run Code Online (Sandbox Code Playgroud)
进一步调查,我在HurlStack中放了几个日志.在
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders),
Run Code Online (Sandbox Code Playgroud)
失败的请求是REQUEST [ ] 0x0 LOW 26."
因此,HurlStack的第101行: URL parsedUrl = new URL(url);
以空URL(request.getUrl()为空)失败.
我正在使用OkHttpStack(扩展HurlStack).关于可能导致这种情况的任何想法?