Edge
Edge

Reputation: 933

how to read it into a string without using a readline of String Buffer in android

public class HttpPosrHitter {

    public static String getJSONfromURL(String url, String member_id,
            String phonenumber) {
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
            pairs.add(new BasicNameValuePair("memberid", member_id));
            pairs.add(new BasicNameValuePair("numbers", phonenumber));
            httppost.setEntity(new UrlEncodedFormEntity(pairs));

            // http post

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        try {

            jArray = new JSONObject(result);
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }

        return result;
    }
}

This is class from which i am Post Phone Number to web service and getting response . when i Post Number of Phone which has 15 to 20 contact i am getting response . but when i post number which has 150 contact i am not getting response one at a time i have to relaunch app two time then i am getting response . i dont know where i am doing mistake . even i am unable to read phone large file in chunks with fixed size buffer.

Upvotes: 0

Views: 135

Answers (2)

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28551

Just to solve all your potential bugs in one single shot: is there anything preventing you from using Retrofit and GSON or Jackson?

Each time I see such JSON/InputStream/URLConnection/... questions, I keep wondering why people keep on spending time to reinvent basic stuff instead of actually writing apps.

Upvotes: 1

VegeOSplash
VegeOSplash

Reputation: 204

        public class HttpPosrHitter {

        public static String getJSONfromURL(String url, String member_id,
                String phonenumber) {
            InputStream is = null;
            String result = "";
            JSONObject jArray = null;
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                List<NameValuePair> pairs = new ArrayList<NameValuePair>();
                pairs.add(new BasicNameValuePair("memberid", member_id));
                pairs.add(new BasicNameValuePair("numbers", phonenumber));
                httppost.setEntity(new UrlEncodedFormEntity(pairs));

                // http post

                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity); //changes Made

            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection " + e.toString());
            }



            try {

                jArray = new JSONObject(result);
            } catch (JSONException e) {
                Log.e("log_tag", "Error parsing data " + e.toString());
            }

            return result;
        }
    }

Upvotes: 0

Related Questions