download: http://www.mediafire.com/download/mwcn2dxyj8znsk5/JSONParsingArray.zip
package com.androidhive.jsonparsing;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.os.Bundle;
public class AndroidJSONParsingActivity extends ListActivity {
// url to make request
static String urlSong = "http://api.lyricsnmusic.com/songs?api_key=e6ea07de4c713953d0fdc9ad49b861&q=Coldplay%20Clocks";
// e6ea07de4c713953d0fdc9ad49b861
// JSON Node names
// contacts JSONArray
JSONArray contacts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hashmap for ListView
String parsedString = "";
try {
parsedString = convertinputStreamToString(urlSong);
} catch (Exception e) {
e.printStackTrace();
}
// String str =
// "[{\"name\":\"name1\",\"url\":\"url1\"},{\"name\":\"name2\",\"url\":\"url2\"}]";
String str = parsedString;
JSONArray jsonarray;
try {
jsonarray = new JSONArray(str);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject obj = jsonarray.getJSONObject(i);
String name = obj.getString("title");
// String url = obj.getString("url");
System.out.println("i="+i+" "+name);
// System.out.println(url);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String convertinputStreamToString(String urlSong)
throws IOException {
URL url = new URL(urlSong);
URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
InputStream is = httpConn.getInputStream();
if (is != null) {
StringBuilder sb = new StringBuilder();
String line;
try {
BufferedReader r1 = new BufferedReader(new InputStreamReader(
is, "UTF-8"));
while ((line = r1.readLine()) != null) {
sb.append(line).append("\n");
}
} finally {
is.close();
}
return sb.toString();
} else {
return "";
}
}
}
manifest
_____________
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidhive.jsonparsing"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".AndroidJSONParsingActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Single List Item View -->
<activity
android:label="Single Menu Item"
android:name=".SingleMenuItemActivity" >
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
package com.androidhive.jsonparsing;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.os.Bundle;
public class AndroidJSONParsingActivity extends ListActivity {
// url to make request
static String urlSong = "http://api.lyricsnmusic.com/songs?api_key=e6ea07de4c713953d0fdc9ad49b861&q=Coldplay%20Clocks";
// e6ea07de4c713953d0fdc9ad49b861
// JSON Node names
// contacts JSONArray
JSONArray contacts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hashmap for ListView
String parsedString = "";
try {
parsedString = convertinputStreamToString(urlSong);
} catch (Exception e) {
e.printStackTrace();
}
// String str =
// "[{\"name\":\"name1\",\"url\":\"url1\"},{\"name\":\"name2\",\"url\":\"url2\"}]";
String str = parsedString;
JSONArray jsonarray;
try {
jsonarray = new JSONArray(str);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject obj = jsonarray.getJSONObject(i);
String name = obj.getString("title");
// String url = obj.getString("url");
System.out.println("i="+i+" "+name);
// System.out.println(url);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String convertinputStreamToString(String urlSong)
throws IOException {
URL url = new URL(urlSong);
URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
InputStream is = httpConn.getInputStream();
if (is != null) {
StringBuilder sb = new StringBuilder();
String line;
try {
BufferedReader r1 = new BufferedReader(new InputStreamReader(
is, "UTF-8"));
while ((line = r1.readLine()) != null) {
sb.append(line).append("\n");
}
} finally {
is.close();
}
return sb.toString();
} else {
return "";
}
}
}
manifest
_____________
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidhive.jsonparsing"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".AndroidJSONParsingActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Single List Item View -->
<activity
android:label="Single Menu Item"
android:name=".SingleMenuItemActivity" >
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
No comments:
Post a Comment