2015-07-03 65 views
-2

你好,爲什麼我不能顯示從這個服務器的JSON響應? 第一個例子的工作: 代表性CLASS:不能,在Android應用程序中顯示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); 
} 

}

活性的方法,是相同的,但我只是改變了變數,並且網址:

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



    } 
} 

現在我看不到來自服務器的響應,應用程序調用良好,並得到我認爲的方法,但在屏幕上我有空的領域...請幫助任何人? Greetings

+0

請從後兩個URL響應.. – Akhil

回答

0

通常這意味着您的表示類不完全匹配JSON響應的結構。首先確保服務器返回有效的JSON,接下來,確保代表性類具有所有正確的字段名稱,並使用適當的大小寫。

+0

我明白了,謝謝。但我嘗試了很多與POJO中的響應字段的組合。也許,這是錯誤的Jacskon註釋的錯誤? (屬性,忽略)? – Jasjah

+0

也許張貼您試圖檢索的json – Broak

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()); 
相關問題