Aurélien Leloup
Aurélien Leloup

Reputation: 166

HTTP error 500 on Android

So here is my code, its purpose is to fecth a json file. I get an error 500 from the server, which means I know that it is an internal server error. As I can't access to the logs of the the server, I'm pretty much stuck from now... I read about session and cookies, maybe that's it. What do you guy think of it ?

private class ListingFetcher extends AsyncTask<Void, Void, String> {
    private static final String TAG = "ListingFetcher";
    public static final String SERVER_URL = "http://www.myurl.com/listing.json";

    @Override
    protected String doInBackground(Void... params) {
        try {
            //Create an HTTP client
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(SERVER_URL);

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

                try {
                    //Read the server response and attempt to parse it as JSON
                    Reader reader = new InputStreamReader(content);

                    GsonBuilder gsonBuilder = new GsonBuilder();
                                            gsonBuilder.setDateFormat("M/d/yy hh:mm a");
                    Gson gson = gsonBuilder.create();
                    List<Listing> events = new ArrayList<Listing>();
                    events = Arrays.asList(gson.fromJson(reader, Listing[].class));
                    content.close();

                    handlePostsList(events);
                } catch (Exception ex) {
                    Log.e(TAG, "Failed to parse JSON due to: " + ex);
                    failedLoadingPosts();
                }
            } else {
                Log.e(TAG, "Server responded with status code: " + statusLine.getStatusCode());
                failedLoadingPosts();
            }
        } catch(Exception ex) {
            Log.e(TAG, "Failed to send HTTP POST request due to: " + ex);
            failedLoadingPosts();
        }
        return null;
    } 
}

Upvotes: 1

Views: 1069

Answers (1)

Aur&#233;lien Leloup
Aur&#233;lien Leloup

Reputation: 166

My code is perfectly working. THe only mistake is on this line :

HttpPost post = new HttpPost(SERVER_URL);

Which should be

HttpGet get = new HttpGet(SERVER_URL);

Upvotes: 1

Related Questions