我有我自己的寧靜網頁應用程序(spring引導),它產生JSON數據,我想獲取JSON數據並在我的android應用程序中顯示它。但是我得到一個NullPointerException和JsonMappingException。任何幫助,將不勝感激。在Android應用程序中使用寧靜的服務
Web應用程序控制器:
@RestController
@RequestMapping("/")
public class IndexController {
private ProductService productService;
@Autowired
public void setProductService(ProductService productService) {
this.productService = productService;
}
@RequestMapping(method = RequestMethod.GET , produces = MediaType.APPLICATION_JSON_VALUE)
public Iterable<Product> getAllProducts(){
return productService.listAllProducts();
}
}
JSON數據通過Web應用程序產生的(它的推移和回覆111通過流API獲取微博):
[{"productId":758916869776478208,"description":"LYS Türkiye 688’si ve 857.si Ömer Özyer’den Fethiye’nin Altın Kızları #haber #sondakika @habermatik aracılığıyla","name":"erkanilik48","location":"fethiye gazetesi sahibi"},{"productId":758916869961101316,"description":"I'm at Balıkesir in Balıkesir, Türkiye","name":"onplaka","location":"Bursa-Balıkesır"},{"productId":758917112295362560,"description":"Erdoğan'sız Türkiye diyorlar anket yaptım bakalım ahali nasıl Türkiye istiyor anket süresi 10 dakika .","name":"TakipGalaxi","location":"New York , LITTLE ITALY"},{"productId":758917117353668610,"description":"RT @TATARKENAN23: AğlarkenSeni Türkiye ye götüreceğiz denildiğinde susan çocuklar varHalep için DÛA bekliyoruz","name":"NecmeddinLd","location":null}]
Android應用MainActivity:
@Override
protected void onStart() {
super.onStart();
new HttpRequestTask().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_refresh) {
new HttpRequestTask().execute();
return true;
}
return super.onOptionsItemSelected(item);
}
private class HttpRequestTask extends AsyncTask<Void, Void, Greeting> {
@Override
protected Greeting doInBackground(Void... params) {
try {
final String url = "blabla";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
Greeting greeting;
greeting = restTemplate.getForObject(url, Greeting.class);
return greeting;
} catch (Exception e) {
Log.e("MainActivity", e.getMessage(), e);
}
return null;
}
@Override
protected void onPostExecute(Greeting greeting) {
TextView text1 = (TextView) findViewById(R.id.textView);
TextView text2 = (TextView) findViewById(R.id.textView2);
TextView text3 = (TextView) findViewById(R.id.textView3);
TextView text4 = (TextView) findViewById(R.id.textView4);
assert text1 != null;
text1.setText((int) greeting.getProductId());
assert text2 != null;
text2.setText(greeting.getDescription());
assert text3 != null;
text3.setText(greeting.getName());
assert text4 != null;
text4.setText(greeting.getLocation());
}
}
}
CONSOLE LOG:
[ 07-29 06:50:00.665 2886: 2932 D/ ]
HostConnection::get() New Host Connection established 0xae552980, tid 2932
07-29 06:50:00.706 2886-2932/org.hello.tweetconsumer I/OpenGLRenderer:
Initialized EGL, version 1.4
07-29 06:50:00.740 2886-2932/org.hello.tweetconsumer W/EGL_emulation: eglSurfaceAttrib not implemented
07-29 06:50:00.740 2886-2932/org.hello.tweetconsumer W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaaac9420, error=EGL_SUCCESS
07-29 06:50:02.663 2886-2931/org.hello.tweetconsumer E/MainActivity: Could not read JSON: Can not deserialize instance of org.hello.tweetconsumer.Greeting out of START_ARRAY token
at [Source: buffer([email protected]43f53).inputStream(); line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.hello.tweetconsumer.Greeting out of START_ARRAY token
at [Source: buffer([email protected]43f53).inputStream(); line: 1, column: 1]
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of org.hello.tweetconsumer.Greeting out of START_ARRAY token
at [Source: buffer([email protected]43f53).inputStream(); line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.hello.tweetconsumer.Greeting out of START_ARRAY token
at [Source: buffer([email protected]43f53).inputStream(); line: 1, column: 1]
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readInternal(MappingJackson2HttpMessageConverter.java:126)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:147)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:76)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:484)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:237)
at org.hello.tweetconsumer.MainActivity$HttpRequestTask.doInBackground(MainActivity.java:90)
at org.hello.tweetconsumer.MainActivity$HttpRequestTask.doInBackground(MainActivity.java:82)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.hello.tweetconsumer.Greeting out of START_ARRAY token
at [Source: buffer([email protected]43f53).inputStream(); line: 1, column: 1]
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:691)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:685)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1215)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:126)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2158)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readInternal(MappingJackson2HttpMessageConverter.java:123)
... 13 more
07-29 06:50:02.664 2886-2886/org.hello.tweetconsumer D/AndroidRuntime: Shutting down VM
--------- beginning of crash
07-29 06:50:02.665 2886-2886/org.hello.tweetconsumer E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.hello.tweetconsumer, PID: 2886
java.lang.NullPointerException: Attempt to invoke virtual method 'long org.hello.tweetconsumer.Greeting.getProductId()' on a null object reference
at org.hello.tweetconsumer.MainActivity$HttpRequestTask.onPostExecute(MainActivity.java:107)
at org.hello.tweetconsumer.MainActivity$HttpRequestTask.onPostExecute(MainActivity.java:82)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
檢查您創建的模型以反序列化您的JSON,並確保您的屬性名稱遵循Camel-Case中的Oracle文檔,除此之外,您還需要明確提及它們作爲@Seralizedname in m奧德爾。 我假設你正在使用某種GSON解串器。 –
你在期待什麼?問候或可迭代?您不能將Iterable 映射爲Greeting實體。 –
Celtik
真的我會改變, – Didem