NorthernLights
NorthernLights

Reputation: 370

android error when trying to put json data in viewlist php+json+java

I am trying to make a search page for my andriod app using PHP,JSON and SQL.

here is the code that is giving me the error :

  try{
        JSONArray jArray = new JSONArray(result);
        int jArrLeng = jArray.length();
        for(int i=0; i<jArrLeng;i++){
            JSONObject json_data= jArray.getJSONObject(i);

            tempID += json_data.getString("ID") + "\n";
            tempID += json_data.getString("heading") + "\n";
            tempID += json_data.getString("rank") + "\n:";

        }

        arr = tempID.split(":");
        resultLV.setAdapter(new ArrayAdapter<String>(SearchPage.this, android.R.layout.simple_list_item_1,arr));



    }catch (Exception e){
        String errMsg = "error when putting the json data in the list";
        Toast.makeText(getApplicationContext(), errMsg, Toast.LENGTH_LONG).show();
    }

i have already used this code on another page and it works perfectly but when using it another page/activity it gives me an error when trying to put the json data in listview.

i would guess the problem is when i am setting the ArrayAdapter, have i done anything wrong?

resultLV.setAdapter(new ArrayAdapter<String>(SearchPage.this, android.R.layout.simple_list_item_1,arr));

Upvotes: 1

Views: 171

Answers (1)

Petro
Petro

Reputation: 3652

To answer your final question:

    //array list
     List<String> your_array_list = new ArrayList<String>();
     try{
    JSONArray jArray = new JSONArray(result);
    int jArrLeng = jArray.length();
    for(int i=0; i<jArrLeng;i++){
        JSONObject json_data= jArray.getJSONObject(i);

        your_array_list.add(json_data.getString("ID") + "\n");
        your_array_list.add(json_data.getString("heading") + "\n");
        your_array_list.add(json_data.getString("rank") + "\n:");

      }
    resultLV.setAdapter(new ArrayAdapter<String>(SearchPage.this, android.R.layout.simple_list_item_1,your_array_list));

    //then, to get the items from inside the adapter:
    for(String item_in_list : your_array_list){
        System.out.println(item_in_list);
    }

To answer the original question:

Check out http://jsonformatter.curiousconcept.com/ for formatting issues

Try looking at this info: http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject%28java.lang.String%29

JSONObject

public JSONObject(java.lang.String source)
           throws JSONException

Construct a JSONObject from a source JSON text string. This is the most commonly used` JSONObject constructor.

Parameters:
    source - `A string beginning with { (left brace) and ending with } (right brace).` 
Throws:
    JSONException - If there is a syntax error in the source string or a duplicated key.

FYI: The inbuilt JSONObject and JSONArray cannot be used to get certain json responses.

You could try downloading a small library named "json-simple-1.1.1.jar" from this link https://json-simple.googlecode.com/files/json-simple-1.1.1.jar.

Upvotes: 1

Related Questions