我只是想在Android应用程序上使用Retrofit做一个简单的小例子。 但我总是这样做"改造。改造错误"我的logcat中的错误,我无法理解为什么。 有没有人有想法帮助我?
这是我的logcat:
06-29 11:56:03.771 2590-2590/SocialSupaCrew.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed 249K, 9% free 3202K/3512K, paused 38ms, total 40ms
06-29 11:56:03.831 2590-2590/SocialSupaCrew.myapplication D/AndroidRuntime﹕ Shutting down VM
06-29 11:56:03.831 2590-2590/SocialSupaCrew.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a4bba8)
06-29 11:56:03.861 2590-2590/SocialSupaCrew.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: SocialSupaCrew.myapplication, PID: 2590
java.lang.RuntimeException: Unable to start activity ComponentInfo{SocialSupaCrew.myapplication/com.android.myactivity}: retrofit.RetrofitError
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: retrofit.RetrofitError
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:394)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at com.android.$Proxy0.identities(Native Method)
at com.android.myactivity.onCreate(myactivity.java:37)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:136)
at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at com.android.$Proxy0.identities(Native Method)
at com.android.myactivity.onCreate(myactivity.java:37)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
06-29 11:56:07.891 2590-2590/SocialSupaCrew.myapplication I/Process﹕ Sending signal. PID: 2590 SIG: 9
myactivity.java:
public class myactivity extends Activity {
@InjectView(R.id.fullName) TextView fullName;
@InjectView(R.id.idRepo) TextView idRepo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
ButterKnife.inject(this);
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(GitHubClient.API_URL).build();
GitHubClient.GitHub gitHub = restAdapter.create(GitHubClient.GitHub.class);
//I think my error is here
List<GitHubClient.Identity> identitiesList = gitHub.identities("SocialSupaCrew", "PictureRepo");
for (GitHubClient.Identity identity : identitiesList){
fullName.setText("fullName : " + identity.fullName);
idRepo.setText("id = " + identity.id);
}
}
}
GitHubClient.java:
public class GitHubClient {
public static String API_URL = "https://api.github.com";
class Identity {
String fullName;
int id;
}
static interface GitHub {
@GET("/repos/{owner}/{repo}/")
List<Identity> identities (
@Path("owner") String owner,
@Path("repo") String repo
);
}
}
编辑:我已经尝试过AsyncTask,但我现在收到了这个错误
我的新logcat:
06-29 12:51:37.081 2680-2680/SocialSupaCrew.myapplication D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
06-29 12:51:38.211 2680-2693/SocialSupaCrew.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed 213K, 8% free 3239K/3512K, paused 36ms, total 36ms
06-29 12:51:39.761 2680-2693/SocialSupaCrew.myapplication W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0xb1a4bba8)
06-29 12:51:39.801 2680-2693/SocialSupaCrew.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: SocialSupaCrew.myapplication, PID: 2680
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:841)
Caused by: retrofit.RetrofitError: 404 Not Found
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:382)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at com.android.$Proxy0.identities(Native Method)
at com.android.myactivity$Connexion.doInBackground(myactivity.java:49)
at com.android.myactivity$Connexion.doInBackground(myactivity.java:37)
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:841)
06-29 12:51:43.581 2680-2680/SocialSupaCrew.myapplication I/Choreographer﹕ Skipped 34 frames! The application may be doing too much work on its main thread.
我的新活动:
public class myactivity extends Activity {
@InjectView(R.id.fullName) TextView fullName;
@InjectView(R.id.idRepo) TextView idRepo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
ButterKnife.inject(this);
Connexion connexion = new Connexion();
connexion.execute();
}
class Connexion extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute(){
}
@Override
protected String doInBackground(String... args){
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(GitHubClient.API_URL).build();
GitHubClient.GitHub gitHub = restAdapter.create(GitHubClient.GitHub.class);
List<GitHubClient.Identity> identitiesList = gitHub.identities("SocialSupaCrew", "PictureRepo");
for (GitHubClient.Identity identity : identitiesList){
fullName.setText("fullName : " + identity.fullName);
idRepo.setText("id = " + identity.id);
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
}
}
}