1

尝试使用此类从 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:尝试在关闭的流上读取。

任何人都可以提前帮助解决这个问题。

4

5 回答 5

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;

}
    }

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

于 2013-03-28T09:54:29.957 回答
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();
        }
于 2013-03-26T09:44:01.573 回答
0

我建议去除静电。对我来说,它在移除后起作用。

静态InputStream is = null;

于 2019-03-29T14:29:20.677 回答
-1

起初我真的不喜欢你的静态InputStream变量,为什么是静态的?只需使其成为正常变量而不是静态的。尤其是在 Android 中,静态变量根本不是赢家。

其次,如果您想JSON从服务器获取,则需要使用GETrequest 而不是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了字符串。

于 2013-03-26T09:47:39.350 回答
-2

InputStream non-static这样吧。我使用了post方法,很好......

于 2013-11-25T05:11:08.460 回答