驗證錯誤:無法對任何的應對這些挑戰:{} Android的 - 從這個鏈接401未授權驗證錯誤:無法對任何的應對這些挑戰:{}的Android - 401未授權
我已經採取了參考 Authentication Error when using HttpPost with DefaultHttpClient on Android
我正在使用支持Drupal的Android應用程序。在這我發送數據從Android應用程序到Drupal網站 - JSON格式的Web服務。現在我可以從Drupal web服務中讀取JSON數據並將其寫入我的android應用程序中。但是,在從機器人的Drupal寫面臨的問題,它產生響應的狀態代碼:
401 Unauthorized
從中可以產生401 Android原生應用程序,而來自PhoneGap的,從機器人,當我發起AJAX請求,它完美&寫文章或在Drupal網站上的頁面。這樣就意味着Web服務的工作完全&
my phonegap android app works perfectly there is problem with Android native JAVA application I am running my android application on Android2.3.4 -> Samsung Galaxy S Plus - Samsung GT-I9001
這是我對Java的Android代碼。
==============================
String url = "XXX";
strResponse1 = makeWebForPostIdea(url,title,body);
public static String makeWebForPostIdea(String url, String title,String body)
{
JSONStringer jsonobject = null;
JSONObject json = null;
JSONObject jsonnode = null;
DefaultHttpClient client = new DefaultHttpClient();
Credentials creds = new UsernamePasswordCredentials("username", "password");
client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), creds);
HttpPost post = new HttpPost(url);
System.out.println("value of the post =============> "+post);
try {
JSONObject jsonvalue = new JSONObject();
jsonvalue.put("value", body.toString());
JSONArray array = new JSONArray();
array.put(jsonvalue);
jsonnode = new JSONObject();
jsonnode.put("und", array);
System.out.println("@@@@@@2 jsonnode=======>"+jsonnode.toString());
} catch (JSONException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
try {
jsonobject = new JSONStringer().array().object().key("und").object().key("0").object().key("value").value(body).endObject().endObject().endObject().endArray();
System.out.println("=============>"+jsonobject);
} catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("type","page"));
params.add(new BasicNameValuePair("title",title));
params.add(new BasicNameValuePair("language","und"));
params.add(new BasicNameValuePair("body",jsonobject.toString()));
System.out.println("value of the params =============> "+params);
UrlEncodedFormEntity formEntity = null;
try {
formEntity = new UrlEncodedFormEntity(params);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
post.setEntity(formEntity);
try {
HttpResponse response = client.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("=========> statusCode post idea=====> "+statusCode);
if (statusCode == HttpStatus.SC_OK)
{
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
return iStream_to_String(is);
}
else
{
return "Hello This is status ==> :"+String.valueOf(statusCode);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static String iStream_to_String(InputStream is1) {
BufferedReader rd = new BufferedReader(new InputStreamReader(is1), 4096);
String line;
StringBuilder sb = new StringBuilder();
try {
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String contentOfMyInputStream = sb.toString();
return contentOfMyInputStream;
}
}
}
這裏是我收到的logcat。
08-09 12:41:29.063: I/System.out(336): value of the post =============> [email protected]
08-09 12:41:29.093: I/System.out(336): @@@@@@2 jsonnode=======>{"und": [{"value":"ddddddd"}]}
08-09 12:41:29.093: I/System.out(336): =============>[{"und":{"0":{"value":"ddddddd"}}}]
08-09 12:41:29.103: I/System.out(336): value of the params =============> [type=page, title=hhhh, language=und, body=[{"und":{"0":{"value":"ddddddd"}}}]]
08-09 12:41:30.913: W/DefaultRequestDirector(336): Authentication error: Unable to respond to any of these challenges: {}
08-09 12:41:30.913: I/System.out(336): =========> statusCode post idea=====> 401
08-09 12:41:30.924: I/System.out(336): =========> Response from post idea => Hello This is status ==> :401
這是我的PhoneGap Ajax請求,它完美的工作。
$('#page_node_create_submit').live('click',function(){
var title = $('#page_node_title').val();
//if (!title) { alert('Please enter a title.'); return false; }
var body = $('#page_node_body').val();
//if (!body) { alert('Please enter a body.'); return false; }
// BEGIN: drupal services node create login (warning: don't use https if you don't have ssl setup)
$.ajax({
url: "XXX",
type: 'post',
data: 'node[type]=page&node[title]=' + encodeURIComponent(title) + '&node[language]=und&node[body][und][0][value]=' + encodeURIComponent(body),
dataType: 'json',
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert('page_node_create_submit - failed to login');
console.log(JSON.stringify(XMLHttpRequest));
console.log(JSON.stringify(textStatus));
console.log(JSON.stringify(errorThrown));
},
success: function (data) {
$.mobile.changePage("index.html", "slideup");
}
});
// END: drupal services node create
return false;
});
=========================================== ======================================
編輯:
我有爲我的錯誤嘗試了各種Apache httpclient方法。在此期間,我已經完成了一些研究並在google上搜索並找到了一些有趣的東西。
1st我發現Android-Google的東西正式不推薦我在我的代碼中使用的Apache HttpClient。檢查這個鏈接。來自Dalvik團隊的傑西威爾遜的Link消息。因爲他們建議使用HttpURLConnection而不是DefaultHttpClient,並且寫道Android團隊將不再開發Apache httpclient。所以它是我正在使用的舊版本。
http://android-developers.blogspot.in/2011/09/androids-http-clients.html
第二的事情,我已經找到形式此鏈接。它表明,Android在發佈Apache的HttpClient 4.0 Beta2時,它有一個陷阱,當談到基本認證。我使用的身份驗證方法是HttpClient 3.x,我從這個鏈接中找到了它。 檢查鏈接。 http://hc.apache.org/httpclient-3.x/authentication.html#Preemptive_Authentication
所以版本問題。
http://dlinsin.blogspot.in/2009/08/http-basic-authentication-with-android.html
我也發現了一些與此問題的潛在解決方案的鏈接。
http://ogrelab.ikratko.com/using-newer-version-of-httpclient-like-4-1-x/
Apache HttpClient 4.1 on Android
What version of Apache HTTP Client is bundled in Android 1.6?
從這些鏈接,我做了一個結論,即如果我們在Apache的HttpClient升級到最新的穩定版本,那麼這個問題就可以解決了。
但是這是不可能的,因爲Android團隊已經正式停止了對Apache httpclient的支持。
有了這個鏈接它可以解決。我沒有嘗試過,但我正在努力。
這是一個庫,可以幫助在Android中升級httpclient版本。
另一種解決方案可以使用HttpURLConnection的。我自己也是努力。
但是,大多數人在這裏的stackoverflow和互聯網似乎使用Android的DefaultHttpCLient。當然它也在我的整個應用程序中與我一起工作,包括登錄,註冊,從服務器讀取和會話等功能。只是它不直接將一些文章發佈到我的服務器Drupal網站。 它在服務器上的用戶註冊期間與POST請求完美協作。
那麼朋友們,對此有何建議?爲什麼它不僅僅是發佈文章?
我也看到了這個問題http://stackoverflow.com/questions/10407547/error-http-1-1-401-unauthorized-with-basic-authentication-in-android-ews-201? rq = 1&在我的代碼中嘗試了它...通過用戶名和密碼,但它仍然不起作用。仍然是相同的響應 - > 401 – 2012-08-09 08:17:35
嘗試http:// stackoverflow。com/questions/8022800/problems-doing-ajax-requests-with-a-phonegap-application – 2012-08-09 10:25:09
@ChanchalShelar我沒有遇到PhoneGap的問題......它完美地工作......我有Android Java應用程序的問題。所以在這裏我的問題是:相同的服務器與Phonegap協同工作,那麼爲什麼它不適用於Android。 – 2012-08-09 10:28:44