java - Android 从 http 请求获取 json 引发 IOException : Attempted read on closed stream

标签 java android json

尝试使用此类从http请求中获取json:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONException;
    import org.json.JSONObject;
    import android.util.Log;

    public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        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();
    } 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;

      }
          }

但有时会出现异常,例如

  E/Buffer 错误(300):转换结果时出错 java.io.IOException:尝试在关闭的流上读取。

任何人都可以帮忙解决这个问题吗?提前致谢。

最佳答案

尝试这样..

HttpClient client = new DefaultHttpClient();
        // Perform a GET request for a JSON list
        HttpUriRequest request = new HttpGet("https://somejson.json");
        // Get the response that sends back
        HttpResponse response = null;
        try {
            response = client.execute(request);
        } catch (ClientProtocolException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

关于java - Android 从 http 请求获取 json 引发 IOException : Attempted read on closed stream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15633579/

相关文章:

java - 无法导入带有 type 标签的 Maven 依赖项

android - 为 WebTokens 身份验证改造自定义客户端

android - kotlin-类不是抽象的,不实现抽象基类成员(RecyclerView)

javascript - 从表行发布数据,如 json 格式

java - 如何根据 JSON 中的 3 个列表大小发送数据

json - NodeJs、Express、BodyParse 和 JSON

java - Moneydance 扩展开发的最佳实践

java - JComboBox 未显示

java - 如何找到Java类中哪些对象不能被序列化?

Android:如何将 Class 类型放入 Bundle 中?