java - Volley : No authentication challenges found

标签 java android android-volley misfit

我在 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 的建议实现了一个帮助类,因为据我了解,在本地定义请求时无法重写 getparams 方法。 executeJson() 方法很有趣:我从 SharedPreferences 中获取用户访问 token (正确存储的位置),将其放入 String Map 中并将其提供给 CustomRequest,在帮助类中,它被扔进一个简单返回参数的 getparams 方法。遗憾的是,响应监听器永远不会被调用,因为错误监听器报告以下内容:

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

根据 Misfit 的 API 引用,这应该可行。 现在,我知道 GET 请求需要“ header ”而不是“参数”,但这有什么区别吗?

最佳答案

好的,我找到了解决方案。帮助程序类包含一个重写 getparams 方法,但没有 getheaders 方法。 GET请求需要getheaders,post需要getparams。

关于java - Volley : No authentication challenges found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31593894/

相关文章:

java - 如何在java中将所有SQL表数据插入到一个数组中[android studio]

Android OAuth2 Bearer token 最佳实践

java - 使用 AlarmManager 设置一些通知并取消其中之一

java - 循环创建动态 SQL 字符串

android - 仅在操作模式 (CAB) 中拆分操作栏

android - 避免在不同的 drawable 文件夹中处理相同的 drawable

android - 使用 Volley POST 请求获取 Twitter 授权 token

Java:从html文本到xhtml文本的Jtidy转换

java - 无法找到 tools.jar。预计在 C :\Program Files\Java\jre6\li b\tools. jar 中找到它

android - 在 google playstore 中将订阅设置为非 Activity 状态