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. getting mainmenu applications

How can I get the list of all applications present in mainmenu and
launch them?
--~--~---------~--~----~------------~-------~--~----~

2. Can we use static class that holds all the constants for the applications?

In the last example given, it would be better to define strings like that in
an xml file as the Dev Guide suggests.





Yes it is actually better to use static classes for your constants.
e.g.

class C {
       public static final String PREFS_NAME = "MyPrefsFile";
       public static final String PREFS_FAVS = "MyFavs";
       public static final String PREFS_RECENT = "MyRecents";
}

so can use it from any other activity as follows:

SharedPreferences settings = this.getSharedPreferences(C.PREFS_NAME,
0);
String recent = settings.getString(C.PREFS_RECENT, defaultValue);

--~--~---------~--~----~------------~-------~--~----~ You received this
message because you are su...

--~--~---------~--~----~------------~-------~--~----~

3. Real Estate & SEO

4. Making UPNP (Universal Plug and Play) Protocol work for Android

5. Why there is no log message output in the logcat after installed SDK 1.6

6. Non-Market download problems.

7. How to have a horizontal scroll view with multiple buttons?