i m trying to write a method for calling service and get JSON data back in Android application. assume all necessary gloable variables are declared already...
these are the lib i m using:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
here is my code:
public class JSONParser {
String url = "http://gdata.youtube.com/feeds/api/users/eon/uploads?&v=2&max- results=50&alt=jsonc";
JSONObject jObj = null;
JSONArray ja = null;
String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url){
try {
URL urlGetter = new URL(url);
URLConnection tc = urlGetter.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream(), "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = in.readLine()) != null) {
sb.append(line + "\n");
}
json = sb.toString();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
it just dose not work, i tried to run in debug mode, when it run to this line:
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream(), "iso-8859-1"), 8);
it complains about : java.net.UnknownHostException: gdata.youtube.com i don't know how to solve this problem... any suggestions? thanks
by the way, in my android manifest, i have permit internet already:
if you look up the url in browser, you can see the json format of ti ...and it can be connected...but my code just complaining about unknowhostexception...."http://gdata.youtube.com/feeds/api/users/eon/uploads?&v=2&max- results=50&alt=jsonc";