尝试从Web API获取响应时BufferReader中的错误

时间:2013-12-06 10:01:54

标签: android api get inputstream

似乎“今天”不是我的日子,所以我试图通过GET响应从web-API获取数据,其中包含JSON结构。我想提一下,这不是我的第一个“json-get”应用程序,我已经制作了一个通过API加载数据的应用程序,所有事情都非常好,直到今天。因此,在尝试连接时,我经常会在同一行中遇到2个错误。

  • NumberFormatException: Invalid int: ""
  • java.io.EOFException at at libcore.io.Streams.readAsciiLine(Streams.java:203)

大约每4个应用程序启动崩溃,其中一个错误。我已经尝试在不同的设备和模拟器中启动我的应用程序,但仍然会出现此错误。这是一个错误吗?我想请注意,我没有设备或API的任何互联网问题,您可以自己尝试here

已更新 现在仍然是无效int的第一个问题,代码如下所示:

 public static void getNewsList(final OnLoadFinishedListener listener) {
    if (listener != null) {
        new Thread(new Runnable() {
            public void run() {
                Thread.currentThread().setName("Thread_getNewsList");
                ArrayList<News> news = null;
                try {
                    URL url = new URL(mURL_API + "oper.php?act=getnewsall");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestProperty("Connection", "close");
                    conn.connect();
             //still getting an error in this line -->
                    if (conn.getResponseCode() == 200) {
             //<--
                        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), HTTP.UTF_8));
                        StringBuilder buffer = new StringBuilder();
                        String line = null;
                        try {
                            while ((line = in.readLine()) != null) {
                                buffer.append(line);
                            }

                        } finally {
                            in.close();
                        }
                        JsonArray request = (JsonArray) mGparser.parse(buffer.toString());
                        in.close();

                        news = mGson.fromJson(request, new TypeToken<ArrayList<News>>() {
                        }.getType());
                        listener.onLoadFinished(news);
                        conn.disconnect();
                    } else
                        throw new NoInternetException("Bad internet request, response code is "
                                + conn.getResponseCode());
                } catch (Exception e) {
                    e.printStackTrace();
                    listener.onNotConnected();
                }
            }
        }).start();
    }
}

2 个答案:

答案 0 :(得分:0)

让我试着让你今天的“糟糕”变好今天

public static void getNewsList(final OnLoadFinishedListener listener) {
        if (listener != null) {
            new Thread(new Runnable() {
                public void run() {
                    Thread.currentThread().setName("Thread_getNewsList");
                    ArrayList<News> news = null;
                    try {
                        URL url = new URL(mURL_API + "oper.php?act=getnewsall");
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        conn.setRequestProperty("Connection", "close");
                        conn.connect();
            //Here i get an error -->
                        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),HTTP.UTF_8));
            //<--

                        StringBuilder buffer = new StringBuilder();


                        String line = null;
                        try {
                            while ((line = in.readLine()) != null) {
                                buffer.append(line);
                            }

                        } finally {
                            in.close();
                        }


                        JsonArray request = (JsonArray) mGparser.parse(buffer.toString());
                        in.close();

                        news = mGson.fromJson(request, new TypeToken<ArrayList<News>>() {
                        }.getType());
                        listener.onLoadFinished(news);
                        conn.disconnect();
                    } catch (Exception e) {
                        e.printStackTrace();
                        listener.onNotConnected();
                    }
                }
            }).start();
        }
    }

答案 1 :(得分:0)

要解决NumberFormatException,请参阅我的评论。 在conn.connect()之后尝试放置此代码:

InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
Gson gson = new Gson();
//I don't know what is type of news but based on your code
news = gson.fromJson(isr, new TypeToken<ArrayList<News>>());