检索JSON会抛出ClientProtocolException

时间:2012-07-03 20:42:06

标签: android json http exception

我写了两个几乎相同的检索JSONObject的方法。较新的方法抛出ClientProtocolException,代码500.我试图找出这两种方法之间的差异可能导致一个抛出异常而另一个完全正常工作。

方法#1:

public class MainActivity extends Activity
{
    // fluff

    protected JSONObject retrieveJSON()
    {
        HttpClient client = new DefaultHttpClient();
        String url = getString(R.string.url);
        String result = null;
        JSONObject json = null;

        try
        {
            HttpGet getMethod = new HttpGet(url);
            ReponseHandler<String> responseHandler = new BasicResponseHandler();
            result = client.execute(getMethod, responseHandler);

            json = new JSONObject(responseBody);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }   
}

方法#2

public class ServiceRequest
{
    public ServiceRequest()
    {
    }

    public ServiceRequest(String url)
    {
        this.URL = url;
    }

    public String URL;

    public String execute()
    {
        String result = null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet getRequest = new HttpGet(this.URL);

        try
        {
            ResponseHandler<String> handler = new BasicResponseHandler();
            result = httpClient.execute(getRequest, handler);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return result;
    }
}

两种方法都返回result,然后将其处理为JSONObject。或者至少,这两种方法都是应该的。我能想到的唯一主要区别是方法#1 被编码为主要活动的类方法,而方法#2 存在于独立类中。这种差异可能会引发ClientProtocolException吗?

0 个答案:

没有答案