Android应用程序 - 通过HTTP POST谷歌授权 [英] Android application - Google Authorization via HTTP Post
问题描述
我只是想通过HTTP POST发送给我的谷歌帐户的认证数据。我已经建立了HTTPpost(URLen codeD ArrayList的名称 - 值对),并执行HttpClient的得到HTT presponse。然而,这是问题开始的地方,在HTT presponse我回去似乎异常随时我尝试调用它的相关方法(getStatusLine或getEntity)一回。我也试图检查空的反应,做一个简单的if(空)其他类型检查,但仍没有运气。
I am just trying to send my Google account authentication data via HTTP POST. I have built the HTTPpost (URLencoded the ArrayList name - value pair) and executed the HTTPClient to get the HTTPResponse. However this is where the problem starts, the HTTPResponse I get back seems to return an exception anytime I try to call one of its associated methods (getStatusLine or getEntity). I also tried to check for "null" response, by doing a simple "if (null) else" type checking, but still no luck.
这是问题,因为我使用的模拟器?
Is this problem because I am using the emulator?
-----更新-----
我还发现,我得到一个空指针的反应,从而导致该异常。所以,存在一个问题,我访问谷歌的API的方式。网址为https://www.google.com/accounts/ClientLogin和电子邮件和的passwd是两个参数,我使用POST请求。
I have found out that I am getting a NULL Pointer response, which causes the exception. So, there is an issue with the way I am accessing the Google API. The URL is "https://www.google.com/accounts/ClientLogin" and "Email" and "Passwd" are the two parameters I use for the POST request.
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使用的Htt prequest和放大器;也u必须使用一个名为的Base64及放库; Android版本2.1及放大器;上述
Instead using httpPost use HttpRequest & also u have to use a library called Base64 & Android version 2.1 &above
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);
这篇关于Android应用程序 - 通过HTTP POST谷歌授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!