[英]Android application - Google Authorization via HTTP Post
我只是想通過HTTP POST發送我的Google帳戶身份驗證數據。 我已經構建了HTTPpost(URLencoded the ArrayList name - value pair)並執行HTTPClient來獲取HTTPResponse。 然而,這是問題開始的地方,我回來的HTTPResponse似乎在我嘗試調用其關聯方法之一時返回異常(getStatusLine或getEntity)。 我還試圖通過做一個簡單的“if(null)else”類型檢查來檢查“null”響應,但仍然沒有運氣。
這個問題是因為我正在使用模擬器嗎?
----- -----更新
我發現我得到一個NULL指針響應,這會導致異常。 因此,我訪問Google API的方式存在問題。 網址為“https://www.google.com/accounts/ClientLogin”,“Email”和“Passwd”是我用於POST請求的兩個參數。
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("<URL HERE>");
try {
List<NameValuePair> parameters = new ArrayList<NameValuePair>(2);
parameters.add(<name_value_pair>);
parameters.add(<name_value_pair>);
httppost.setEntity(new UrlEncodedFormEntity(parameters));
HttpResponse response = httpclient.execute(httppost);
StatusLine returned_status = response.getStatusLine();
int status_code = returned_status.getStatusCode();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
相反,使用httpPost使用HttpRequest,你也必須使用一個名為Base64和Android 2.1及以上版本的庫
String data;
HttpParams httpParameters;
HttpClient client;
HttpResponse response;
String userAuth;
httpParameters = new BasicHttpParams();
String auth = android.util.Base64.encodeToString(
(username + ":" + userpwd).getBytes("UTF-8"),
android.util.Base64.NO_WRAP
);
HttpGet request = new HttpGet(StaticURL.uMain+resourceURI);
request.addHeader("Authorization", "Basic "+ auth);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutConnection);
client = new DefaultHttpClient(httpParameters);
response = client.execute(request);
userAuth = EntityUtils.toString(response.getEntity());
System.out.println("Data. in login.."+userAuth);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.