【问题标题】:Android HttpURLConnection POST getInputStream response data not returns array contentAndroid HttpURLConnection POST getInputStream 响应数据不返回数组内容
【发布时间】:2016-12-16 22:05:22
【问题描述】:

我试图在使用 getInputStream() 执行“POST”请求后获取数据。我正在获取数据。但是对于“邮递员”,我看到那部分数据没有收到。

在 android 上获得关注: {"data":{"companies":[],"trends":[],"users":[]},"meta":{"total_companies":1000,"total_trends":5,"total_users ":11}}

但“邮递员”返回以下内容: {"data":{"companies":[{"name":"Hostess","description":null},{"name":"Tesco","description":null},{"name" :"Whitestrips (Crest)","description":null}],"trends":[],"users":[{"id":10,"first_name":"Test","last_name":"test" ,"密码":"","后缀":null,"性别":"U","email":"test@test.com","email_work":null,"company":null,"facebook": null,"twitter":null,"birth_date":"2016-06-14","country":null,"language":null,"is_admin":0,"is_active":1,"personality":null, "degree_id":null,"registration_date":{"date":"2016-12-07 14:57:26.000000","timezone_type":3,"timezone":"UTC"},"ideas_count":0," Followers_count":1,"follows_count":0,"is_following":false,"follows_trends_count":0,"suggested_trends_count":0}]},"meta":{"total_companies":1000,"total_trends":5," total_users":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();
        }

提前致谢

【问题讨论】:

  • 代码是正确的,只是我为关键字输入了错误的值。

标签: android post httpurlconnection


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-11
    • 2011-09-05
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    • 2013-09-15
    相关资源
    最近更新 更多