无法在Android应用中显示Json响应

时间:2015-07-03 16:23:31

标签: android json jackson response resttemplate

您好我为什么无法从此服务器显示我的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());



    }
}

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

公共类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());