Skip to content Skip to sidebar Skip to footer

Android Json Receive Issue

I have a string problem, I used the below code for recieving JSON data from an URL, the code is working fine, but the problem is I am not getting full data only half of the JSON va

Solution 1:

Try getting data like this using BufferedReader

String line="";
   BufferedReader rd = newBufferedReader(newInputStreamReader(in));
                // Read response while ((line = rd.readLine()) != null) { 
               total.append(line); 
            }
   String jsonString=total.toString();

Solution 2:

InputStream is = entity.getContent();
BufferedReader out = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = out.readLine()) != null) {sb.append(line + "\n");
            }
is.close();
String json = sb.toString();

try this

Solution 3:

Please try this,

publicstaticJSONObjectgetJson(String url){

    InputStream is = null;
    String result = "";
    JSONObject jsonObject = null;

    // HTTPtry {           
        HttpClient httpclient = newDefaultHttpClient(); // for port 80 requests!HttpPost httppost = newHttpPost(url);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch(Exception e) {
        returnnull;
    }

    // Read response to stringtry {           
        BufferedReader reader = newBufferedReader(newInputStreamReader(is,"utf-8"),8);
        StringBuilder sb = newStringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();             
    } catch(Exception e) {
        returnnull;
    }

    // Convert string to objecttry {
        jsonObject = newJSONObject(result);            
    } catch(JSONException e) {
        returnnull;
    }

    return jsonObject;

}}

Solution 4:

you have to remove this line

int n =  in.read(b);

and add

int n=0;

    while ((n= in.read(b)) != null)

Post a Comment for "Android Json Receive Issue"