0

私の状況は次のとおりです。サーバーにハッシュを送信した後、サーバー上のphpスクリプトから応答としてJSON配列を取得します。JSON配列には2つの可能な形式があります。ハッシュが有効でない場合、最初のハッシュはのようになり{"status":0}ます。2番目のフォーメーション、ハッシュが有効な場合、ハッシュには予定が含まれます->はのようになります{"appointmentName":"Meeting","date":"2013-03-15","startTime":"10:00:00","endTime":"12:10:00","status":2}

JSON配列は、複数の予定で埋められる可能性があります。下記参照:

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

android-appのListViewにそれらの予定をリストしたいと思います。

JSON配列を処理する私のメソッド:

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();
    }
}

私のListViewはまだ特別なものではありません:

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

handleActivationResult()メソッドがリストに予定を入力するのはどうでしょうか。

4

1 に答える 1

0

json 文字列が構成されていることを確認します"status":0。データがアダプターに設定されていない場合は、構成されます。

サーバーから応答を取得したら、json オブジェクトを作成する前に、文字列を確認し"status" 0"ます。表示エラー文字列が含まれているかどうかを確認します。json で成功応答のステータスについて言及していないためです。

"success":2実際のデータを解析する前に取得している場合は、ステータスを解析し、それに応じて行動してください。

于 2013-03-11T10:47:26.190 に答える