Android上的HTTP错误500

时间:2014-08-01 12:15:22

标签: json http gson

所以这是我的代码,其目的是fecth json文件。我从服务器收到错误500,这意味着我知道这是一个内部服务器错误。由于我无法访问服务器的日志,我现在几乎被困住了...我读到了会话和cookie,也许就是这样。你怎么看待它?

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

1 个答案:

答案 0 :(得分:1)

我的代码完美无缺。唯一的错误是在这一行:

HttpPost post = new HttpPost(SERVER_URL);

哪个应该是

HttpGet get = new HttpGet(SERVER_URL);