'Android GSON processing remote JSON runtime exception

I am attempting to write an Android app which casts JSON input. This is my sample input.

I have the following class to serve as the data container:

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

}

Using the following code:

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

I get the following exception error:

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

What am I doing wrong?

Update

The following the the my main activity code:

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


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source