我是 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/