2

我对 Android 开发人员相当陌生,正在尝试编写一个程序来解析来自网站的一些 JSON 并将其输出到 ListView 中。但是,当我运行我的程序时,我得到了错误:

05-24 05:37:41.524: E/JSON Parser(783): Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONArray

这真的很奇怪,因为我的 JSON 页面中似乎甚至没有 xml 标头。我试图解析的 JSON 是:

[{"codeField":"COMPSCI 101","semesterField":"Summer School; Semester 1; Semester 2","titleField":"Principles of Programming"},{"codeField":"COMPSCI 105","semesterField":"Summer School; Semester 1; Semester 2","titleField":"Principles of Computer Science"},{"codeField":"COMPSCI 111\/111G","semesterField":"Summer School; Semester 1; Semester 2","titleField":"Mastering Cyberspace: An Introduction to Practical Computing"},{"codeField":"COMPSCI 210","semesterField":"Semester 1; Semester 2","titleField":"Computer Systems 1"},{"codeField":"COMPSCI 215","semesterField":"Semester 2","titleField":"Computer Systems 2"},{"codeField":"COMPSCI 220","semesterField":"Semester 2","titleField":"Algorithms and data structures"},{"codeField":"COMPSCI 225","semesterField":"Semester 1; Semester 2","titleField":"Discrete Structures in Mathematics and Computer Science"},{"codeField":"COMPSCI 230","semesterField":"Semester 1","titleField":"Software Design and Construction"},{"codeField":"COMPSCI 230","semesterField":"Semester 2","titleField":"Software Construction"}]

我知道,空白很糟糕,但它是我无法更改的外部来源。

我的代码:

从主要活动:

public class AndroidJSONParsingActivity extends ListActivity {

private static String url = "http://redsox.tcs.auckland.ac.nz/734A/CSService.svc/courses";

private static final String TAG_CODE = "codeField";
private static final String TAG_SEMESTER = "semesterField";
private static final String TAG_TITLE = "titleField";

JSONArray courses = null;

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

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

    JSONParser jParser = new JSONParser();
    courses = jParser.getJSONfromURL(url);

    try {
        for(int i = 0; i < courses.length(); i++){
            JSONObject c = courses.getJSONObject(i);

            // Storing each json item in variable
            String code = c.getString(TAG_CODE);
            String semester = c.getString(TAG_SEMESTER);
            String title = c.getString(TAG_TITLE);

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

            // adding each child node to HashMap key => value
            map.put(TAG_CODE, code);
            map.put(TAG_SEMESTER, semester);
            map.put(TAG_TITLE, title);

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


    //Updating parsed JSON data into ListView
    ListAdapter adapter = new SimpleAdapter(this, courseList,
            R.layout.list_item,
            new String[] { TAG_CODE, TAG_SEMESTER, TAG_TITLE }, new int[] {
                    R.id.code, R.id.semester, R.id.title });

    setListAdapter(adapter);

在 JSONParser 类中:

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    static JSONArray jArray = null;

    public JSONParser() {

    }

    public JSONArray 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) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // return JSON String
        try {
            jArray = new JSONArray(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        return jArray;

    }
}

编辑:完整的 logcat 是:

05-24 06:48:55.347: D/dalvikvm(787): GC_CONCURRENT freed 68K, 8% free 2736K/2948K, paused 6ms+34ms, total 115ms
05-24 06:48:55.547: E/JSON Parser(787): Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONArray
05-24 06:48:55.547: D/AndroidRuntime(787): Shutting down VM
05-24 06:48:55.547: W/dalvikvm(787): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-24 06:48:55.577: E/AndroidRuntime(787): FATAL EXCEPTION: main
05-24 06:48:55.577: E/AndroidRuntime(787): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.jsonparsing/com.androidhive.jsonparsing.AndroidJSONParsingActivity}: java.lang.NullPointerException
05-24 06:48:55.577: E/AndroidRuntime(787):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-24 06:48:55.577: E/AndroidRuntime(787):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-24 06:48:55.577: E/AndroidRuntime(787):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-24 06:48:55.577: E/AndroidRuntime(787):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-24 06:48:55.577: E/AndroidRuntime(787):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 06:48:55.577: E/AndroidRuntime(787):  at android.os.Looper.loop(Looper.java:137)
05-24 06:48:55.577: E/AndroidRuntime(787):  at android.app.ActivityThread.main(ActivityThread.java:5041)
05-24 06:48:55.577: E/AndroidRuntime(787):  at java.lang.reflect.Method.invokeNative(Native Method)
05-24 06:48:55.577: E/AndroidRuntime(787):  at java.lang.reflect.Method.invoke(Method.java:511)
05-24 06:48:55.577: E/AndroidRuntime(787):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-24 06:48:55.577: E/AndroidRuntime(787):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-24 06:48:55.577: E/AndroidRuntime(787):  at dalvik.system.NativeStart.main(Native Method)
05-24 06:48:55.577: E/AndroidRuntime(787): Caused by: java.lang.NullPointerException
05-24 06:48:55.577: E/AndroidRuntime(787):  at com.androidhive.jsonparsing.AndroidJSONParsingActivity.onCreate(AndroidJSONParsingActivity.java:44)
05-24 06:48:55.577: E/AndroidRuntime(787):  at android.app.Activity.performCreate(Activity.java:5104)
05-24 06:48:55.577: E/AndroidRuntime(787):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-24 06:48:55.577: E/AndroidRuntime(787):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-24 06:48:55.577: E/AndroidRuntime(787):  ... 11 more
4

4 回答 4

1

Alhumdulillah.我终于解决了你的问题..

您正在POST向此 url 发出请求,但它是不允许的。您应该将 HTTP 请求更改为键入GET

只需访问http://http-headers.online-domain-tools.com/

并检查 POST AND GET

在 POST 你得到这个回复:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Service</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Service</p>
      <p xmlns="">Method not allowed. Please see the <a rel="help-page" href="http://redsox.tcs.auckland.ac.nz/734A/CSService.svc/help">service help page</a> for constructing valid requests to the service.</p>
    </div>
  </body>
</html>

这就是您收到该错误的原因!

因此,要么您允许从服务器发布,要么您可以在JSONParser类中进行一些更改以更改HttpPostHttpGet. 看这里

于 2013-05-24T08:38:19.670 回答
0

您只需在 json 解析器类中创建一个 json 数组。然后你尝试获取一个 json 对象。尝试

 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) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    return jObj;

