我正在尝试使用Retrofit从网站获取数据。我在线跟踪了http://kdubblabs.com/java/retrofit-by-square/retrofit-simple-get/和http://corner.squareup.com/2013/05/retrofit-one-dot-oh.html的示例。我正在粘贴下面的代码 -
public class MainActivity extends ActionBarActivity {
public interface CatAPI{
@GET("/apj/category.php")
public String subCat();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@SuppressWarnings("deprecation")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Builder b=new RestAdapter.Builder();
Builder s=b.setServer("http://kurdshopping.net/");
RestAdapter adapter=s.build();
CatAPI api=adapter.create(CatAPI.class);
String response=api.subCat();
Log.i("Subcat", response);
return rootView;
}
}
}
请告诉我我做错了什么。 logcat显示以下错误 -
05-06 17:23:41.037: E/AndroidRuntime(3365): FATAL EXCEPTION: main
05-06 17:23:41.037: E/AndroidRuntime(3365): Process: com.example.retrofitdemo, PID: 3365
05-06 17:23:41.037: E/AndroidRuntime(3365): java.lang.VerifyError: retrofit/converter/GsonConverter
05-06 17:23:41.037: E/AndroidRuntime(3365): at retrofit.Platform$Android.defaultConverter(Platform.java:117)
05-06 17:23:41.037: E/AndroidRuntime(3365): at retrofit.RestAdapter$Builder.ensureSaneDefaults(RestAdapter.java:714)
05-06 17:23:41.037: E/AndroidRuntime(3365): at retrofit.RestAdapter$Builder.build(RestAdapter.java:707)
05-06 17:23:41.037: E/AndroidRuntime(3365): at com.example.retrofitdemo.MainActivity$PlaceholderFragment.onCreateView(MainActivity.java:82)
05-06 17:23:41.037: E/AndroidRuntime(3365): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
05-06 17:23:41.037: E/AndroidRuntime(3365): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
05-06 17:23:41.037: E/AndroidRuntime(3365): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
05-06 17:23:41.037: E/AndroidRuntime(3365): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-06 17:23:41.037: E/AndroidRuntime(3365): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
05-06 17:23:41.037: E/AndroidRuntime(3365): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
05-06 17:23:41.037: E/AndroidRuntime(3365): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
05-06 17:23:41.037: E/AndroidRuntime(3365): at android.app.Activity.performStart(Activity.java:5241)
05-06 17:23:41.037: E/AndroidRuntime(3365): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
05-06 17:23:41.037: E/AndroidRuntime(3365): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-06 17:23:41.037: E/AndroidRuntime(3365): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-06 17:23:41.037: E/AndroidRuntime(3365): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-06 17:23:41.037: E/AndroidRuntime(3365): at android.os.Handler.dispatchMessage(Handler.java:102)
05-06 17:23:41.037: E/AndroidRuntime(3365): at android.os.Looper.loop(Looper.java:136)
05-06 17:23:41.037: E/AndroidRuntime(3365): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-06 17:23:41.037: E/AndroidRuntime(3365): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 17:23:41.037: E/AndroidRuntime(3365): at java.lang.reflect.Method.invoke(Method.java:515)
05-06 17:23:41.037: E/AndroidRuntime(3365): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-06 17:23:41.037: E/AndroidRuntime(3365): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-06 17:23:41.037: E/AndroidRuntime(3365): at dalvik.system.NativeStart.main(Native Method)
如果有人能帮助我开始改造,那将是件好事。在此先感谢您的帮助。