How to connect to google base?

by kingfu » Sat, 29 Nov 2008 16:30:48 GMT


Sponsored Links
 i 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);
}
------------------------------------------------
Notesmail,pws is that



How to connect to google base?

by kingfu » Sat, 29 Nov 2008 16:30:50 GMT


 i 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


Sponsored Links