标签: android-volley

应该维护多少个Volley请求队列?

目前我正在维护一个静态的Volley请求队列,如下所述:

实例化核心Volley对象

  private static RequestQueue mReqQueue;
Run Code Online (Sandbox Code Playgroud)

每个应用程序应该只有一个静态请求队列吗?拥有多个人有什么危害?例如,如果我想要一个请求队列来处理Twitter请求,该怎么办?另外还有其他一切,如身份验证,图像检索等.

android android-volley

14
推荐指数
1
解决办法
6384
查看次数

如何使用OkHttp 2.0实现Android Volley?

OkHttp2.0不再支持此OkHttpStack:https://gist.github.com/JakeWharton/5616899

将OkHttp 2.0.0与Volley集成的当前模式是什么?

android android-volley okhttp

14
推荐指数
2
解决办法
1万
查看次数

在不同的线程中制作排球请求

我想在不同的Thread中使用Volley Library发出请求.

这意味着,Tread中的连接和UIThread中的数据进程.我想这样做,因为我有很多连接所以要处理很多数据,今天我有用户界面就是块...

那么,我怎样才能在不同的线程中建立和启动连接,并在UIThread中执行OnResponse()/ OnErrorResponse()

JsonArrayRequest getReq = new JsonArrayRequest(url,new Response.Listener<JSONArray>() {

    @Override
    public void onResponse(JSONArray response) {
        Log.d("onRESPONSE Synchro -> Produit",response.toString());                                 
        PgrBarProducts.setMax(response.length());       
        percentDisplayProduct.setText("0/"+ PgrBarProducts.getMax());
        nbMaxCallNetwork = PgrBarProducts.getMax();
        try {
            for (int i = 0; i < response.length(); i++) {                                           
                JSONObject explrObject = response.getJSONObject(i);
                String id = Integer.toString((Integer) explrObject.get("id")); 

                callOneObject(id, PgrBarProducts, percentDisplayProduct , 1); // appel du product
             }
        } catch (JSONException e) {
            e.printStackTrace(new PrintWriter(stackTrace));
        }                           
    }
 }, new Response.ErrorListener() {
     @Override
     public void onErrorResponse(VolleyError error) {
         changeStatutToError();
         VolleyLog.d("", …
Run Code Online (Sandbox Code Playgroud)

java multithreading android request android-volley

14
推荐指数
1
解决办法
8796
查看次数

Android - 取消Volley请求

我在我的项目中使用Android Volley lib来执行网络请求,所有工作都很顺利但是我对这个lib的"取消"功能有一些麻烦.我解释我的问题..

我有一个活动,我在OnCreate方法中执行请求,请求被调用,没问题.但为了确保该cancel方法有效,我想测试并尝试两件事:

  1. 我解雇了我的请求,就像取消它之后:

    MySingleton.getMyData("urltocall", getDataListener, requestTag); MySingleton.getRequestQueue().cancelAll(requestTag);

这一个有效!取消被调用(我也可以在Volley的Request类中看到它):

public void cancel() {
        mCanceled = true; // my breakpoint is called here
    }
Run Code Online (Sandbox Code Playgroud)
  1. 我激活了我的请求,在调用了我的活动的finish()方法和活动的方法onDestroy和/或onStop方法后,我正在调用相同的代码:

    MySingleton.getMyData("urltocall", getDataListener, requestTag); MySingleton.getRequestQueue().cancelAll(requestTag);

但这不起作用!

requestTag不是null并且很好地传递给Volley,所以我无法理解为什么第一种方法有效而不是另一种方法...知道我的目的是在onDestroy被调用时取消请求..

谢谢你的帮助

android request android-volley

14
推荐指数
3
解决办法
2万
查看次数

Android Volley error.getMessage()为空

所以我正在为我的服务器创建一个POST JSonObjectRequest,当它成功时一切正常并且信息被发布,但是如果有错误并且我尝试在吐司中显示它,它会显示为空白.这是我的要求:

private void logUserIn() {
    final String URL = Globals.BASE_URL +"/auth/login/";
    // Post params to be sent to the server
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("username", username.getText().toString());
    params.put("password", password.getText().toString());

    JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        Log.d("Log In User", response.toString());

                        //logged in db, changes screens
                        Intent nextScreen = new Intent(getApplicationContext(), MyProfile.class);
                        startActivity(nextScreen);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public …
Run Code Online (Sandbox Code Playgroud)

android android-volley

14
推荐指数
1
解决办法
1万
查看次数

"警告:不要将Android上下文类放在静态字段中;这是内存泄漏(并且还会中断Instant Run)"

类似的问题已经被问这里,这里这里,但背景是从这个完全不同,而且,从这个错误给代码是由Android和Android工作室的制作者编写的.

这是代码:

public class MySingleton {
    private static MySingleton mInstance;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private static Context mCtx;

    private MySingleton(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();

        mImageLoader = new ImageLoader(mRequestQueue,
                new ImageLoader.ImageCache() {
            private final LruCache<String, Bitmap>
                    cache = new LruCache<String, Bitmap>(20);

            @Override
            public Bitmap getBitmap(String url) {
                return cache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                cache.put(url, bitmap);
            }
        });
    }

    public static synchronized MySingleton …
Run Code Online (Sandbox Code Playgroud)

java android memory-leaks android-volley

14
推荐指数
1
解决办法
2万
查看次数

什么是Android Volley的最新版本?

我很遗憾地问这个非常简单的问题,但是我试图找到Android Volley的最后一个版本超过半小时.

这里:https://developer.android.com/training/volley/index.html我找到了

dependencies {
    ...
    compile 'com.android.volley:volley:1.0.0'
}
Run Code Online (Sandbox Code Playgroud)

但我确信这不是最后一个版本.使用这个答案没有帮助,因为它没有显示凌空的任何更新.

谢谢

android android-studio android-volley

14
推荐指数
2
解决办法
2万
查看次数

使用Volley和Dagger获得Robolectric ShadowApplication时的NPE

在我的Android应用程序中,我设置了Volley.

Robolectric.application已初始化,所有其他测试均可顺利运行.尝试获取模拟HTTP响应时出现此错误.

这是我的测试:

@RunWith(MyRobolectricTestRunner.class)
public class ApiTests {

    @Inject
    protected Api api;

    @Before
    public void setUp() {
        ObjectGraph.create(new AndroidModule(Robolectric.application), new TestApplicationModule()).inject(this);
    }

    @Test
    public void shouldGetErrorList() throws Exception {
        Project project = new Project("test", "test", "test", DateTime.now());
        addPendingProjectsErrorsResponse("response.json"); //adding response to FakeHttpLayer

        api.getProjectErrors(project, new Listener<ProjectErrors>() {
                @Override
                public void onResponse(ProjectErrors response) {
                    assertNotNull(response);
                }
            }, new ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    throw new RuntimeException(error);
                }
            }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

Exception in thread "Thread-3" java.lang.NullPointerException
    at org.robolectric.shadows.ShadowLooper.getMainLooper(ShadowLooper.java:59) …
Run Code Online (Sandbox Code Playgroud)

android robolectric dagger android-volley

13
推荐指数
1
解决办法
1444
查看次数

显示没有网络请求的NetworkImageView的默认图像

我有一个ListView显示的东西,其中许多有一个说明性的图像,其中一些没有.ViewHolder使用NetworkImageView显示插图.

问题是即使设置了默认图像,也只有在调用setImageUrl()时才会显示它,如果url为空,则会将视图的源位图设置为null.如果网址不为空,则会发出请求.这意味着即使没有与该特定视图关联的有效网络URL,也强制一个人发出网络请求,否则不显示默认图像而是显示空视图.

(提交的问题:https://code.google.com/p/android/issues/detail?id = 59538)

有没有办法对没有有效网址的项目进行虚假网络请求?

android android-volley networkimageview

13
推荐指数
1
解决办法
1万
查看次数

使用Google排球进行SSL固定

我提到我到目前为止所尝试的内容,我开始提出问题:

我的应用程序中没有证书,我只使用SHA256密钥,互联网上的大多数答案都需要应用程序中的物理证书才能将其加载到密钥库中,我没有.

我收到以下错误:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
Run Code Online (Sandbox Code Playgroud)

1)TrustKit它需要编译SDK 24及以上,但我有23个和许多支持库与SDK 23同步所以我无法更改所有这些,它可能会在某个时候崩溃我的应用程序.

2)CWAC-NetSecurity我在我的代码中实现了这个,没有使用Android N安全设置,我也遵循git页面上给出的指令,但是不能将sslSocketfactory传递给Volley,它有OkHTTP的例子.所以它也给出了上述错误.

我用OKHttp的CertificatePinner尝试了这个,它也不适用于我.同样的错误.我也尝试将hostNameVerifier和sslSocketFactory传递给HttpsUrlConnection但是同样的错误.

JsonObjectRequestSolaire jsonRequest = new JsonObjectRequestSolaire(method, URL, object, headers, responseListener, errorListener);
    RetryPolicy policy = new DefaultRetryPolicy(TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    jsonRequest.setRetryPolicy(policy);
    jsonRequest.setShouldCache(false);

    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .certificatePinner(new CertificatePinner.Builder()
                    .add("my_domain", "sha256/shaKey")//example.com
                    .add("my_domain", "sha256/shaKey")//also tried *.example.com
                    .build())
            .build();

    //HttpsURLConnection.setDefaultHostnameVerifier(okHttpClient.hostnameVerifier());
    //HttpsURLConnection.setDefaultSSLSocketFactory(okHttpClient.sslSocketFactory());

    RequestQueue requestQueue = Volley.newRequestQueue(activity.getApplicationContext(), new HurlStack(null, okHttpClient.sslSocketFactory()));
    requestQueue.add(jsonRequest);
Run Code Online (Sandbox Code Playgroud)

通过使用trustKit我们的iOS人员实现,它正在为他工作.

提前致谢.

请在这里分享您的宝贵意见,以便我能理解这个SSL固定概念.

android android-volley okhttp3 certificate-pinning public-key-pinning

13
推荐指数
1
解决办法
824
查看次数