package com.exercise.AndroidGooglePlus;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidGooglePlusActivity extends Activity {
TextView item_post;
final static String GeogleApisUrl = "https://www.googleapis.com/plus/v1/";
final static String API_KEY = "<Your API key>";
final static String GooglePlusPublicPosts_url
= GeogleApisUrl
+ "activities?query=cookie%20recipes&orderBy=best"
+ "&key=" + API_KEY;
String[] PublicPosts = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
item_post = (TextView)findViewById(R.id.item_post);
String googlePlusResult = QueryGooglePlus(GooglePlusPublicPosts_url);
ParseGooglePlusJSON_PublicPosts(googlePlusResult);
}
String QueryGooglePlus(String queryString){
String qResult = null;
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(queryString);
try {
HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();
if (httpEntity != null){
InputStream inputStream = httpEntity.getContent();
Reader in = new InputStreamReader(inputStream);
BufferedReader bufferedreader = new BufferedReader(in);
StringBuilder stringBuilder = new StringBuilder();
String stringReadLine = null;
while ((stringReadLine = bufferedreader.readLine()) != null) {
stringBuilder.append(stringReadLine + "\n");
}
qResult = stringBuilder.toString();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
Toast.makeText(AndroidGooglePlusActivity.this,
e.toString(),
Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(AndroidGooglePlusActivity.this,
e.toString(),
Toast.LENGTH_LONG).show();
}
return qResult;
}
void ParseGooglePlusJSON_PublicPosts(String json){
if(json != null){
try {
JSONObject JsonObject = new JSONObject(json);
JSONArray JsonArray_items = JsonObject.getJSONArray("items");
PublicPosts = new String[JsonArray_items.length()];
for (int i = 0; i < JsonArray_items.length(); i++){
JSONObject post = JsonArray_items.getJSONObject(i);
PublicPosts[i] = post.getString("title");
}
String postList = "";
for (int i = 0; i < PublicPosts.length; i++){
postList += String.valueOf(i) + ": " + PublicPosts[i] + "\n"
+ "------------\n";
}
item_post.setText(postList);
Toast.makeText(AndroidGooglePlusActivity.this,
"finished",
Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(AndroidGooglePlusActivity.this,
e.toString(),
Toast.LENGTH_LONG).show();
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/item_post"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Note: Need permission of "android.permission.INTERNET" to access Google Plus online.
Download the files.
Ref:
- Google+ Platform Blog - Google+ APIs: Now With Search and More!
Related article:
- Query and Parse Google+ JSON
I have to send a comment/ or new post in google Plus.
ReplyDeleteHow? Any idea.
Thanks in advance
Hey, nice tutorial.. But can you tell me from where/how to get API KEY for integration of google+?
ReplyDeleteCan you post here steps for that.. Thanks in advance.
Mansi.
hello PRASHANT,
ReplyDeleteI have no idea right now, sorry!
hello m@n!,
ReplyDeletePlease refer Apply your own Google+ API key.
How to fetch public activities only posted by my friends or which is showing in my wall?
ReplyDelete