【发布时间】: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