过去两个月我遇到了一个非常大的问题,因为我在Eclipse中使用Spring Android与Maven依赖,但是有一个错误,
NoClassDefFound:org.springframework.web.client.RestTemplate。
但我无法找到此错误的来源。它是什么?
错误:
06-03 12:46:44.102: ERROR/AndroidRuntime(1265): FATAL EXCEPTION: main
06-03 12:46:44.102: ERROR/AndroidRuntime(1265): java.lang.NoClassDefFoundError: org.springframework.web.client.RestTemplate
06-03 12:46:44.102: ERROR/AndroidRuntime(1265): at com.ex.newRest.newRest.onCreate(newRest.java:20)
06-03 12:46:44.102: ERROR/AndroidRuntime(1265): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-03 12:46:44.102: ERROR/AndroidRuntime(1265): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-03 12:46:44.102: ERROR/AndroidRuntime(1265): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-03 12:46:44.102: ERROR/AndroidRuntime(1265): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-03 12:46:44.102: ERROR/AndroidRuntime(1265): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-03 12:46:44.102: ERROR/AndroidRuntime(1265): at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 12:46:44.102: ERROR/AndroidRuntime(1265): at android.os.Looper.loop(Looper.java:123)
06-03 12:46:44.102: ERROR/AndroidRuntime(1265): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-03 12:46:44.102: ERROR/AndroidRuntime(1265): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 12:46:44.102: ERROR/AndroidRuntime(1265): at java.lang.reflect.Method.invoke(Method.java:507)
06-03 12:46:44.102: ERROR/AndroidRuntime(1265): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-03 12:46:44.102: ERROR/AndroidRuntime(1265): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-03 12:46:44.102: ERROR/AndroidRuntime(1265): at dalvik.system.NativeStart.main(Native Method)
代码:
package com.ex.newRest;
import org.springframework.web.client.RestTemplate;
import android.app.Activity;
import android.os.Bundle;
public class newRest extends Activity {
/** Called when the activity is first created. */
String postalCode;
String countryCode;
String radius;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RestTemplate restTemplate = new RestTemplate();
// The HttpComponentsClientHttpRequestFactory uses the
// org.apache.http package to make network requests.
// restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
// The URL for making the GET request.
final String url = "http://ws.geonames.org/findNearbyPostalCodesJSON?postalcode={postalCode}&country={countryCode}&radius={radius}";
String result = restTemplate.getForObject(url, String.class);
System.out.println("Response->"+result);
// Initiate the HTTP GET request, expecting an array of State
// objects in response.
//PostalCodes result = restTemplate.getForObject(
// url, PostalCodes.class, this.postalCode, this.countryCode,
//this.radius);
}
}