3

次のコードでヌル ポインター例外が発生しています。

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

基本的に、関数クラスを使用してこのクラスにアクセスし、最終的に別のメイン クラスを介して実装します。

4

1 に答える 1

3

問題は、コンテンツを取得したい場所に並んでいることですhttpentity。誤って switch ステートメント全体を省略nullして.method12is = httpentity.getContent();

また、Android のドキュメントを確認すると、次のことがわかります。

public abstract HttpEntity getEntity () API レベル 1 で追加

存在する場合、この応答のメッセージ エンティティを取得します。エンティティは、setEntity を呼び出すことによって提供されます。

応答エンティティを返すか、存在しない場合は null を返します

これは、応答エンティティがない場合、getEntity()メソッドが を返すことを意味しnullます。そして、2番目のケースでのみ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();
}
于 2013-04-19T05:37:22.533 に答える