#java #android
#java #Android
Вопрос:
я создал сервис
открытый класс JsonSpiceService расширяет SpringAndroidSpiceService{
@Override
public RestTemplate createRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
// find more complete examples in RoboSpice Motivation app
// to enable Gzip compression and setting request timeouts.
// web services support json responses
MappingJacksonHttpMessageConverter jsonConverter =
new MappingJacksonHttpMessageConverter();
FormHttpMessageConverter formHttpMessageConverter =
new FormHttpMessageConverter();
StringHttpMessageConverter stringHttpMessageConverter =
new StringHttpMessageConverter();
final List<HttpMessageConverter<?>> listHttpMessageConverters =
restTemplate.getMessageConverters();
listHttpMessageConverters.add(jsonConverter);
listHttpMessageConverters.add(formHttpMessageConverter);
listHttpMessageConverters.add(stringHttpMessageConverter);
restTemplate.setMessageConverters(listHttpMessageConverters);
// @see http://sapandiwakar.in/eofexception-with-spring-rest-template-android/
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
return restTemplate;
}
@Override
public CacheManager createCacheManager(Application application)
throws CacheCreationException {
CacheManager cacheManager = new CacheManager();
JacksonObjectPersisterFactory jacksonObjectPersisterFactory =
new JacksonObjectPersisterFactory(application);
cacheManager.addPersister(jacksonObjectPersisterFactory);
return cacheManager;
}
}
затем я создал запрос, подобный этому
открытый класс PractitionerRequest расширяет SpringAndroidSpiceRequest{
final String TAG="ROBOSPICE";
public PractitionerRequest() {
super(PractList.class);
// TODO Auto-generated constructor stub
}
@Override
public PractList loadDataFromNetwork() throws Exception{
URL url = new URL("http://svv.in.net/service/test.php");
Log.d(TAG, "" url);
return getRestTemplate().getForObject(url.toURI(), PractList.class);
//return getRestTemplate().getForObject(url, PractList.class);
}
}
и это мой класс POJO
public class PractList {
String name;
String hobbies;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHobbies() {
return hobbies;
}
public void setHobbies(String hobbies) {
this.hobbies = hobbies;
}
}
я вызываю это в Activity
SpiceManager SpiceManager = новый SpiceManager(JsonSpiceService.class );
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "starting service");
spiceManager.start(this);
}
@Override
protected void onStop() {
if( spiceManager.isStarted() ) {
spiceManager.shouldStop();
Log.d(TAG, "stopping service");
}
super.onStop();
}
private void performRequest() {
AdminActivity.this.setProgressBarIndeterminateVisibility(true);
PractitionerRequest request = new PractitionerRequest();
spiceManager.execute(request, "sss", DurationInMillis.ONE_MINUTE, new PractRequestListerner());
}
private void roboSpice() {
performRequest();
Log.d(TAG, "started");
/*Log.d(TAG, "started");
PractitionerRequest request=new PractitionerRequest();
spiceManager.execute(request,"Json",DurationInMillis.ONE_MINUTE, new PractitionerRequestListener());
Log.d(TAG, "started:" request.toString());
Log.d(TAG, "end");*/
/*new PractRequestListerner();
if (isPageInCache()) {
showMsg("The page is already cached.");
Log.d(TAG, "The page is already cached");
}
*/
}
private boolean isPageInCache() {
String pageName="yes";
try {
PractitionerRequest request=new PractitionerRequest();
spiceManager.execute(request,"Json",DurationInMillis.ONE_MINUTE, new PractitionerRequestListener());
} catch (Exception e) {
return false;
}
return true;
}
public void onRequestSuccess(PractList practList) {
// setProgressBarIndeterminateVisibility(false);
Log.d(TAG, "*******************");
Log.e("TEST",practList.getName());
Log.e("TEST",practList.getHobbies());
}
public void onRequestFailure(SpiceException spiceException) {
setProgressBarIndeterminateVisibility(false);
final String msg = spiceException.getCause().getMessage();
showMsg("Error: " msg);
}
private void showMsg(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
class PractRequestListerner implements RequestListener<PractList>{
@Override
public void onRequestFailure(SpiceException e) {
Log.e("TEST","fail");
Log.e("TEST","fail" e.getMessage());
}
@Override
public void onRequestSuccess(PractList list) {
Log.e("TEST","succes");
Log.e("TEST","fail" list.getName());
}
}
но я получаю эти ошибки, не получая точной ошибки
Ответ №1:
Эти классы находятся в разных jar-файлах — если у вас есть jackson-databind.jar
без jackson-core.jar
, вы получите эту ошибку.
Если вы используете maven, добавьте это в свой pom:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.1.1</version>
</dependency>
Если нет, вы можете получить jar здесь: