【问题标题】:Disecting response from server to get what you need剖析来自服务器的响应以获取您需要的内容
【发布时间】:2018-05-31 13:26:45
【问题描述】:

您好,我正在使用 GET 请求成功地从我的数据库中检索数据。

在我的数据库中,我有 3 个字段 - employeeId, employeeName, userPin

但是,当我从我的 android 应用程序中收到响应消息时,我只想看到 userPin

这是我的代码:

String url = "http://xxx.xx.xxx.xxx:7001/engAppApi/webservices/usersTable/"+ id;

            URL object = new URL(url);

            HttpURLConnection con = (HttpURLConnection) object.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setRequestProperty("Content-Type", "application/json");
            con.setRequestProperty("Accept", "application/json");
            con.setRequestProperty("Authorization", "Bearer " +token);
            con.setRequestProperty
            con.setRequestMethod("GET");

            Log.d("responsecode", String.valueOf(con.getResponseCode()));
            Log.d("responsemessage", String.valueOf(con.getHeaderFields()));

            if (con.getResponseCode() == HttpsURLConnection.HTTP_OK || con.getResponseCode() == HttpsURLConnection.HTTP_ACCEPTED ) {
                String line;
                BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream()));
                while ((line=br.readLine()) != null) {
                    userPinRetrieved+=line;
                }
            }

        } catch (Exception e) {
            Log.v("ErrorAPP", e.toString());
        }
        return "";
    }

邮递员结果:

<?xml version="1.0" encoding="UTF-8"?>
<usersTable>
    <employeeId>2</employeeId>
    <employeeName>Tom Williams                  </employeeName>
    <userPin>666666</userPin>
</usersTable>

谁能帮帮我?

【问题讨论】:

  • 您忘记告诉我们 API 返回的内容。 JSON? XML?亚美尔?纯文本?熊猫?考拉?桉树?袜子?
  • @DavidConrad 我认为它返回 XML/JSON
  • 您可以简单地卷曲 URL 并找出它返回的内容。
  • @DavidConrad 我使用过邮递员,它会返回员工 ID、姓名和密码。但我想将引脚分配给变量
  • 是的,它返回它们,但是以什么格式?您为什么不编辑您的问题并将邮递员的实际结果放入其中。

标签: java android get httpurlconnection httpresponse


【解决方案1】:

我建议您将retrofit 用于android,因为只解析必要的值会很容易。例如,在我的项目中,我需要获取用户统计信息。为此,我创建了价值模型:

@SerializedName("user_stats")
@Expose
private List<MyStatsModel> myStats = null;

get方法

public List<MyStatsModel> getMyStats() {
    return myStats;
}

并在我的 onResponse 方法中使用此值轻松实现我想要的一切:

 @Override
        public void onResponse(Call<Model> call, Response<Model> response) {
            if(response.isSuccessful()) {
                    if(response.body().getMyStats() == null) {
                        emptyTextStats.setVisibility(View.VISIBLE);
                        statsRecyclerView.setVisibility(View.GONE);
                    }
                    else {
                        Log.d("tag", "stats response null" + MainActivity.getSid());
                        emptyTextStats.setVisibility(View.GONE);
                        statsRecyclerView.setVisibility(View.VISIBLE);
                        statsAdapter.updateStats(response.body().getMyStats());
                    }
                }
            }

另外,site 将帮助您将 JSON 解析为 Model 和 java 对象

【讨论】:

  • 我发现改造很混乱,所以我使用了简单的 httpurlconnection
  • 我同意你的观点,但是改造是非常有用的技术,所以,无论如何,你必须学习它。但是,可能会更晚
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-16
  • 2013-08-05
  • 1970-01-01
  • 2016-10-08
  • 2011-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多