所以Retrofit 2.0.0最近发布了,并没有真正的任何更新示例如何使用它,但我试图实现它的基本API调用.我得到了一个
java.lang.IllegalArgumentException: Unable to create converter for class`
Run Code Online (Sandbox Code Playgroud)
引起的
Caused by: java.lang.IllegalArgumentException: Could not locate converter for class orbyt.app.dataclass. Tried:
* retrofit.OkHttpBodyConverterFactory
Run Code Online (Sandbox Code Playgroud)
在尝试进行api调用时.
我正在从URL检索XML feed然后解析它.我需要做的是将内部存储到手机中,这样当没有互联网连接时,它可以解析保存的选项而不是实时选项.
我面临的问题是我可以创建url对象,使用getInputStream来获取内容,但它不会让我保存它.
URL url = null;
InputStream inputStreamReader = null;
XmlPullParser xpp = null;
url = new URL("http://*********");
inputStreamReader = getInputStream(url);
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFileAppeal.srl"));
//--------------------------------------------------------
//This line is where it is erroring.
//--------------------------------------------------------
out.writeObject( inputStreamReader );
//--------------------------------------------------------
out.close();
Run Code Online (Sandbox Code Playgroud)
任何想法如何保存输入流,以便我以后加载它.
干杯
我需要在我的应用程序中使用Retrofit库下载所有类型的文件(二进制文件,图像,文本等).网上的所有示例都使用HTML GET方法.我需要使用POST来防止自动缓存.
我的问题是如何在Retrofit中使用POST方法下载文件?
我有一个Kotlin协程和改造项目。
我有这些依赖项:
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
Run Code Online (Sandbox Code Playgroud)
今天,我在项目中将Retrofit更新为2.6.0。在https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter中,它已被弃用。在https://github.com/square/retrofit/blob/master/CHANGELOG.md#version-260-2019-06-05中写道Retrofit当前支持suspend。
因此,我删除了该文件,retrofit2-kotlin-coroutines-adapter:0.9.2并在Retrofit客户端中更改了以下几行:
retrofit = Retrofit.Builder()
.baseUrl(SERVER_URL)
.client(okHttpClient)
.addConverterFactory(MyGsonFactory.create(gson))
//.addCallAdapterFactory(CoroutineCallAdapterFactory()) - removed it.
.build()
Run Code Online (Sandbox Code Playgroud)
运行时,第一个请求捕获异常:
java.lang.IllegalArgumentException: Unable to create call adapter for kotlinx.coroutines.Deferred<com.package.model.response.UserInfoResponse>
for method Api.getUserInfo
Run Code Online (Sandbox Code Playgroud)
据我了解,不是CoroutineCallAdapterFactory()可以使用CallAdapter.Factory(),而是抽象的。
如果在Api类中,我更改了suspend开头添加的请求:
@FormUrlEncoded
@POST("user/info/")
suspend fun getUserInfo(@Field("token") token: String): Deferred<UserInfoResponse>
override suspend fun getUserInfo(token: String): Deferred<UserInfoResponse> =
service.getUserInfo(token)
Run Code Online (Sandbox Code Playgroud)
我得到这个例外:
java.lang.RuntimeException: Unable to invoke no-args constructor for kotlinx.coroutines.Deferred<com.package.model.response.UserInfoResponse>. Registering an …Run Code Online (Sandbox Code Playgroud) 我正在使用 Kotlin Android 应用程序访问 API 端点。在此 API 调用中,我返回一个字节数组。我想看到的是一种将字节数组转换为 pdf 文件并将其保存在手机下载文件夹中的方法。
private suspend fun getIDCard(sessionID: String?, carriermemid: String?): ByteArray? {
var results: String = ""
val postData = "{\"sessionid\": \" $sessionID\"}"
val outputStreamWriter: OutputStreamWriter
var byteArray: ByteArray? = null
val url: URL = URL(idURL + carriermemid)
val conn: HttpURLConnection = url.openConnection() as HttpURLConnection
try {
conn.setRequestProperty(apiConfig.typeKey, apiConfig.typeValueJSON)
conn.setRequestProperty("Accept", "application/json")
conn.setRequestProperty("sessionid", sessionID)
conn.requestMethod = apiConfig.methodGet
val responseCode: Int = conn.responseCode
println("Response Code :: $responseCode")
//returning 404
return if (responseCode == HttpURLConnection.HTTP_OK) {// …Run Code Online (Sandbox Code Playgroud)