【问题标题】:"Null pointer exception" when accessing json object using url使用 url 访问 json 对象时出现“空指针异常”
【发布时间】:2011-02-11 12:30:23
【问题描述】:

我正在尝试使用以下代码访问 json 对象

http://epubreader.XXXXXXX.net/public/json/books.json

        try
       {
          InputStream in = openHttpConnection("http://epubreader.feathersoft.net
          /public/json/books.json"); 
          Data=new byte[in.available()];
          in.read(Data);
        }
       catch(IOException e)
           {
           Log.e("url", e.getLocalizedMessage()+e.getCause()+e.getStackTrace());    
           }


   }

   private InputStream openHttpConnection(String url_send) throws IOException
   {

        InputStream in = null;
        int response = -1;

        URL url = new URL(url_send);
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))
           throw new IOException("Not an HTTP connection");

        try {
           HttpURLConnection httpConn = (HttpURLConnection) conn;
           httpConn.setAllowUserInteraction(false);
           httpConn.setInstanceFollowRedirects(true);
           httpConn.setRequestMethod("GET");
           httpConn.connect();

           response = httpConn.getResponseCode();
           if (response == HttpURLConnection.HTTP_OK) {
              in = httpConn.getInputStream();
           }
        } catch (Exception ex) {
           throw new IOException("Error connecting");
        }
        return in;
    }


  }   

然后我得到 Nullpointer 异常我无法弄清楚它是什么请帮助我

感谢您的宝贵时间

【问题讨论】:

    标签: android web-services json


    【解决方案1】:

    NPE 在哪一行?如果响应 != HttpURLConnection.HTTP_OK,那么 openHttpConnection 将返回 null 并且您将在 in.read(Data) 上获得 NPE。你可能想做这样的事情。

    if (response == HttpURLConnection.HTTP_OK) {
         in = httpConn.getInputStream();
    } else {
         throw new IOException("Bad Response Received");
    }
    

    而且你也不需要 openHttpConnection 中的 try 和 catch 块,只需让它抛出 IOException 并像在上面的代码中一样处理它。

    在 sdk 中使用 Apache HttpClient 类可能会更简洁。类似的东西。

    HttpClient client = AndroidHttpClient.newInstance("myUserAgent");
    HttpGet httpGet = new HttpGet("http://epubreader.feathersoft.net
              /public/json/books.json");
    HttpResponse response = client.execute(httpGet);
    if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        InputStream inputStream = response.getEntity().getContent();
        // read from stream
    }
    

    您还可以将 execute 与匿名 ResponseHandler 一起使用,这样您的方法将在成功时返回一个 List。

    【讨论】:

    • 感谢您的快速回复。我尝试使用 DefaultHttpClient。我收到错误 -:( 并警告“在非活动 InputConnection 上显示状态”。输入流的空指针异常
    • 是的。但是通过使用 DefaultHttpClient 而不是 AndroidHttpClient。我对 java 和 android 非常陌生。什么是 AndroidHttpClient .....谢谢您的时间
    • 它是 Android 的一部分。它使用 Android 的最佳设置定制 DefaultHttpClient。
    • 谢谢伙计。这是我的错误我明白了(我正在使用 Sdk 3 。其中 AndroidHttpClient 不存在 -:()
    猜你喜欢
    • 2015-03-22
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    相关资源
    最近更新 更多