这是我的代码。我添加了
compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.0'
compile 'com.google.code.gson:gson:2.6.1'
compile 'com.squareup.okhttp3:okhttp:3.1.2'
与build.gradle
的依赖关系。
我已添加:
<uses-permission android:name="android.permission.INTERNET" />
表示。
这是一个代码:
public class MainActivity extends AppCompatActivity {
public static final String API_URL = "https://api.github.com";
public static class Contributor {
public final String login;
public final int contributions;
public Contributor(String login, int contributions) {
this.login = login;
this.contributions = contributions;
}
}
public interface GitHub {
@GET("/repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributors(
@Path("owner") String owner,
@Path("repo") String repo);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onAddClick(View view) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
GitHub github = retrofit.create(GitHub.class);
try {
Call<List<Contributor>> call = github.contributors("square", "retrofit");
List<Contributor> contributors = call.execute().body();
for (Contributor contributor : contributors) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
当我在行
上使用调试器运行此代码时Call<List<Contributor>> call = github.contributors("square", "retrofit");
我收到消息“不幸的是,MyAppName已停止”。我该如何解决?
更新
这是我的logcat
:
03-28 02:12:40.125 31118-31118/com.example.demas.customwidget A/art: art/runtime/thread.cc:1329] Throwing new exception 'length=215; index=630' with unexpected pending exception: java.lang.ArrayIndexOutOfBoundsException: length=215; index=630
03-28 02:12:40.125 31118-31118/com.example.demas.customwidget A/art: art/runtime/thread.cc:1329] at void com.example.demas.customwidget.MainActivity.onAddClick(android.view.View) (MainActivity.java:54)
03-28 02:12:40.125 31118-31118/com.example.demas.customwidget A/art: art/runtime/thread.cc:1329] at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
03-28 02:12:40.125 31118-31118/com.example.demas.customwidget A/art: art/runtime/thread.cc:1329] at void android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(android.view.View) (AppCompatViewInflater.java:288)
03-28 02:12:40.125 31118-31118/com.example.demas.customwidget A/art: art/runtime/thread.cc:1329] at boolean android.view.View.performClick() (View.java:5198)
03-28 02:12:40.125 31118-31118/com.example.demas.customwidget A/art: art/runtime/thread.cc:1329] at void android.view.View$PerformClick.run() (View.java:21147)
03-28 02:12:40.125 31118-31118/com.example.demas.customwidget A/art: art/runtime/thread.cc:1329] at void android.os.Handler.handleCallback(android.os.Message) (Handler.java:739)
03-28 02:12:40.125 31118-31118/com.example.demas.customwidget A/art: art/runtime/thread.cc:1329] at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:95)
03-28 02:12:40.125 31118-31118/com.example.demas.customwidget A/art: art/runtime/thread.cc:1329] at void android.os.Looper.loop() (Looper.java:148)
03-28 02:12:40.125 31118-31118/com.example.demas.customwidget A/art: art/runtime/thread.cc:1329] at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:5417)
03-28 02:12:40.125 31118-31118/com.example.demas.customwidget A/art: art/runtime/thread.cc:1329] at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
03-28 02:12:40.125 31118-31118/com.example.demas.customwidget A/art: art/runtime/thread.cc:1329] at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:726)
03-28 02:12:40.125 31118-31118/com.example.demas.customwidget A/art: art/runtime/thread.cc:1329] at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:616)
03-28 02:12:40.125 31118-31118/com.example.demas.customwidget A/art: art/runtime/thread.cc:1329]
03-28 02:12:40.178 31118-31118/com.example.demas.customwidget A/art: art/runtime/barrier.cc:90] Check failed: count_ == 0 (count_=-1, 0=0) Attempted to destroy barrier with non zero count
03-28 02:12:40.178 31118-31118/com.example.demas.customwidget A/art: art/runtime/runtime.cc:366] Runtime aborting --- recursively, so no thread-specific detail!
03-28 02:12:40.178 31118-31118/com.example.demas.customwidget A/art: art/runtime/runtime.cc:366]
03-28 02:12:40.178 31118-31118/com.example.demas.customwidget A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 31118 (as.customwidget)
答案 0 :(得分:3)
我修复了在Android Studio中禁用Instant run
模式的相同问题。
以下是有关该错误的讨论https://github.com/square/retrofit/issues/1486,另一种可能的解决方案是升级gradle插件版本。