Hi all,
I want to save data to google base, but I failed to connect to the
google account client which is the first step that connect to google
base.
I found Andriod provides the same java.net package as PC platform's,
so I use this java.net to connect to google base. The following is my
code.
Could you please help me to investigate it? thanks a lot!
----the code that connect to google client-------
private static final String AUTHENTICATION_URL = "https://
www.google.com/accounts/ClientLogin";
String gmail = "";
String pws = "";
private String authenticate() {
// create the login request
String postOutput = null;
try {
URL url = new URL(AUTHENTICATION_URL);
postOutput = getToken(url);
} catch (Exception e) {
System.out.println("Could not connect to authentication
server: "
+ e.toString());
return null;
}
StringTokenizer tokenizer = new StringTokenizer(postOutput,
"=\n ");
String token = null;
while (tokenizer.hasMoreElements()) {
if (tokenizer.nextToken().equals("Auth")) {
if (tokenizer.hasMoreElements()) {
token = tokenizer.nextToken();
}
break;
}
}
return token;
}
private String getToken(URL url) throws IOException {
// Open connection
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
// Set properties of the connection
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// Form the POST parameters
StringBuilder content = new StringBuilder();
content.append("Email=").append(URLEncoder.encode(gmail,
"UTF-8"));
content.append("&Passwd=").append(URLEncoder.encode(pws,
"UTF-8"));
content.append("&service=").append(URLEncoder.encode("gbase",
"UTF-8"));
OutputStream outputStream = urlConnection.getOutputStream();
outputStream.write(content.toString().getBytes("UTF-8"));
outputStream.close();
// Retrieve the output
int responseCode = urlConnection.getResponseCode();
InputStream inputStream;
if (responseCode == HttpURLConnection.HTTP_OK) {
inputStream = urlConnection.getInputStream();
} else {
inputStream = urlConnection.getErrorStream();
}
return toString(inputStream);
}
-------------------------------
Notes: gmail,pws is your own google mail address and password
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---