【问题标题】:Can't, show Json response in Android app不能,在 Android 应用中显示 Json 响应
【发布时间】:2015-07-03 16:23:30
【问题描述】:

您好,为什么我不能显示来自该服务器的 json 响应? 第一个示例工作: 代表类:

package com.rest.jasjah.twojaaura.com.rest.jasjah.twojaaura.pojo;

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({
    "one",
    "key"
})
public class ExampleOneTwo {

@JsonProperty("one")
private String one;
@JsonProperty("key")
private String key;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
 *
 * @return
 * The one
 */
@JsonProperty("one")
public String getOne() {
    return one;
}

/**
 *
 * @param one
 * The one
 */
@JsonProperty("one")
public void setOne(String one) {
    this.one = one;
}

/**
 *
 * @return
 * The key
 */
@JsonProperty("key")
public String getKey() {
    return key;
}

/**
 *
 * @param key
 * The key
 */
@JsonProperty("key")
public void setKey(String key) {
    this.key = key;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}

活动方法:

private class HttpRequestTask extends AsyncTask<Void, Void, ExampleOneTwo> {

    @Override
    protected ExampleOneTwo doInBackground(Void... params) {
        try {

            final String url = "http://echo.jsontest.com/key/value/one/two";
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            ExampleOneTwo exampleOneTwo = restTemplate.getForObject(url, ExampleOneTwo.class);
            return exampleOneTwo;
        } catch (Exception e) {
            Log.e("Fields", e.getMessage(), e);
        }

        return null;
    }
    @Override
    protected void onPostExecute(ExampleOneTwo exampleOneTwo) {

        TextView fieldTemp = (TextView) findViewById(R.id.txtGetTemp);
        TextView fieldWeather = (TextView) findViewById(R.id.txtGetWeather);
        fieldTemp.setText(exampleOneTwo.getKey());
        fieldWeather.setText(exampleOneTwo.getOne());



    }
}

这工作正常......但现在我在其他服务器上尝试这个,它只在应用程序中显示空字段。

代表类:

package com.rest.jasjah.twojaaura.com.rest.jasjah.twojaaura.pojo;

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)

public class Value {

@JsonProperty("id")
private Integer id;
@JsonProperty("quote")
private String quote;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
 *
 * @return
 * The id
 */
@JsonProperty("id")
public Integer getId() {
    return id;
}

/**
 *
 * @param id
 * The id
 */
@JsonProperty("id")
public void setId(Integer id) {
    this.id = id;
}

/**
 *
 * @return
 * The quote
 */
@JsonProperty("quote")
public String getQuote() {
    return quote;
}

/**
 *
 * @param quote
 * The quote
 */
@JsonProperty("quote")
public void setQuote(String quote) {
    this.quote = quote;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}

}

ACTIVITY METHOD ,是一样的,但我只改变了变量和 url :

private class HttpRequestTask extends AsyncTask<Void, Void, Value> {

    @Override
    protected Value doInBackground(Void... params) {
        try {

            final String url = "http://gturnquist-quoters.cfapps.io/api/random";
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            Value value = restTemplate.getForObject(url, Value.class);
            return value;
        } catch (Exception e) {
            Log.e("Fields", e.getMessage(), e);
        }

        return null;
    }
    @Override
    protected void onPostExecute(Value value) {

        TextView fieldTemp = (TextView) findViewById(R.id.txtGetTemp);
        TextView fieldWeather = (TextView) findViewById(R.id.txtGetWeather);
        fieldTemp.setText(value.getId().toString());
        fieldWeather.setText(value.getQuote());



    }
}

现在我看不到来自服务器的响应,应用程序调用良好并获得我认为的方法,但在屏幕上我有空字段...请帮助任何人? 问候

【问题讨论】:

  • 请从两个网址发布回复..

标签: android json jackson response resttemplate


【解决方案1】:

通常这意味着您的表示类与 JSON 响应的结构不完全匹配。首先确保服务器返回有效的 JSON,接下来,确保表示类具有所有正确的字段名称,并根据需要大写

【讨论】:

  • 我明白了,谢谢。但是我在 POJO 中尝试了很多与这个响应字段的组合。也许,这是 Jacskon 注释的错误? (属性,忽略)?
  • 也许发布你试图检索的 json
【解决方案2】:

好的,我解决了... 如果您需要获得更深领域的权限,则需要从上层类调用方法。 像这样:

公共类 ValueMain {

@JsonProperty("type")
private String type;
@JsonProperty("value")
private Value value;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String,   Object>();

/**
 *
 * @return
 * The type
 */
@JsonProperty("type")
public String getType() {
    return type;
}

/**
 *
 * @param type
 * The type
 */
@JsonProperty("type")
public void setType(String type) {
    this.type = type;
}

/**
 *
 * @return
 * The value
 */
@JsonProperty("value")
public Value getValue() {
    return value;
}

/**
 *
 * @param value
 * The value
 */
@JsonProperty("value")
public void setValue(Value value) {
    this.value = value;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}

}

现在我在活动中调用方法:

fieldTemp.setText(valueMain.getValue().getQuote()); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    相关资源
    最近更新 更多