我试图编写一个方法来调用服务,并获取JSON数据回到Android应用程序。假设所有必要的gloable变量已经宣布...Android的JSON解析,无法连接到服务
这些都是使用的lib IM:
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;
这里是我的代码:
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;
}
它只是没有作用,我想运行在调试模式下,当它运行到这一行时:
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream(), "iso-8859-1"), 8);
它抱怨:java.net.UnknownHost例外:gdata.youtube.com 我不知道如何解决这个问题...任何建议? 感谢
顺便说一下,在我的Android清单,我有许可证的互联网已经:
如果你看看在浏览器中的网址,就可以看到TI的JSON格式...,它可以被连接...但我的代码只是抱怨unknowhostexception ....“http://gdata.youtube.com/feeds/api/users/eon/uploads?& v = 2 & max-results = 50 & alt = jsonc“;
你是通过移动/无线连接到互联网吗? – Bush