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

时间:2013-03-26 09:34:00

标签: 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 Error(300):转换结果java.io.IOException时出错:尝试在已关闭的流上读取。

任何人都可以事先得到帮助。

5 个答案:

答案 0 :(得分:0)

尝试这样..

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();
        }

答案 1 :(得分:0)

谢谢我将代码更改为:现在它的速度比以前快了。

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

   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;

}
    }

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

答案 2 :(得分:0)

我建议删除静电。在删除之后,它与我一起工作。

静态 InputStream为= null;

答案 3 :(得分:-1)

起初我真的不喜欢你的静态InputStream变量,为什么是静态?只是使它成为正常变量而不是静态变量。特别是在Android中,静态变量根本不是一个胜利。

如果您想从服务器获取JSON,则需要使用GET请求代替POST

提问。

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

while ((line = reader.readLine()) != null) {
    sb.append(line + "\n");
}
reader.close();
// next work

最后一个建议。如何使用EntityUtils代替 getContent()。您将通过它节省时间,而不是从InputStream中读取。

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

现在你快速JSON为String。

答案 4 :(得分:-2)

只需提出InputStream non-static即可。 我用post方法就好了...