对于 json 解析器类,当您获取它时(在运行时),如下所示:

 class LoadAllProducts extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ProductsView.this);
        pDialog.setMessage(resStrings.get(0));
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        List<NameValuePair> params1 = new ArrayList<NameValuePair>();
        JSONObject json = jParser.makeHttpRequest(UrlList.url_all_products, "GET", params1);
        Log.d(resStrings.get(1), json.toString());
        int success = 0;
        try {
            success = json.getInt(TagList.TAG_SUCCESS);
            if (success == 1) {
                final JSONArray products = json.getJSONArray(TagList.TAG_PRODUCTS);
                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);
                    String id = c.getString(TagList.TAG_PID);
                    String name = c.getString(TagList.TAG_PNAME);
                    String quantity = c.getString(TagList.TAG_PQUANTITY);
                    String price = c.getString(TagList.TAG_PPRICE);
                    String mod = c.getString(TagList.TAG_PMOD);

                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TagList.TAG_PID, id);
                    map.put(TagList.TAG_PNAME, name);
                    map.put(TagList.TAG_PMOD, mod);
                    map.put(TagList.TAG_PQUANTITY, quantity);
                    map.put(TagList.TAG_PPRICE, price);
                    productsList.add(map);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
       runOnUiThread(new Runnable() {
          @Override
        public void run() {
                ListAdapter adapter = new SimpleAdapter(ProductsView.this, productsList, R.layout.list_item,  new String[] { TagList.TAG_PID, TagList.TAG_PNAME, TagList.TAG_PMOD, TagList.TAG_PQUANTITY, TagList.TAG_PPRICE }, new int[] { R.id.pid, R.id.pname, R.id.pmod, R.id.pquantity, R.id.pprice });
                setListAdapter(adapter);
                setProductList();
          }
       });
    }

是的,我也有一个使用 jsonString 的类:我在一个类中有这个

public static final String MAP_ONE = "{\"width\": \"7\", \"height\": \"7\", \"walls\" : [], \"charmain\" : [[0,0]], \"charenemy\" : [[0,6],[6,6]]}";

然后是一个类工厂

public class TileMapFactory {

public static ArrayList<Point> buildMap(final String tileMapJson) throws JSONException {

    ArrayList<Point> points = new ArrayList<Point>();

    final JSONObject jObject = new JSONObject(tileMapJson);
    final JSONArray jWalls = jObject.getJSONArray("walls");

    for (int i = 0; i < jWalls.length(); i++) {
        final JSONArray jWall = jWalls.getJSONArray(i);
        points.add(new Point((Integer) jWall.get(0), (Integer) jWall.get(1)));
    }

    return points;
}

}

然后你可以像这样访问字符串:

walls = TileMapFactory.buildMap(tileMap);
        for (int i = 0; i < walls.size(); i++) {
            mWorldMap[walls.get(i).x][walls.get(i).y].setType(Tile.TYPE_WALL);
        }

在此示例中,墙壁字符串为空,但是当您将墙壁标签替换为 charenemy 时,您将拥有一个包含 2 个字符串的数组。然后您可以通过调用例如walls.get(i); 来访问像arraylist 一样的字符串;在您的情况下,例如 codeField。

于 2013-05-24T06:10:40.380 回答
0

添加新行可能会弄乱您的 json 字符串。
尝试更改sb.append(line + "\n");为,sb.append(line);因为"\n"会弄乱您的字符串。
否则你的代码看起来不错。我建议您AsyncTask在您的情况下使用任务。

    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); // escape will mess up your json string
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }
于 2013-05-24T08:10:56.787 回答
0

现在可以了。谢谢你们。:) 我只是简单地使用了 GSON 中的方法——更容易。无论如何感谢所有帮助。

是的,HttpPost 也应该是一个 HttpGet。

于 2013-05-24T10:27:12.950 回答