1

Trying to get the json from http request by using this class:

    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;

      }
          }

But some times getting the exception like

  E/Buffer Error(300): Error converting result java.io.IOException: Attempted read on closed stream.

Can any one help on this thanks in advance.

4

5 に答える 5

0

try like this way..

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

thanks i changed the code to this:Now its double faster than before.

But i need to test more number of times since i was getting the exception which i posted as question very rarely.

   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;

}
    }

It was faster than before ,But problem is app crashing when its the network connection is slow.

于 2013-03-28T09:54:29.957 に答える
0

I suggest removing the static. With me it worked after the removal.

static InputStream is = null;

于 2019-03-29T14:29:20.677 に答える
-1

At first i really don't like your static InputStream variable, why static? Just make it normal variable and not static. And especially in Android static variables are not a win at all.

And second if you want to get JSON from Server you need to use GET request instead of POST

And to question.

I think problem is that you should close BufferedReader rather than InputStream

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

And at the end one suggestion. What about to use EntityUtils instead of getContent(). You will save a time by it instead of reading from InputStream.

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

and now you have quickly JSON as String.

于 2013-03-26T09:47:39.350 に答える
-2

just make InputStream non-static that's all. I used post method and just fine...

于 2013-11-25T05:11:08.460 に答える