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


Other Threads

1. feedr

rr, sejak 2hr feedr tdk bisa load feed baru..sdh refresh & uninstall,
install lg jg tdk bs..ada yg alami jg? semua feed address tdk bisa
load..kira2 masalah & solusinya apa ya?

tia

-- 
"Indonesian Android Community [id-android]" 

2. google checkout

RR,
Apa ada yg bisa informasikan bgaimana menggunakan google checkout? sy sdh
buat google accounts juga. Namun pada saat mau beli apps berbayar di market
( menggunakan market enabler), ternyata tidak ada Indonesia disitu, apa sy
isi dgn fake country aja? let's say USA gt? apa nanti pada saat dilihat
no.Credit card saya terdaftar di Indo, tetap akan bisa?

Mhn infonya...TIA

-- 
"Indonesian Android Community [id-android]" 

3. How to clean DDMS view of the logs though ant script

4. FroYo Alsa issues

5. boot speed -- scan package, IO buffering, and prefetch

6. Questions for the group regarding URL Launchers

7. download android 2.2 failed because of "File too large6"