0

my situation is the following: I get a JSON-Array as response from a php script on a server after I sent a hash to it. There are two possible formations of the JSON-Array. If the hash is not valid, the first would be just like {"status":0}. Second formation, when the hash is valid, it would have appointments in it -> would be like {"appointmentName":"Meeting","date":"2013-03-15","startTime":"10:00:00","endTime":"12:10:00","status":2}.

The JSON-Array could be filled with more than just one appointment. See below:

{
    "appointmentName": "Proposal",
    "date": "2013-11-11",
    "startTime": "09:00:00",
    "endTime": "13:00:00",
    "status": 2
}
{
    "appointmentName": "Meeting",
    "date": "2013-03-15",
    "startTime": "10:00:00",
    "endTime": "12:10:00",
    "status": 2
}

I want to list those appointments in a ListView on the android-app.

My method that handles the the JSON-Array:

public void handleActivationResult(String result) {     
    try {           
            JSONObject jsonObject = new JSONObject(new String(result));         
            String statusCode = jsonObject.getString("status");

            TextView mainView = (TextView) findViewById(R.id.textView1);

            if (statusCode.equals("2")) {
                    //fill listview with appointments
            } else if (statusCode.equals(0)) {
                //empty string sent to server
            } else if (statusCode.equals(5)) {
                //hash doesnt match
            } else if (statusCode.equals(6)) {
                //correct hash, but no upcoming appointments
            } else {
                //unknown error
            }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

My ListView isnt something special yet:

<ListView
    android:id="@+id/appointmentsList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</ListView>

How should it bee, that the handleActivationResult() method will fill the list with the appointments?

amoht
  • 3
  • 1
  • 3
  • 2
    JSON array for multiple appointments is not valid. check it using http://jsonlint.com/ – Jay Mayu Mar 11 '13 at 09:10
  • Also, to populate ListView with JSON, look at this post: http://stackoverflow.com/a/8113337/782609 – kamituel Mar 11 '13 at 09:12
  • Thanks for the Answer. I'm trying out the JSONAdapter but the problem here is now, that I dont know how to use it specifically in my Situation on my JSONObject. So the other Part, do I have to change the output of the appointments from my php script? – amoht Mar 11 '13 at 09:28
  • look this example-http://stackoverflow.com/questions/4540754/add-dynamically-elements-to-a-listview-android – Ajit Mar 11 '13 at 10:01
  • and to handle json array, see this example-http://stackoverflow.com/questions/15127373/create-android-layout-with-buttons-from-server-response/15128543#15128543 – Ajit Mar 11 '13 at 10:03

1 Answers1

0

Check the json string consists "status":0,if not set the data to the adapter.

When you get response from server,before creating json object check for the string.wheather it contains "status" 0" if contains display error string.As you did not mention status for success response in json.

if you are getting "success":2 then before parsing actual data ,parse status and act accordingly.

Meher
  • 2,545
  • 3
  • 26
  • 53