IOException:意外结束流抛出HttpUrlConnection

时间:2017-08-06 20:04:23

标签: java android inputstream ioexception

我在尝试阅读IOException: unexpected end of stream的{​​{1}}时收到inputStream。我已使用其他网址(如HttpUrlConnection进行了测试,并在其他有用的人员建议中添加了http://google.com测试错误但未发生错误。我的目标SDK和buildSDK匹配。我使用相同的类来获取具有不同URL的其他片段的结果而没有问题。由于尝试更改为另一个API,我遇到了这个问题。

我试图从 - urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0");

获取JSON结果的网址

我的代码来获取JSON -

http://eventregistry.org/json/article?query=%7B%22%24query%22%3A%7B%22%24and%22%3A%5B%7B%22sourceUri%22%3A%7B%22%24and%22%3A%5B%22bbc.co.uk%22%5D%7D%7D%2C%7B%22lang%22%3A%22eng%22%7D%5D%7D%7D&action=getArticles&apikey=342f5f25-75ac-44b5-8da0441508e871e8&resultType=articles&articlesSortBy=date&articlesCount=10&articlesIncludeArticleImage=true

堆栈跟踪

package com.example.android.greennewswire;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

public final class QueryUtils {

    private static final String LOG_TAG = QueryUtils.class.getSimpleName();

    private QueryUtils(){
    }

    public static ArrayList<News> fetchNewsData(String requestUrl) {
        URL url = createUrl(requestUrl);
        String jsonResponse = null;
        try {String testString = readUrl(requestUrl); Log.i(LOG_TAG, "testString: " + testString);
            jsonResponse = makeHttpRequest(url);
        } catch (IOException e) {
            Log.e(LOG_TAG, "Error making HTTP request", e);
        }
        if (jsonResponse == null) {
            return new ArrayList<News>();
        }
        ArrayList<News> news = extractBooks(jsonResponse);

        return news;
    }

    public static URL createUrl(String stringUrl){
        URL url = null;
        try {
            url = new URL(stringUrl);
        } catch (MalformedURLException e) {
            Log.e(LOG_TAG, "Malformed URL", e);
        } return url;
    }

    private static String makeHttpRequest(URL url) throws IOException {
        String jsonResponse = null;

        if (url == null) {
            return jsonResponse;
        }

        HttpURLConnection urlConnection = null;
        InputStream inputStream = null;
        InputStream errorStream = null;

        try {
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setReadTimeout(10000);
            urlConnection.setRequestMethod("GET");
            urlConnection.setConnectTimeout(10000);
            urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0");
            urlConnection.connect();
            Log.e(LOG_TAG, "Response code: " + urlConnection.getResponseCode());


            if (urlConnection.getResponseCode() == 200) {
                inputStream = urlConnection.getInputStream();
                jsonResponse = readFromInputStream(inputStream);
            } else {
                Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
            }
        } catch (IOException e) {
            String string = e.getCause().toString();
            Log.e(LOG_TAG, "Problem reading from input stream, " + string, e);
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            } if (inputStream != null) {
                inputStream.close();
            }
        }
        return jsonResponse;
    }


    private static String readFromInputStream(InputStream inputStream) throws IOException {
        StringBuilder output = new StringBuilder();
        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
            BufferedReader reader = new BufferedReader(inputStreamReader);
            String line = reader.readLine();
            while (line != null) {
                output.append(line);
                line = reader.readLine();
            }
        }
        return output.toString();
    }

0 个答案:

没有答案