使用 REST TEMPLATE 时从 JSON 获取 ID
Get ID from JSON when using REST TEMPLATE
我正在为我的网络使用 Resttemplate。它工作得很好,但我不知道如何从服务器响应中准确提取特定参数。
响应正文如下所示:
{
"userdata": {
"id": "9981",
"userid": "5a01cda4a00a3",
"login": "yikes@test.de",
"name": "yikes",
"vorname": "yikes",
"passwort": "3136b44e1c508d36ae4abf6f1160f779",
"email": "yikes@test.de",
"reg_date": "2017-11-07 16:13:40",
等等……
编辑:
try {
RestTemplate template = new RestTemplate();
final String Login = "https://app.pengueen.de//api/v1/login/?";
final String username1 = "username";
final String password1 = "password";
final String apikey = "api_key";
Uri builtUri = Uri.parse(Login)
.buildUpon()
.appendQueryParameter(username1, username.getText().toString())
.appendQueryParameter(password1, password.getText().toString())
.appendQueryParameter(apikey, "awd-DFFDD-dsse")
.build();
String url = (builtUri.toString());
//Umwandelnb damit url conevter nichts abfuckt
URI uri = new URI(url);
//Error Handeler
template.setErrorHandler(new DefaultResponseErrorHandler(){
protected boolean hasError(HttpStatus statusCode) {
return false;
}});
//Response von Post bekommen
response = template.postForEntity(uri, null, String.class);
HttpStatus status = response.getStatusCode();
String restCall = response.getBody();
} catch (URISyntaxException e) {
e.printStackTrace();
}
if (response.getStatusCode().toString().equals("200")) {
/
/ 在这里我想获取 id 以便我可以将它传递给下一个 class!
Toast.makeText(getApplicationContext(), "Login erfolgreich", Toast.LENGTH_LONG).show();
Intent intent=new Intent(LoginScreen9.this,SideMenu1.class);
intent.putExtra("loginy","true");
intent.putExtra("id",response.getBody());
startActivity(intent);
}
提前致谢!
好的,尝试使用 JSONObject :
String restCall = response.getBody();
JSONObject jObject = new JSONObject(restCall);
JSONObject userObject = jObject.getJSONObject("userdata");
String id = userObject.getString("id");
我正在为我的网络使用 Resttemplate。它工作得很好,但我不知道如何从服务器响应中准确提取特定参数。
响应正文如下所示:
{
"userdata": {
"id": "9981",
"userid": "5a01cda4a00a3",
"login": "yikes@test.de",
"name": "yikes",
"vorname": "yikes",
"passwort": "3136b44e1c508d36ae4abf6f1160f779",
"email": "yikes@test.de",
"reg_date": "2017-11-07 16:13:40",
等等……
编辑:
try {
RestTemplate template = new RestTemplate();
final String Login = "https://app.pengueen.de//api/v1/login/?";
final String username1 = "username";
final String password1 = "password";
final String apikey = "api_key";
Uri builtUri = Uri.parse(Login)
.buildUpon()
.appendQueryParameter(username1, username.getText().toString())
.appendQueryParameter(password1, password.getText().toString())
.appendQueryParameter(apikey, "awd-DFFDD-dsse")
.build();
String url = (builtUri.toString());
//Umwandelnb damit url conevter nichts abfuckt
URI uri = new URI(url);
//Error Handeler
template.setErrorHandler(new DefaultResponseErrorHandler(){
protected boolean hasError(HttpStatus statusCode) {
return false;
}});
//Response von Post bekommen
response = template.postForEntity(uri, null, String.class);
HttpStatus status = response.getStatusCode();
String restCall = response.getBody();
} catch (URISyntaxException e) {
e.printStackTrace();
}
if (response.getStatusCode().toString().equals("200")) {
/ / 在这里我想获取 id 以便我可以将它传递给下一个 class!
Toast.makeText(getApplicationContext(), "Login erfolgreich", Toast.LENGTH_LONG).show();
Intent intent=new Intent(LoginScreen9.this,SideMenu1.class);
intent.putExtra("loginy","true");
intent.putExtra("id",response.getBody());
startActivity(intent);
}
提前致谢!
好的,尝试使用 JSONObject :
String restCall = response.getBody();
JSONObject jObject = new JSONObject(restCall);
JSONObject userObject = jObject.getJSONObject("userdata");
String id = userObject.getString("id");