[英]Getting just a string from JSON api in JAVA on Android studio
在此链接中: https : //uhunt.onlinejudge.org/api/uname2uid/felix_halim该api返回的只是一个字符串!
像这样:
339
我尝试过这种方式,但这不起作用:
String link = "https://uhunt.onlinejudge.org/api/uname2uid/felix_halim"; // getting userID
Log.d("tttt", link);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, link, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
String inline = response.toString();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.d("tttt", "error");
}
});
我还要在Android Studio 3.1上使用此功能,并使用库“'com.android.volley:volley:1.1.0'”
请帮我! 任何帮助将不胜感激。 谢谢。
快速浏览API,看起来您正在访问的端点中仅包含字符串“ 339”(在浏览器中打开链接)。
通过Postman REST Client向该端点发送GET请求也得到339,表明这确实是后端方面的问题。
其他端点的响应为JSON格式。 尝试另一个端点,例如: https : //uhunt.onlinejudge.org/api/contests/id/10 ,您将看到您具有有效的JSONObject。
修复后端的问题,以该端点的JSON格式显示数据,并且您在前端的响应也应反映出相同的结果。
我不知道您的服务端点是否正确,但它确实返回339。这显然意味着您的服务api没有返回json字符串。 您的首要任务应该是获取正确的端点。 一旦有了正确的端点,请按照以下提到的步骤从json字符串获取Java对象。
例如,您具有以下要转换为Java对象的json字符串:
{"firstname":"Ashwani","lastname":"Kumar","age":"30"}
添加Jackson解析器依赖项以包含在gradle文件中。
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.5'
compile 'com.fasterxml.jackson.core:jackson-core:2.8.5'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.8.5'
在gradle文件的android标签中排除META-INF / LICENSE,以避免Android Studio中的dup许可证文件错误
packagingOptions {
exclude 'META-INF/LICENSE'
}
为您的json字符串解析创建模型。 使用以下服务自动生成模型: http : //www.jsonschema2pojo.org/
将您的json字符串粘贴到中,然后在源类型中选择JSON。 单击预览或下载按钮以获取生成的Java类。 在这种情况下,我们得到以下java类:
package com.example;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"firstname",
"lastname",
"age"
})
public class Person {
@JsonProperty("firstname")
private String firstname;
@JsonProperty("lastname")
private String lastname;
@JsonProperty("age")
private String age;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("firstname")
public String getFirstname() {
return firstname;
}
@JsonProperty("firstname")
public void setFirstname(String firstname) {
this.firstname = firstname;
}
@JsonProperty("lastname")
public String getLastname() {
return lastname;
}
@JsonProperty("lastname")
public void setLastname(String lastname) {
this.lastname = lastname;
}
@JsonProperty("age")
public String getAge() {
return age;
}
@JsonProperty("age")
public void setAge(String age) {
this.age = age;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
使用以下代码将json字符串解析为java对象:
//Example Json String
String personJsonStr = "{\"firstname\":\"Ashwani\",\"lastname\":\"Kumar\",\"age\":\"30\"}";
//Pass the jason string and model class you want to convert you json string into
Person person = mapper.readValue(personJsonStr, Person.class);
// read from json string
String firstName = person.getFirstname();
String lastName = person.getLastname();
String age = person.getAge();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.