【问题标题】:Getting just a string from JSON api in JAVA on Android studio仅从 Android Studio 上的 JAVA 中的 JSON api 获取字符串
【发布时间】:2018-05-27 09:59:22
【问题描述】:

在这个链接中: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'”的另一件事

请帮帮我!任何形式的帮助将不胜感激。谢谢。

【问题讨论】:

标签: java android json android-volley


【解决方案1】:

快速浏览 API,您访问的端点中似乎只有字符串“339”(在浏览器中打开链接)。

通过 Postman REST Client 向该端点发送 GET 请求也会得到 339,说明这确实是后端问题。

其他端点的响应为 JSON 格式。尝试另一个端点,例如:https://uhunt.onlinejudge.org/api/contests/id/10,你会看到你有一个有效的 JSONObject。

解决后端问题,以 JSON 格式显示此端点的数据,并且您在前端的响应应该反映相同。

【讨论】:

  • 他被问到要在他的 android 项目中更改什么,以便他可以解析字符串。这个答案没有回答问题。
【解决方案2】:

我不知道您的服务端点是否正确,但它确实返回 339。这显然意味着您的服务 api 没有返回 json 字符串。您的首要任务应该是获得正确的端点。一旦你有了正确的端点,请按照下面提到的步骤从 json 字符串中获取 Java 对象。

例如,您有以下要转换为 Java 对象的 json 字符串:

{"firstname":"Ashwani","lastname":"Kumar","age":"30"}

添加要包含在 gradle 文件中的 Jackson 解析器依赖项。

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 license文件错误

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 2020-02-11
    • 2019-03-16
    相关资源
    最近更新 更多