Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android针对应用引擎服务器的请求失败(401)_Android_Google App Engine_Authentication_Cookies_Httpclient - Fatal编程技术网

android针对应用引擎服务器的请求失败(401)

android针对应用引擎服务器的请求失败(401),android,google-app-engine,authentication,cookies,httpclient,Android,Google App Engine,Authentication,Cookies,Httpclient,我正在针对应用引擎服务器对android应用程序进行身份验证,基本上遵循以下帖子: 看起来我在过程结束时得到的cookie不好——我得到了401,所以我尝试复制cookie并在浏览器中测试它,但仍然得到了401。将浏览器cookie复制到android应用程序时,请求会起作用 我怎么会得到一块无效的饼干?我甚至尝试过使令牌无效,但仍然得到相同的结果…不要麻烦自己这么做。使用这个:不要麻烦自己做这件事。使用此选项:不知道您使用的是什么httpclient 也许阅读apache会有所帮助…不知道您使

我正在针对应用引擎服务器对android应用程序进行身份验证,基本上遵循以下帖子:

看起来我在过程结束时得到的cookie不好——我得到了401,所以我尝试复制cookie并在浏览器中测试它,但仍然得到了401。将浏览器cookie复制到android应用程序时,请求会起作用


我怎么会得到一块无效的饼干?我甚至尝试过使令牌无效,但仍然得到相同的结果…

不要麻烦自己这么做。使用这个:

不要麻烦自己做这件事。使用此选项:

不知道您使用的是什么httpclient


也许阅读apache会有所帮助…

不知道您使用的是什么httpclient


也许阅读apache会有所帮助…

出于某种原因,这起作用了:我将初始cookie请求的URL从https更改为http,然后又更改了回去

但最终我决定改变我的实现,按照alistair的建议去做。结果是更加优雅。这是我的登录活动(请注意,我正在将客户端连接到loopj api提供的持久cookie存储):


顺便说一句&供参考:loopj库使用
SharedReferences
API来存储cookie,并将其巧妙地包装为
PersistentCookieStore

,出于某种原因,这起到了作用:我将初始cookie请求的URL从https更改为http,然后又将其更改回来

但最终我决定改变我的实现,按照alistair的建议去做。结果是更加优雅。这是我的登录活动(请注意,我正在将客户端连接到loopj api提供的持久cookie存储):


顺便说一句&供参考:loopj库使用
SharedReferences
API来存储cookie,并将其巧妙地包装为
PersistentCookieStore

此库可以获得应用程序引擎cookie吗?你能给出一个代码示例吗?从文档来看似乎不是这样。这个库可以获得应用程序引擎cookie吗?你能给出一个代码示例吗?从文档上看似乎不是这样,我使用的是
DefaultHttpClient=newdefaulthttpclient()。但是看我的编辑,问题是cookie一开始是无效的,这就是为什么我要得到401。但是看我的编辑,问题是cookie一开始是无效的,这就是为什么我得到了401。是的,它把我定位到了正确的位置,这就是为什么我投了高票,只是缺少一点解释。是的,它把我定位到了正确的位置,这就是为什么我投了高票,只是缺少一点解释。
public class AccountList extends ListActivity {
    protected AccountManager accountManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        accountManager = AccountManager.get(getApplicationContext());
        Account[] accounts = accountManager.getAccountsByType("com.google");
        this.setListAdapter(new ArrayAdapter<Account>(this, R.layout.list_item, accounts));   
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Account account = (Account)getListView().getItemAtPosition(position);
        accountManager.invalidateAuthToken("com.google", null);
        accountManager.getAuthToken(account, "ah", null, this, new GetAuthTokenCallback(), null);
    }

    private class GetAuthTokenCallback implements AccountManagerCallback<Bundle> {
        public void run(AccountManagerFuture<Bundle> result) {
            AsyncHttpClient client = new AsyncHttpClient();
            PersistentCookieStore myCookieStore = new PersistentCookieStore(getBaseContext());
            client.setCookieStore(myCookieStore);
            try {
                Bundle bundle;
                bundle = result.getResult();
                Intent intent = (Intent)bundle.get(AccountManager.KEY_INTENT);
                if(intent != null) {
                    // User input required
                    startActivity(intent);
                } else {
                    String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                    String url = "http://myapp.appspot.com/_ah/login?continue=http://localhost/&auth=" + token;
                    client.post(url, new AsyncHttpResponseHandler());
                    Intent backToMainActivity = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(backToMainActivity);
                }
            } catch (OperationCanceledException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (AuthenticatorException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
AsyncHttpClient client = new AsyncHttpClient();
client.setCookieStore(new PersistentCookieStore(this));