Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用改装库将X-API-Key和X-Session添加到标题?_Android_Kotlin_Retrofit_Retrofit2_Httpurlconnection - Fatal编程技术网

Android 如何使用改装库将X-API-Key和X-Session添加到标题?

Android 如何使用改装库将X-API-Key和X-Session添加到标题?,android,kotlin,retrofit,retrofit2,httpurlconnection,Android,Kotlin,Retrofit,Retrofit2,Httpurlconnection,对于我的多部分请求,我想使用改装库而不是HttpURLConnection,以将图像发送到服务器。由于某种原因,我的HttpURLConnection请求无法工作 但是我在改装方面遇到了问题,我不知道如何将会话令牌和API密钥添加到我的请求中 这是我用于HttpURLConnection的每个请求的解决方案(它适用于除多部分请求以外的所有请求): 这是我使用改装库的请求: val f = File(app.cacheDir, filename) f.createNewFile() val fo

对于我的
多部分请求
,我想使用
改装
库而不是
HttpURLConnection
,以将图像发送到服务器。由于某种原因,我的
HttpURLConnection
请求无法工作

但是我在
改装
方面遇到了问题,我不知道如何将
会话令牌
API密钥
添加到我的请求中

这是我用于
HttpURLConnection
的每个请求的解决方案(它适用于除多部分请求以外的所有请求):

这是我使用改装库的请求:

val f = File(app.cacheDir, filename)
f.createNewFile()

val fos = FileOutputStream(f)
fos.write(data)
fos.flush()
fos.close()

val reqFile = RequestBody.create(MediaType.parse("image/*"), f)
val body = MultipartBody.Part.createFormData("upload", f.name, reqFile)

val httpUrl = HttpUrl.Builder()
    .scheme("https")
    .host(mainUrl)
    .addPathSegment(apiSegment)
    .addQueryParameter("X-API-Key", apiType.apiKey)
    .addQueryParameter("X-Session", getServerSession())

val service = Retrofit.Builder().baseUrl(httpUrl.build()).build().create(Service::class.java)
val req = service.postImage(body)
val response = req.execute()
我使用了
addQueryParameter
,因为我了解到这是如何将这两个参数添加到标头的方法,但这只会影响API调用的URL,它会将API键和会话添加到URL,服务器根本无法识别

更新:

我的邮政服务界面:

internal interface MultipartService {
        @Multipart
        @POST("{url}")
        fun postImage(@Part image: MultipartBody.Part): Call<ResponseBody>
        fun setEndpoint(@Path("url") endpoint: String): Call<ResponseBody>
    }
内部接口多部件服务{
@多部分
@POST(“{url}”)
趣味postImage(@Part image:MultipartBody.Part):调用
fun setEndpoint(@Path(“url”)endpoint:String):调用
}
更新:已修复

   internal interface MultipartServicePost {
        @POST("{url}")
        @Multipart
        fun postImage(@Part image: MultipartBody.Part, @Path(value = "url", encoded = true) endpoint: String): Call<ResponseBody>
    }
内部接口MultipartServicePost{
@POST(“{url}”)
@多部分
趣味postImage(@Part image:MultipartBody.Part,@Path(value=“url”,encoded=true)端点:字符串):调用
}
addQueryParameter()
就像名字说的那样,用于在查询中添加参数

您需要的是一个
拦截器
。以下是一个例子:

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();  
httpClient.addInterceptor(new Interceptor() {  
    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {
        Request original = chain.request();

        Request request = original.newBuilder()
            .header("X-API-Key", apiType.apiKey)
            .header("X-Session", getServerSession())
            .method(original.method(), original.body())
            .build();

        return chain.proceed(request);
    }
}
关于动态URL,您可以在定义接口时定义动态路径段。例如:

public interface PostService {  

    @GET("/posts/{post_id}")
    Task getPost(@Path("post_id") long id);
}

这是有效的。我有一个关于URL传递的问题。有没有办法为我的API调用更改post URL?因为如果我使用HttpUrl和
.addPathSegment()
它会在段之间添加奇怪的
%2F
字符串。URL由“https:/”+baseURL+/apiVersion/+/apiFunction/+/name/组成,这三个是动态的,可以更改。所以我不能将它们添加到@Post内部服务界面。好吧,酷(别忘了投票并验证答案…)。关于你的评论,你的意思是你想要动态URL吗?当你定义你的眼睛时,这是可能的。几乎一切都是动态的。我可以更改整个API url(我使用多个API服务器和这个多部分请求),所以最好的方法是,我可以将一堆url部分附加到一个字符串中,并将其用作url。类似于HttpUrlConnection。我可以创建由多个URL部分粘合在一起的URL对象。示例:
val url=url(apiType.url+apiBasePath+apiPath+apiName+apiSubName)
我在Kotlin中尝试了它,但它导致了错误:
HTTP方法注释是必需的(例如,@GET,@POST等)
并且在接口中有
@POST({url}”)
(请参见编辑的问题)@GET(),@POST(),等是改造2的一部分。但是你不能做你发布的事情:
@POST(“{url}”)
必须写在你的
setEndpoint()方法的正上方
public interface PostService {  

    @GET("/posts/{post_id}")
    Task getPost(@Path("post_id") long id);
}