forked from novoda/android-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a simple example originally modified from javacodegeeks.com
- Loading branch information
Kevin McDonagh
committed
Jan 5, 2012
1 parent
053d7ca
commit b072305
Showing
10 changed files
with
505 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:versionCode="1" | ||
android:versionName="1.0" package="com.novoda"> | ||
|
||
<application android:icon="@drawable/icon" android:label="@string/app_name"> | ||
<activity android:name="com.novoda.activity.JsonRequest" | ||
android:label="@string/app_name"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
|
||
</application> | ||
|
||
<uses-sdk android:minSdkVersion="3" /> | ||
<uses-permission android:name="android.permission.INTERNET"></uses-permission> | ||
|
||
</manifest> |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?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:id="@+id/search" | ||
android:layout_width="fill_parent" | ||
android:layout_height="wrap_content" /> | ||
<ListView | ||
android:id="@+id/list" | ||
android:layout_width="fill_parent" | ||
android:layout_height="fill_parent" /> | ||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<string name="hello">Hello World, JsonParsingActivity!</string> | ||
<string name="app_name">JsonParser</string> | ||
</resources> |
89 changes: 89 additions & 0 deletions
89
GsonJsonWebservice/src/com/novoda/activity/JsonRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.novoda.activity; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import org.apache.http.HttpEntity; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.HttpStatus; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.impl.client.DefaultHttpClient; | ||
|
||
import android.app.Activity; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.widget.ArrayAdapter; | ||
import android.widget.ListView; | ||
import android.widget.SimpleAdapter; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import com.google.gson.Gson; | ||
import com.novoda.R; | ||
import com.novoda.model.Result; | ||
import com.novoda.model.SearchResponse; | ||
|
||
public class JsonRequest extends Activity { | ||
|
||
private static final String SEARCH = "Droidcon"; | ||
String url = "http://search.twitter.com/search.json?q=" + SEARCH; | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.main); | ||
((TextView)findViewById(R.id.search)).setText("Searching for: " + SEARCH); | ||
} | ||
|
||
@Override | ||
protected void onResume() { | ||
super.onResume(); | ||
Toast.makeText(this, "Querying droidcon on Twitter", Toast.LENGTH_SHORT).show(); | ||
|
||
Reader reader = new InputStreamReader(retrieveStream(url)); | ||
SearchResponse response = new Gson().fromJson(reader, SearchResponse.class); | ||
|
||
List<String> searches = new ArrayList<String>(); | ||
Iterator<Result> i = response.results.iterator(); | ||
while(i.hasNext()){ | ||
Result res = (Result )i.next(); | ||
searches.add(res.text); | ||
} | ||
|
||
ListView v = (ListView)findViewById(R.id.list); | ||
v.setAdapter(new ArrayAdapter<String>(this, | ||
android.R.layout.simple_list_item_1, searches.toArray(new String[searches.size()]))); | ||
} | ||
|
||
private InputStream retrieveStream(String url) { | ||
|
||
DefaultHttpClient client = new DefaultHttpClient(); | ||
HttpGet getRequest = new HttpGet(url); | ||
|
||
try { | ||
|
||
HttpResponse getResponse = client.execute(getRequest); | ||
final int statusCode = getResponse.getStatusLine().getStatusCode(); | ||
|
||
if (statusCode != HttpStatus.SC_OK) { | ||
Log.w(getClass().getSimpleName(), "Error " + statusCode + " for URL " + url); | ||
return null; | ||
} | ||
|
||
HttpEntity getResponseEntity = getResponse.getEntity(); | ||
return getResponseEntity.getContent(); | ||
|
||
} | ||
catch (IOException e) { | ||
getRequest.abort(); | ||
Log.w(getClass().getSimpleName(), "Error for URL " + url, e); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.novoda.model; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
public class Metadata { | ||
|
||
@SerializedName("result_type") | ||
public String resultType; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.novoda.model; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
public class Result { | ||
|
||
@SerializedName("from_user_id_str") | ||
public String fromUserIdStr; | ||
|
||
@SerializedName("profile_image_url") | ||
public String profileImageUrl; | ||
|
||
@SerializedName("created_at") | ||
public String createdAt; | ||
|
||
@SerializedName("from_user") | ||
public String fromUser; | ||
|
||
@SerializedName("id_str") | ||
public String idStr; | ||
|
||
public Metadata metadata; | ||
|
||
@SerializedName("to_user_id") | ||
public String toUserId; | ||
|
||
public String text; | ||
|
||
public long id; | ||
|
||
@SerializedName("from_user_id") | ||
public String from_user_id; | ||
|
||
@SerializedName("iso_language_code") | ||
public String isoLanguageCode; | ||
|
||
@SerializedName("to_user_id_str") | ||
public String toUserIdStr; | ||
|
||
public String source; | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
GsonJsonWebservice/src/com/novoda/model/SearchResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.novoda.model; | ||
|
||
import java.util.List; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
public class SearchResponse { | ||
|
||
public List<Result> results; | ||
|
||
@SerializedName("max_id") | ||
public long maxId; | ||
|
||
@SerializedName("since_id") | ||
public int sinceId; | ||
|
||
@SerializedName("refresh_url") | ||
public String refreshUrl; | ||
|
||
@SerializedName("next_page") | ||
public String nextPage; | ||
|
||
@SerializedName("results_per_page") | ||
public int resultsPerPage; | ||
|
||
public int page; | ||
|
||
@SerializedName("completed_in") | ||
public double completedIn; | ||
|
||
@SerializedName("since_id_str") | ||
public String sinceIdStr; | ||
|
||
@SerializedName("max_id_str") | ||
public String maxIdStr; | ||
|
||
public String query; | ||
|
||
} |
Oops, something went wrong.