如何在不使用android中的String Buffer读取线的情况下将其读入字符串

时间:2014-08-04 11:17:26

标签: java 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;
    }
}

这是我将电话号码发送到网络服务并获得回复的课程。  当我发布有15到20个联系人的电话号码我得到回应。但是当我发布有150个联系人的号码时我没有得到一个响应,我必须重新启动应用程序两次然后我得到回应。我不知道我在哪里做错了。即使我无法用固定大小的缓冲区读取大块文件。

2 个答案:

答案 0 :(得分:1)

只需一次性解决所有潜在错误:是否有任何因素阻止您使用RetrofitGSONJackson

每当我看到这样的JSON / InputStream / URLConnection / ...问题时,我一直在想为什么人们会花时间重新发明基本内容而不是实际编写应用程序。

答案 1 :(得分:0)

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