Java Android从http请求获取json引发IOException:尝试在封闭流上读取

Java Android从http请求获取json引发IOException:尝试在封闭流上读取,java,android,json,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;

尝试使用以下类从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 Error300:转换结果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();
        }

起初我真的不喜欢你的静态InputStream变量,为什么是静态的?只需将其设置为正常变量,而不是静态变量。尤其是在Android中,静态变量根本不是一场胜利

第二,如果您想从服务器获取JSON,您需要使用get请求而不是POST

还有一个问题

我认为问题在于应该关闭BufferedReader而不是InputStream

最后还有一个建议。如何使用而不是getContent。这样可以节省时间,而不是从InputStream读取

HttpEntity entity = response.getEntity();
String json = EntityUtils.toString(entity);

现在您可以快速地将JSON作为字符串。

谢谢,我将代码更改为:现在比以前快一倍

但我需要测试更多的次数,因为我得到了异常,我很少作为问题发布

   public class JSONParser {

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

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url) {

    HttpClient client = new DefaultHttpClient();
    // Perform a GET request for a JSON list
    HttpUriRequest request = new HttpGet(url);
    // 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();
    }

    HttpEntity entity = response.getEntity();

    try {
        json = EntityUtils.toString(entity);
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}
    }
它比以前快了,但问题是当网络连接缓慢时应用程序会崩溃。

只需使InputStream非静态即可。
我使用了post方法,很好…

我建议删除静电干扰。对我来说,它在搬走后起了作用


静态InputStream为空

请粘贴日志cat错误。为什么有静态InputStream字段?拥有任何类型的输入流字段都是不寻常的。。。将其设置为静态几乎肯定是不合适的。作为局部变量,您的所有静态变量可能会更好。您确定输入是iso-8859-1吗?@jon Skeet谢谢,我认为您完全正确。但为什么会出现异常,实际上我在pageviewer的getview中多次从异步类调用此方法。@etienne,我对iso-8859-1不太清楚,你能推荐一下标准方法吗?
   public class JSONParser {

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

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url) {

    HttpClient client = new DefaultHttpClient();
    // Perform a GET request for a JSON list
    HttpUriRequest request = new HttpGet(url);
    // 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();
    }

    HttpEntity entity = response.getEntity();

    try {
        json = EntityUtils.toString(entity);
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}
    }