Hi friends,
I want to save data to google base, but I always failed to connect to
google base.
I found that Andriod has also java.net.* package, so I think I can use
it just like the java.net package for PC, that's right?
Could you please help me to investigate it? thanks a lot!
--------the code that I connect google account,this is the first step
to connect google base:getToken() which failed to
connect--------------------------
private static final String AUTHENTICATION_URL = "https://
www.google.com/accounts/ClientLogin";
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 getTokenForPC(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 that your 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
-~----------~----~----~----~------~----~------~--~---