Android HttpURLConnection POST getInputStream响应数据不返回数组内容

时间:2016-12-16 22:05:22

标签: android post httpurlconnection

我正在尝试在执行" POST"之后获取数据使用getInputStream()请求。我正在获取数据。但随着" Postman"我看到没有收到部分数据。

关注android: 的 {&#34;数据&#34; {&#34;公司&#34;:[],&#34;趋势&#34;:[],&#34;用户&#34;:[]} &#34;元&#34; {&#34; total_companies&#34;:1000,&#34; total_trends&#34;:5,&#34; TOTAL_USERS&#34;:11}} < / p>

但&#34; postman&#34;返回以下: 的 {&#34;数据&#34; {&#34;公司&#34;:[{&#34;名称&#34;:&#34;主持人&#34;&#34;描述&#34 ;日期null},{&#34;名称&#34;:&#34; Tesco的&#34;&#34;描述&#34;:空},{&#34;名称&#34;:&#34; Whitestrips(Crest)&#34;,&#34;说明&#34;:null}],&#34;趋势&#34;:[],&#34;用户&#34;:[{&#34; id& #34;:10,&#34;如first_name&#34;:&#34;试验&#34;&#34;姓氏&#34;:&#34;测试&#34;&#34;密码&#34; :&#34;&#34;&#34;后缀&#34;:空,&#34;两性&#34;:&#34; U&#34;&#34;电子邮件&#34;:&# 34; test@test.com",&#34; email_work&#34;:空,&#34;公司&#34;:空,&#34; Facebook和#34;:空,&#34;叽叽喳喳&# 34;:空,&#34; birth_date&#34;:&#34; 2016年6月14日&#34;&#34;国家&#34;:空,&#34;语言&#34;:空,& #34; is_admin&#34;:0,&#34; IS_ACTIVE&#34;:1,&#34;个性&#34;:空,&#34; degree_id&#34;:空,&#34; REGISTRATION_DATE&#34 ;:{&#34; date&#34;:&#34; 2016-12-07 14:57:26.000000&#34;,&#34; timezone_type&#34;:3,&#34; timezone&#34; :&#34; UTC&#34;}&#34; ideas_count&#34;:0,&#34; FOLLOWERS_COUNT&#34;:1,&#34; follows_count&#34;:0,&#34; is_foll由于&#34;:假,&#34; follows_trends_count&#34;:0,&#34; suggested_trends_count&#34;:0}]},&#34;元&#34; {&#34; total_companies&#34; :1000,&#34; total_trends&#34;:5,&#34; TOTAL_USERS&#34;:11}}

所以似乎没有收到公司和用户的阵列数据。 我正在关注POST请求:

HttpURLConnection urlConnection = null;

        try {
            URL url = new URL(apiUrl);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setUseCaches(false);
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            String userAuth = USERNAME + ":" + PASSWORD;
            String basicAuth = "Basic " + new String(Base64.encode(userAuth.getBytes(), Base64.NO_WRAP));
            Log.d("SerachApi", "User Auth is: " + basicAuth);
            urlConnection.setRequestProperty("Authorization", basicAuth);
            urlConnection.setRequestMethod("POST");

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("keyword", searchFor));
            OutputStream os = urlConnection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(getQuery(params));
            writer.flush();
            writer.close();
            os.close();


            urlConnection.connect();

            // Get response code
            int responseCode = urlConnection.getResponseCode();
            Log.d("SerachApi", "Response Code is: " + responseCode);

            // Read the input stream into a String
            InputStream inputStream = urlConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                    inputStream));
            List<String> response = new ArrayList<String>();

            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                response.add(line);
                Log.d ("aaaaa", line);
            }
            bufferedReader.close();

        } catch (IOException e){
            e.printStackTrace();
        }

提前致谢

1 个答案:

答案 0 :(得分:0)

代码是正确的,只是我为关键字设置了错误的值。