截击:未发现身份验证挑战

Volley: No authentication challenges found

我在 Android 应用程序中使用 Volley 从 Misfit API (http://build.misfit.com) 获取数据。我试图构造一个间歇性的 activity,在有人登录后,从 API 获取所有数据。在那个 activity 中,我执行了一个 JsonObject GET 请求,它应该给我一些关于应用程序用户的信息。到目前为止,这是代码:

package com.iss_fitness.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

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.Volley;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import learn2crack.weboauth2.R;

public class LoadingScreenActivity extends Activity {

    //Introduce an delay
    private final int WAIT_TIME = 500;
    private static final String QUERY_URL = "https://api.misfitwearables.com/move/resource/v1/user/me/profile";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        System.out.println("LoadingScreenActivity  screen started");
        setContentView(R.layout.loading_screen);
        findViewById(R.id.mainSpinner1).setVisibility(View.VISIBLE);

        // Instantiate the RequestQueue.
        final RequestQueue queue = Volley.newRequestQueue(this);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                executeJson();
                System.out.println("Going to Profile Data");
                /* Create an Intent that will start the ProfileData-Activity. */
                Intent mainIntent = new Intent(LoadingScreenActivity.this, DataView.class);
                LoadingScreenActivity.this.startActivity(mainIntent);
                LoadingScreenActivity.this.finish();
            }
        }, WAIT_TIME);
    }

    private Response.ErrorListener createRequestErrorListener() {

        return new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println(error);
            }
        };
    }

    private void executeJson() {
        SharedPreferences prefs = this.getSharedPreferences("AppPref", MODE_PRIVATE);
        final String token = prefs.getString("token", null);
        RequestQueue queue = Volley.newRequestQueue(this);
        Map<String, String> params = new HashMap<String, String>();
        System.out.println(token);
        params.put("access_token", token);
        CustomRequest jsonRequest = new CustomRequest(Request.Method.GET, QUERY_URL, params,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        System.out.println(response);
                    }
                }, this.createRequestErrorListener());
        System.out.println(jsonRequest);
        queue.add(jsonRequest);
    }
}

我是 Android 开发的新手,所以请耐心等待,我会尽量描述代码。我已经按照 JsonObjectRequest 的建议实现了帮助 class,因为据我了解,在本地定义请求时不能覆盖 getparams 方法。 executeJson() 方法是一个有趣的方法:我从我的 SharedPreferences(正确存储它的地方)获取用户访问令牌,将其放入 String Map 并将其提供给 CustomRequest,其中,在帮助 class 中它被扔进一个 getparams 方法,该方法只是 returns 参数。遗憾的是,responselistener 从未被调用,因为 errorlistener 报告如下:

com.android.volley.NoConnectionError: java.io.IOException: No authentication challenges found

根据 Misfit 的 API 参考资料,这应该可行。 现在,我知道 GET 请求需要 "headers" 而不是 "params" 但这有什么区别吗?

好的,我找到了解决办法。助手 class 包含一个重写的 getparams 方法,但没有 getheaders 方法。 GET 请求需要 getheaders,post 需要 getparams。