0

まず、これは私のjson出力です。

{"alert_id":"1","type":"緊急","部門":"工学部","クラス":"4","学部":"ソフトウェア工学","本文":"マイク" ,"日付":"10.04.1990"}

これはコードの解析です。

package com.androidalert;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class AlertsJSON extends Activity {

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

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.tuncayyildirim.com.tr/cagri_android_frontend/event_restful/event_handler/get_event.json");

        EditText textID = (EditText) findViewById(R.id.textID);
        EditText textType = (EditText) findViewById(R.id.textType);
        EditText textDepartment = (EditText) findViewById(R.id.textDepartment);
        EditText textClass = (EditText) findViewById(R.id.textClass);
        EditText textFaculty = (EditText) findViewById(R.id.textFaculty);
        EditText textBody = (EditText) findViewById(R.id.textBody);
        EditText textDate = (EditText) findViewById(R.id.textDate);

        textID.setKeyListener(null);
        textType.setKeyListener(null);
        textDepartment.setKeyListener(null);
        textClass.setKeyListener(null);
        textFaculty.setKeyListener(null);
        textBody.setKeyListener(null);
        textDate.setKeyListener(null);

        try {

            HttpResponse response = httpclient.execute(httppost);
            String jsonResult = inputStreamToString(
                    response.getEntity().getContent()).toString();
            JSONObject object = new JSONObject(jsonResult);

            String id = object.getString("alert_id");
            String type = object.getString("type");
            String department = object.getString("department");
            String clas = object.getString("clas");
            String faculty = object.getString("faculty");
            String body = object.getString("body");
            String date = object.getString("date");

            textID.setText(id);
            textType.setText(type);
            textDepartment.setText(department);
            textClass.setText(clas);
            textFaculty.setText(faculty);
            textBody.setText(body);
            textDate.setText(date);

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private StringBuilder inputStreamToString(InputStream is) {
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        try {
            while ((rLine = rd.readLine()) != null) {
                answer.append(rLine);
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return answer;
    }
}

コードは正しいと思いますが、アプリケーションでその JSON 出力を表示できません。

4

1 に答える 1

0

JSON オブジェクトの解析は正しいように見えるので、問題は別の場所にあります。まず第一に、UI スレッドでネットワーキングを行うべきではありません (最新の SDK では実際にはできません)。また、実際のjsonを受け取った場合は、解析する前に「jsonResult」の値を確認します。

于 2013-06-10T22:29:38.227 に答える