空指针异常:HTTP实体

时间:2013-04-19 05:31:08

标签: android json http httprequest

我在下面的代码中得到一个空指针异常,任何想法为什么?

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    static int GET = '1';
    static int POST ='2';

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params, int method) throws URISyntaxException {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse = null;
        HttpEntity httpentity = null;
        HttpPost postrequest = null;

        try {
            switch (method) {
            case 1: 
                if(params!=null){
                    String paramString = URLEncodedUtils.format(params, "utf-8");
                    url += "?" + paramString;   
                }

                Log.e("URL", url);
                HttpGet httpGet = new HttpGet(url);
                httpResponse = httpClient.execute(httpGet);
                httpentity = httpResponse.getEntity();          

                break;
            case 2: 
                postrequest = new HttpPost(url);
                postrequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
                httpResponse = httpClient.execute(postrequest);
                httpentity = httpResponse.getEntity();
                if(httpentity !=null)
                {
                    Log.i("RESPONSE", EntityUtils.toString(httpentity));
                }
                else{
                    Log.i("NULL", EntityUtils.toString(httpentity));
                }
                break;
            }
            is = httpentity.getContent();
        } 
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            Log.e("JSON", json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);            
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;
    }

}   

NPE出现在

行中
is = httpentity.getContent();

基本上,我使用函数类访问此类,最后通过另一个主类实现它。

1 个答案:

答案 0 :(得分:3)

问题是,您的httpentitynull,您想要检索其内容。您应该确保method12,因为您可能 - 错误地 - 省略整个switch语句并直接转到is = httpentity.getContent();

此外,如果您查看Android文档,则可以阅读:

  

public abstract HttpEntity getEntity()在API级别1中添加

     

获取此响应的消息实体(如果有)。该实体是   通过调用setEntity提供。

     

返回响应实体,如果没有,则返回null

这意味着如果没有响应实体,getEntity()方法将返回null。而且你只在第二种情况下进行了空检查。您还应该在要检索内容的位置进行检查,因此整个try块应如下所示:

try {
    switch (method) {
    case 1: 
        if(params!=null){
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;   
        }

        Log.e("URL", url);
        HttpGet httpGet = new HttpGet(url);
        httpResponse = httpClient.execute(httpGet);
        httpentity = httpResponse.getEntity();          

        break;
    case 2: 
        postrequest = new HttpPost(url);
        postrequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
        httpResponse = httpClient.execute(postrequest);
        httpentity = httpResponse.getEntity();
        if(httpentity !=null)
        {
            Log.i("RESPONSE", EntityUtils.toString(httpentity));
        }
        else{
            Log.i("NULL", EntityUtils.toString(httpentity));
        }
        break;
    }
    if(httpentity != null)
    {
        is = httpentity.getContent();
        Log.i("RESPONSE", EntityUtils.toString(httpentity));
    } else {
        Log.i("NULL", EntityUtils.toString(httpentity));
    }
} 
catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}