java - Android JSON 解析器

标签 java android json

我正在尝试解析位于 http://api.pathofexile.com/ladders/Default?offset=0&limit=1 的 JSON .

{
"total": 15000,
"entries": [
    {
        "online": false,
        "rank": 1,
        "character": {
            "name": "Byrr",
            "level": 85,
            "class": "Shadow",
            "experience": 1397076236
        },
        "account": {
            "name": "Canoobians"
        }
    }
]
}

我一直在关注androidhive tutorial同时尝试修改它以检索“在线”和“排名”元素。 (最终我想要所有具有大量条目的元素,但我只是从这两个元素开始尝试理解事物。

public class AndroidJSONParsingActivity extends ListActivity {

// url to make request
private static String url = "http://api.pathofexile.com/ladders/Default?offset=0&limit=2";

// JSON node names
private static final String TAG_ENTRIES = "entries";
private static final String TAG_ONLINE = "online";
private static final String TAG_RANK = "rank";

// entries JSONArray
JSONArray entries = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> entriesList = new ArrayList<HashMap<String, String>>();

    // create JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from url
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting Array of entries
        entries = json.getJSONArray(TAG_ENTRIES);

        // looping through entries
        for (int i = 0; i < entries.length(); i++) {
            JSONObject ent = entries.getJSONObject(i);

            // storing each JSON item in a variable
            String online = ent.getString(TAG_ONLINE);
            String rank = ent.getString(TAG_RANK);

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put(TAG_ONLINE, online);
            map.put(TAG_RANK, rank);

            // adding HashList to ArrayList
            entriesList.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    // Updating parsed JSON data into ListView
    ListAdapter adapter = new SimpleAdapter(this, entriesList, R.layout.list_item, new String[] { TAG_ONLINE, TAG_RANK }, new int[] { R.id.online, R.id.rank });
    setListAdapter(adapter);

我的 JSONParser() 类与教程中的相同。现在,当我运行该程序时,出现错误:

Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject.

我不知道为什么会发生这个错误,因为根据 JSONLint,JSON 是有效的,所以它不应该发送任何 HTML,对吗?是否有我遗漏的东西,或者甚至是完全不同/更好的提取 JSON 的方法?任何朝着正确方向的努力将不胜感激。

编辑:因为我是新用户,所以我还无法 self 回答,但事实证明我在 JSONParser( ) 我以前没有看到过,并且使用 HttpGet() 而不是 HttpPost() 解决了我的问题。

谢谢。

最佳答案

查看 JSONParser 中的这一行

  BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "iso-8859-1"), 8);

网站返回此 header

Content-Type: text/html; charset=UTF-8

您必须在 BufferedReader 中将 iso-8859-1 更改为 UTF-8。

关于java - Android JSON 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14666728/

相关文章:

java - Intent 无法从应用程序内获得

android - Crosswalk Cordova - 单击延迟未由 ontouchstart 修复

android - 如何使用 BroadcastReceiver 更新两个 Activity

java - 如何使用Java播放wav文件?

java - 对 HashSet 使用 float loadfactor 有什么影响?

c# - JSON 到 C# 类 - 未知的属性名称

mysql - 如何解析json对象 - mysql

javascript - 访问嵌套 json 对象时的值替换

java - JDBC 模板 - 一对多

java - 如何使用 onClick for android 从按钮生成随机类结果?