Java Android项目中的JSON问题

Java Android项目中的JSON问题,java,android,json,http,Java,Android,Json,Http,我正在胡乱处理本教程中的代码: 我下载了源代码,并成功地在我的项目中设置了所有内容。我启动了我的应用程序,按下了“获取JSON”按钮,一切正常 他们教程中使用的URL为 来自该页面的JSON如下所示: [{"dept":"Mobile","dept_id":"1"},{"dept":"Web","dept_id":"2"}] 我想使用我自己的URL,所以我更改了他们给我的URL。我还更改了选择器以匹配新链接 新URL为,这是它在浏览器中返回的内容: {"version":"1.0","topA

我正在胡乱处理本教程中的代码:

我下载了源代码,并成功地在我的项目中设置了所有内容。我启动了我的应用程序,按下了“获取JSON”按钮,一切正常

他们教程中使用的URL为

来自该页面的JSON如下所示:

[{"dept":"Mobile","dept_id":"1"},{"dept":"Web","dept_id":"2"}]
我想使用我自己的URL,所以我更改了他们给我的URL。我还更改了选择器以匹配新链接

新URL为,这是它在浏览器中返回的内容:

{"version":"1.0","topAdds":{"week":"12","player":[{"percent":"41.65","id":"8827"},{"percent":"36.89","id":"8752"},{"percent":"25.60","id":"0519"},{"percent":"16.35","id":"5730"},{"percent":"14.14","id":"6562"},{"percent":"13.56","id":"7547"},{"percent":"10.34","id":"3869"},{"percent":"9.87","id":"5660"},{"percent":"8.75","id":"7553"},{"percent":"8.36","id":"7084"},{"percent":"8.12","id":"7480"},{"percent":"8.05","id":"4907"},{"percent":"7.10","id":"6616"},{"percent":"6.85","id":"7625"},{"percent":"6.66","id":"3494"},{"percent":"6.56","id":"8117"},{"percent":"6.11","id":"7071"},{"percent":"5.91","id":"4092"},{"percent":"5.86","id":"7399"},{"percent":"5.78","id":"6715"},{"percent":"4.68","id":"4932"},{"percent":"4.49","id":"3969"},{"percent":"4.46","id":"7837"},{"percent":"4.38","id":"7895"},{"percent":"4.24","id":"7021"},{"percent":"4.13","id":"0525"},{"percent":"4.05","id":"5666"},{"percent":"3.86","id":"6528"},{"percent":"3.81","id":"0512"},{"percent":"3.70","id":"3875"},{"percent":"3.68","id":"4984"},{"percent":"3.67","id":"8135"},{"percent":"3.48","id":"5482"},{"percent":"3.32","id":"1653"},{"percent":"3.29","id":"8244"},{"percent":"3.20","id":"0511"},{"percent":"2.76","id":"3369"},{"percent":"2.73","id":"5065"},{"percent":"2.72","id":"6786"},{"percent":"2.70","id":"8326"},{"percent":"2.66","id":"7150"},{"percent":"2.65","id":"7034"},{"percent":"2.64","id":"3336"},{"percent":"2.59","id":"6950"},{"percent":"2.54","id":"8685"},{"percent":"2.48","id":"8500"},{"percent":"2.44","id":"8339"},{"percent":"2.38","id":"0507"},{"percent":"2.35","id":"8074"},{"percent":"2.34","id":"7883"},{"percent":"2.27","id":"8667"},{"percent":"2.21","id":"8742"},{"percent":"2.18","id":"0502"},{"percent":"2.15","id":"7740"},{"percent":"2.12","id":"7472"},{"percent":"2.11","id":"3298"},{"percent":"2.02","id":"9008"},{"percent":"2.01","id":"6743"},{"percent":"2.01","id":"6643"}]},"encoding":"ISO-8859-1"}
现在,当我启动应用程序并单击“获取JSON”按钮时,我得到的是“Success[]”,而不是“Success-Ex:Data…-”,并且与之前一样,没有数据列表出现

我应该怎么做才能使新的URL工作

以下是我正在使用的代码:

MyFantasyLeagueActivity.java

public final static String BaseUrl="http://football.myfantasyleague.com/"; 

ArrayList<DEPT_HOLD> deptList=new ArrayList<DEPT_HOLD>();

private class GetDeptAyncTask extends AsyncTask<Hashtable<String,String>,Void,String>{

      @Override
      protected String doInBackground(Hashtable<String,String>... params) {
       Hashtable ht=params[0];

       String json=HelperHttp.getJSONResponseFromURL(BaseUrl+"2007/export?TYPE=topAdds&W=12&JSON=1", ht);
       if(json!=null) parseJsonString(deptList,json);
       else{
        return "Invalid Company Id";
       }
       return "SUCCESS";
      }

      protected void parseJsonString(ArrayList<DEPT_HOLD> deptList,String json){
          try {
               JSONArray array=new JSONArray(json);
               for(int i=0;i<array.length();i++){
                JSONObject j=array.getJSONObject(i);
                DEPT_HOLD d=new DEPT_HOLD();
                d.one=j.optString("percent","");
                d.two=j.optString("id","");
                deptList.add(d);
               }

              } catch (JSONException e) {
               e.printStackTrace();
              }

      } 


      @Override
      protected void onPostExecute(String result){

          if(result=="SUCCESS")
          {
           Toast.makeText(MyFantasyLeagueActivity.this, "Success"+deptList, Toast.LENGTH_SHORT).show();
           DeptArrayAdapter adapter=new DeptArrayAdapter(MyFantasyLeagueActivity.this,R.id.text1,deptList);
           ListView listv=(ListView)findViewById(R.id.lv);
           listv.setAdapter(adapter);
          }
          else{}
      }

}
public最终静态字符串BaseUrl=”http://football.myfantasyleague.com/"; 
ArrayList deptList=新的ArrayList();
私有类GetDeptAyncTask扩展异步任务{
@凌驾
受保护字符串doInBackground(哈希表…参数){
哈希表ht=参数[0];
字符串json=HelperHttp.getJSONResponseFromURL(BaseUrl+“2007/export?TYPE=topAdds&W=12&json=1”,ht);
if(json!=null)parseJsonString(deptList,json);
否则{
返回“公司Id无效”;
}
返回“成功”;
}
受保护的void parseJsonString(ArrayList deptList,String json){
试一试{
JSONArray数组=新的JSONArray(json);

对于(int i=0;i原始JSON在顶层有一个数组,但您的新JSON是一个对象。因此,在您的
parseJsonString
方法中,您首先需要从数据中创建一个
JSONObject
。然后需要钻取
topAdds
player
属性,以获得百分比/id对的数组。没有错误检查:

protected void parseJsonString(ArrayList<DEPT_HOLD> deptList,String json){
    try {
        JSONObject top = new JSONObject(json);
        JSONObject topAdds = (JSONObject) top.get("topAdds");
        JSONArray array = (JSONArray) topAdds.get("player");

        for(int i = 0; i < array.length(); i++) {
            JSONObject j = array.getJSONObject(i);
            DEPT_HOLD d = new DEPT_HOLD();
            d.one = j.optString("percent","");
            d.two = j.optString("id","");
            deptList.add(d);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
} 
protectedvoid-parseJsonString(ArrayList-deptList,String-json){
试一试{
JSONObject top=新的JSONObject(json);
jsonobjecttopadds=(JSONObject)top.get(“topAdds”);
JSONArray数组=(JSONArray)topAdds.get(“player”);
对于(int i=0;i
原始JSON在顶层有一个数组,但您的新JSON是一个对象。因此,在您的
parseJsonString
方法中,您首先需要从数据中创建一个
JSONObject
。然后您需要钻取
topAdds
player
属性以获得百分比/id对的数组。没有error检查:

protected void parseJsonString(ArrayList<DEPT_HOLD> deptList,String json){
    try {
        JSONObject top = new JSONObject(json);
        JSONObject topAdds = (JSONObject) top.get("topAdds");
        JSONArray array = (JSONArray) topAdds.get("player");

        for(int i = 0; i < array.length(); i++) {
            JSONObject j = array.getJSONObject(i);
            DEPT_HOLD d = new DEPT_HOLD();
            d.one = j.optString("percent","");
            d.two = j.optString("id","");
            deptList.add(d);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
} 
protectedvoid-parseJsonString(ArrayList-deptList,String-json){
试一试{
JSONObject top=新的JSONObject(json);
jsonobjecttopadds=(JSONObject)top.get(“topAdds”);
JSONArray数组=(JSONArray)topAdds.get(“player”);
对于(int i=0;i
本教程中的只是一个简单的JSON数组。但你的数组没有那么复杂。因此,我认为你可能需要检查你的JSON解析器。在你的代码中硬编码这个JSON字符串,并检查你是否解析了它。只发布相关数据,而不是整个项目。如果你收到正确的数据,JSON解析器位应该足够了(你确实检查过了,对吗?)本教程中的一个只是一个简单的JSON数组。但是你的数组没有那么复杂。因此,我认为你可能需要检查你的JSON解析器。在你的代码中硬编码这个JSON字符串,并检查你是否解析了它。只发布相关数据,而不是整个项目。如果你收到正确的数据,JSON解析器位应该足够了(你确实检查过了,对吗?)