0

JSON 入力をキャストする Android アプリを作成しようとしています。これは私のサンプル入力です。

データコンテナーとして機能する次のクラスがあります。

public class DATA {

    public Long id;
    public String title;
    public String author;
    public String url;
    public String date;
    public String body;

    public DATA() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public String toString(){
        return "DATA-Oblect: ID=> " + id + "/nTITLE=> " + title;
    }

}

次のコードを使用します。

protected void doInBackground(String... url) {

try{
    //create an HTTP client
    HttpClient client   =   new DefaultHttpClient();
    HttpPost post       =   new HttpPost("http://kylewbanks.com/rest/posts");//url[0]);

    //perform the request and check the status code
    HttpResponse response = client.execute(post);
    StatusLine statusLine   =   response.getStatusLine();

    if(statusLine.getStatusCode() == 200){

        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();
        content = entity.getContent();



        try{
            Reader reader = new InputStreamReader(content);

            GsonBuilder gsonBuilder = new GsonBuilder();
            gsonBuilder.setDateFormat("M/d/yy hh:mm a");
            Gson gson = gsonBuilder.create();

            List<DATA> Data = new ArrayList<DATA>();
            Data = Arrays.asList(gson.fromJson(reader, DATA[].class));

            content.close();

        }catch(Exception ex){
            Log.e(TAG, "JSON parse failed due to: " + ex);
        }

    }else{
        Log.e(TAG, "Server response code: " + statusLine.getStatusCode());
    }
}catch(Exception ex){
    Log.e(TAG, "HTTP-Post failed due to: " + ex);
}

}

次の例外エラーが発生します。

JSON parse failed due to: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

誰が私が間違っているのか教えてもらえますか?

アップデート:

以下は私の主な活動コードです:

public class MainActivity extends Activity {

    private List<DATA> Data;
    public static final String jsonSource = "http://kylewbanks.com/rest/posts";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

//      DataRetriever("http://kylewbanks.com/rest/posts");

        new DataRetriever(this.getApplicationContext()).execute(jsonSource);


    }



    /**
     * Callback function for handling retrieved data from the
     * DATARetrieve class
     * @param Data
     */
    public void DataListDrop(List<DATA> Data){
        this.Data = Data;

        Toast.makeText(MainActivity.this, "Testing ... testing!", Toast.LENGTH_SHORT).show();

        runOnUiThread(new Runnable() {          

            @Override
            public void run() {
                // TODO Auto-generated method stub
                for(DATA data : MainActivity.this.Data){
                    Toast.makeText(MainActivity.this, data.title, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * Callback function for responding no-data return from the 
     * DATARetrieve class
     */
    private void NoData(){
        runOnUiThread(new Runnable() {

            @Override
            public void run(){
                Toast.makeText(MainActivity.this, "No data to process! Checkout LogCat.", Toast.LENGTH_SHORT).show();
            }
        });
    }

}
4

0 に答える 0