我是 JSON Parsing 的新手,在从 JSON URL 检索数据时需要一些帮助......我已经阅读了许多关于 JSON Parsing 的教程,它们都对我有很大帮助,但我没有被卡住......我正在尝试从一个
URL = "http://mygogolfteetime.com/iphone/topfive/155"
但我在日志中Json object
返回空值。
但是我可以从另一个检索相同的数据
URL = "http://api.mygogolfteetime.com/top_5.php?id=155&apikey=eart3562@#bety7*&6b12zAf*&etrqvbn*&LMQW"
我还附上了我的代码,以便可以帮助我解决我的问题..
我的测试活动的代码:
public class TestActivity extends Activity {
private static final String url = "http://mygogolfteetime.com/iphone/topfive/155";
private static final String TAG = "Top5";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v(TAG, "URL: "+String.valueOf(url));
JsonParser jParse = new JsonParser();
JSONObject json = jParse.getJSONfromUrl(url);
Log.v(TAG, "Json: "+String.valueOf(json));
}
}
这是我的 JsonParser 活动的代码:
public class JsonParser {
static InputStream is;
static JSONObject jObj;
static String json = null;
private static final String TAG = "Top5";
public JSONObject getJSONfromUrl(String url)
{
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch(ClientProtocolException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
json = sb.toString();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
jObj = new JSONObject(json);
}
catch(JSONException e)
{
e.printStackTrace();
}
return jObj;
}
}
我还在 Manifest 文件中添加了 Internet 权限...
<uses-permission android:name="android.permission.INTERNET"/>
提前致谢...
对不起,我的英语不好。