我写了一个连接到我的数据库的简单Web应用程序。我在android上有一个应用程序,它发送一个获取数据库内容的请求。
一开始我使用普通的字符串,但很快意识到它没有给我任何东西,我必须使用由hibernate创建的类的对象
我的AsyncTask代码:
X
但是当我运行我的应用程序时,我收到了这个错误:
private class DatabaseLoaderTask extends AsyncTask<Void, Void, Void> {
TextView testText;
TacticsEntity content = new TacticsEntity();
@Override
protected void onPreExecute() {
testText = (TextView) findViewById(R.id.textView);
}
@Override
protected Void doInBackground(Void... urls) {
RestTemplate restTemplate = new RestTemplate();
content = restTemplate.getForObject("http://192.168.0.105:8080/SpringMVCApp/api/tactics", TacticsEntity.class);
runOnUiThread(new Runnable() {
@Override
public void run() {
testText.setText(content.getId());
}
});
return null;
}
}
在我看来,这可能是因为我的Web应用程序返回了一个数组:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of com.example.jarek.stratcaller.TacticsEntity out of START_ARRAY token
at [Source: buffer(com.android.okhttp.internal.http.HttpConnection$ChunkedSource@2325082d).inputStream(); line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.example.jarek.stratcaller.TacticsEntity out of START_ARRAY token
at [Source: buffer(com.android.okhttp.internal.http.HttpConnection$ChunkedSource@2325082d).inputStream(); line: 1, column: 1]
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:182)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:174)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:89)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:544)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:499)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:264)
at com.example.jarek.stratcaller.MainMenuActivity$DatabaseLoaderTask.doInBackground(MainMenuActivity.java:31)
at com.example.jarek.stratcaller.MainMenuActivity$DatabaseLoaderTask.doInBackground(MainMenuActivity.java:18)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.example.jarek.stratcaller.TacticsEntity out of START_ARRAY token
at [Source: buffer(com.android.okhttp.internal.http.HttpConnection$ChunkedSource@2325082d).inputStream(); line: 1, column: 1]
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:854)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:850)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1258)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:155)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:134)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3702)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2798)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:179)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:174)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:89)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:544)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:499)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:264)
at com.example.jarek.stratcaller.MainMenuActivity$DatabaseLoaderTask.doInBackground(MainMenuActivity.java:31)
at com.example.jarek.stratcaller.MainMenuActivity$DatabaseLoaderTask.doInBackground(MainMenuActivity.java:18)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
TacticsRepository类:
@ResponseBody
@RequestMapping(value = "/api/tactics", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
public Object[] listTacticsJson(ModelMap model) throws JSONException {
return tacticsRepository.findAll().toArray();
}
任何想法如何解决?