【问题标题】:Android HTTP Authentication errorAndroid HTTP 身份验证错误
【发布时间】:2012-05-16 18:11:10
【问题描述】:
final HttpClient hClient = new DefaultHttpClient();
final HttpPost hPost = new HttpPost(
        "http://xxx.xxx");
try {
    hPost.setHeader("Accept", "application/json");
    hPost.setHeader("Content-type",
            "application/json");
    hPost.setEntity(new StringEntity(JSON));
    // execute request
    final HttpResponse response = hClient
            .execute(hPost);
    final HttpEntity entity = response.getEntity();

Android 返回 05-16 20:02:52.784: W/DefaultRequestDirector(15642): Authentication error: Unable to respond to any of these challenge: {} 我不知道如何解决它。问题出在哪里,我该如何解决?

【问题讨论】:

标签: java android http http-authentication


【解决方案1】:

尝试如下设置Http参数:

HttpParams myParams = new BasicHttpParams();

HttpConnectionParams.setSoTimeout(myParams, 10000);
HttpConnectionParams.setConnectionTimeout(myParams, 10000); // Timeout

DefaultHttpClient httpClient = new DefaultHttpClient(myParams);

对于 http 帖子和响应,您可以尝试以下代码。请根据您的需要自定义(例如设置您的字符串参数)

HttpResponse response;
httpClient.getParams().setParameter("Your String", "YourStringValue");
localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(YOurURL);

Log.e("URL", URL); //just to check 

httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.RFC_2109);

StringEntity tmp = null;

httpPost.setHeader("Accept", "application/json");

tmp = new StringEntity(inputJObject.toString(), "UTF-8");

httpPost.setEntity(tmp);

response = httpClient.execute(httpPost, localContext);


Log.i(TAG, response.getStatusLine().toString()); // Examine the response status


HttpEntity entity = response.getEntity();  // Get hold of the response entity

【讨论】:

  • 根本没有改变任何东西:(
  • 那么,你是说这里需要超时配置吗? @peter_ziegler
【解决方案2】:

我也有这个警告,但它没有引起任何特殊问题,请求仍然正常。

但是,当我开始使用 AndroidHttpClient 而不是 DefaultHttpClient 时,我就不再收到此错误。一件不舒服的事情是你不能在 UI 线程中使用 AndroidHttpClient 并且需要在另一个线程上运行它。

【讨论】:

  • 我的请求似乎被此警告拒绝,我不知道如何解决。实现这个线程解决方案最简单的方法是什么?
  • 我会尝试 android 上可用的不同 http 客户端。每个人都有不同的能力android-developers.blogspot.com/2011/09/… ...我看不出您的问题没有回答哪些挑战...
【解决方案3】:

我遇到了这个错误,并通过回归基础解决了它!错误是由对https://www.googleapis.com/oauth2/v1/userinfo 的请求生成的。解决方法贴在下面:

            URLConnection conn = (HttpURLConnection) url.openConnection();
            conn.addRequestProperty("key", API_KEY);
    conn.addRequestProperty("client_id", CLIENT_ID);
    conn.addRequestProperty("client_secret", "");
    conn.setRequestProperty("Authorization", "OAuth " + token);

所有添加请求都是 HTTPGET 键/值,并且 setProperty 添加了一个标头参数。由于 Google API 控制台在其当前版本中没有提供客户端密码,我添加了一个 balnk 字符串..这最终解决了 AuthToken 到用户数据的问题:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多