1

我正在处理本教程中的代码:http: //mobisys.in/blog/2012/01/parsing-json-from-url-in-android/

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

他们教程中使用的 URL 是http://mobisys.in/quicknotify/get_departments.php?company_id=1

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

[{"dept":"Mobile","dept_id":"1"},{"dept":"Web","dept_id":"2"}]

我想使用我自己的 URL,所以我将他们提供的 URL 更改为我自己的。我还更改了选择器以匹配新链接。

新的 URL 是http://football.myfantasyleague.com/2007/export?TYPE=topAdds&W=12&JSON=1这就是它在浏览器中返回的内容:

{"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{}
      }

}

我确信解决方案是显而易见的,我会觉得自己很愚蠢,但我花了太多时间看这个。我需要一双新的眼睛来帮助我弄清楚发生了什么。

有什么建议么?

4

1 回答 1

1

原始 JSON 在顶层有一个数组,但您的新 JSON 是一个对象。所以在你的parseJsonString方法中,你首先需要创建JSONObject出数据。然后,您需要深入研究topAddsandplayer属性以获取百分比/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();
    }
} 
于 2013-03-12T21:59:18.867 回答