java.lang.IllegalStateException : Could not execute method of the activity while executing Retrofit GET method in Android studio

标签 java android android-studio retrofit

我是 Android 应用程序开发新手。我研究了一些在线教程和 wiki,并开始开发应用程序,该应用程序将调用 Apache 服务器(PHP+SLIM+MySQL)上托管的 API。我已经创建了一些API 并使用适用于 Chrome 的 RESTClient 成功对其进行了测试。

对于android,经过一些研究,我决定使用Square的改造库,它似乎工作非常有效(比Android中实现的Apache.http协议(protocol)快5-6倍,比Volley(Google)和)快2-3倍Robospice)和非常轻的内存占用。

我创建了一个示例 Android 项目(使用 Android studio 0.8 beta)并按照他们的文档实现了 Rertofit。但是当我编译我的项目并将我的 apk 烧录到我的手机时...它无法执行“java.lang.IllegalStateException”。以下是我的实现的详细信息..

我不知道如何纠正这个错误...我是否在 list 文件中遗漏了任何内容,或者没有实现我应该实现的任何方法...

我包含的库(包括所有 gradle 依赖项)

  • Gson 2.3
  • 改造1.6.1
  • okhttp 2.0
  • okhttp-urlconnection 2.0
  • okio 1.0.1

结构

public class MyStructures {
                  public static class Contributor {
                    public String login;
                    public int contributions;
                  }
               }

界面

  public interface MyInterface {

            @GET("/repos/{owner}/{repo}/contributors")
            List<Contributor> contributors(
                    @Path("owner") String owner,
                    @Path("repo") String repo
            );
        }

适配器创建、调用REST方法、获取结果

private static final String API_URL = "https://api.github.com";    
RestAdapter restAdapter = new RestAdapter.Builder()
                        .setEndpoint(API_URL)
                        .build();

                // Create an instance of our GitHub API interface.
                MyInterface myInterface = restAdapter.create(MyInterface.class);


                // Fetch and print a list of the contributors to this library.
                List<Contributor> contributors = myInterface.contributors("square", "retrofit");
                for (Contributor contributor : contributors) {
                  System.out.println(contributor.login + " (" + contributor.contributions + ")");

               }

list 文件:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication" >
    <!-- added this two permission by looking in to some answer on stackoverflow 
    which i geuss not required for newer android version since googles has given n/w permission by default-->
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MyActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

最佳答案

我第一次遇到这个问题是通过在 list 中添加以下权限来解决的

<uses-permission android:name="android.permission.INTERNET" /> 

关于java.lang.IllegalStateException : Could not execute method of the activity while executing Retrofit GET method in Android studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25740236/

相关文章:

java - 在 @Async 调用中正确使用 EntityManager

java - 局部变量的 UML

android - 使用 Kotlin 进行数据绑定(bind)会导致 Resources$NotFoundException

android-studio - 如何在 Android Studio 中使用 Flutter RP2 清理您的构建?

android - Android Studio AVD Manager 中的 SD 卡路径更改重置

java - 在 ImageView 上绘图

java - 为什么不支持向 java.time.Instant 添加周数?

android - smoothScrollToPositionFromTop() 并不总是像它应该的那样工作

android - Cordova /平台/android/gradlew : Command failed with exit code 1 Error output:

java - 登录模式 - Android Studio