在改装请求中添加动态标题和正文

时间:2017-04-07 12:08:43

标签: android retrofit retrofit2

我正在使用Retrofit 2并使用令牌密钥保护API 所以我需要添加令牌来请求。

这是我的终点:

http://192.168.1.22:8000/api/auth/queue/store

POST请求改进:

@Multipart
@POST("auth/queue/store")
Call<ResponseBody> registerQueue(@Header("Authorization") String Authorization,@Part("task") RequestBody task);

活动:

    public void sendNetworkRequest(String token, String task) {
    String temp ="Bearer "+token;
        RequestBody taskBody = RequestBody.create(MediaType.parse("text/plain"), task);
        Log.d(TAG, "send Network Request: Task: "+task+" "+" "+ temp  );

        Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl("http://192.168.1.22:8000/api/")
                .addConverterFactory(GsonConverterFactory.create());
        Retrofit retrofit = builder.build();
        ApiInterface apiInterface = retrofit.create(ApiInterface.class);
        Call<ResponseBody> call = apiInterface.registerQueue(temp,taskBody);

        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                if(response.isSuccessful()) {
                    Log.d(TAG, "SignUn Successful: ");
                    Toast.makeText(QueueActivity.this, "SignUp Successful", Toast.LENGTH_LONG).show();
                    onQueueSuccess();
                }else{
                    onQueueFailed(); // This is where the code ends and it doesn't add anything to API database
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

                Log.d(TAG,"went wrong",t);
                Toast.makeText(QueueActivity.this, "something went wrong", Toast.LENGTH_LONG).show();
                onQueueFailed();

            }
        });

错误:没有语法错误。问题是无法向后端添加任何内容。

P.S:关于发帖人我可以通过在标题中添加Authorization: Bearer <token here>并在task: <task here>中请求Form Data来实现此目的 例如task: mobile

更新日志

I/OpenGLRenderer: Initialized EGL, version 1.4
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/LoginActivity: Login
D/LoginActivity: send request fired
D/LoginActivity: Log in Successfull: 
D/LoginActivity: this is token in intent: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjcsImlzcyI6Imh0dHA6XC9cLzE5Mi4xNjguMS4yMjo4MDAwXC9hcGlcL2F1dGhcL2xvZ2luIiwiaWF0IjoxNDkxNTY3NDEwLCJleHAiOjE0OTE1NzEwMTAsIm5iZiI6MTQ5MTU2NzQxMCwianRpIjoiYzRhMTM5YzkxZDBkZDNmMGM3MDUwMjI3ODc3YTQ5YmUifQ.9SA6D3O0oWd3fcg-i3lR2n1nbJ8r0Xj2wC3ZZ6I1l2k
I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
V/RenderScript: 0xa9fbb000 Launching thread(s), CPUs 2
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa004f40
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa004b50
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa005090
D/QueueActivity: send Network Request: Task: mobiletest  Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjcsImlzcyI6Imh0dHA6XC9cLzE5Mi4xNjguMS4yMjo4MDAwXC9hcGlcL2F1dGhcL2xvZ2luIiwiaWF0IjoxNDkxNTY3NDEwLCJleHAiOjE0OTE1NzEwMTAsIm5iZiI6MTQ5MTU2NzQxMCwianRpIjoiYzRhMTM5YzkxZDBkZDNmMGM3MDUwMjI3ODc3YTQ5YmUifQ.9SA6D3O0oWd3fcg-i3lR2n1nbJ8r0Xj2wC3ZZ6I1l2k
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa004fb0

**更新okHttpInterceptor LOG **

04-07 16:00:11.614 24583-25144/com.example.comunicate D/OkHttp: --> POST http://192.168.1.22:8000/api/auth/queue/store http/1.1
04-07 16:00:11.614 24583-25144/com.example.comunicate D/OkHttp: Content-Type: multipart/form-data; boundary=958f2f27-e6d5-453b-836d-d2ef8073ef5a
04-07 16:00:11.615 24583-25144/com.example.comunicate D/OkHttp: Content-Length: 232
04-07 16:00:11.616 24583-25144/com.example.comunicate D/OkHttp: Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjcsImlzcyI6Imh0dHA6XC9cLzE5Mi4xNjguMS4yMjo4MDAwXC9hcGlcL2F1dGhcL2xvZ2luIiwiaWF0IjoxNDkxNTY5OTkwLCJleHAiOjE0OTE1NzM1OTAsIm5iZiI6MTQ5MTU2OTk5MCwianRpIjoiN2ZmYWZhM2ZmMTJiOTM3Y2RhNmVkNjAyYzc4Zjk3ZWUifQ.JuOxvj80nr9LmjCvJqwSERmJD-jOtg_W9UHs232lEzY
04-07 16:00:11.618 24583-25144/com.example.comunicate D/OkHttp: --958f2f27-e6d5-453b-836d-d2ef8073ef5a
04-07 16:00:11.618 24583-25144/com.example.comunicate D/OkHttp: Content-Disposition: form-data; name="task"
04-07 16:00:11.618 24583-25144/com.example.comunicate D/OkHttp: Content-Transfer-Encoding: binary
04-07 16:00:11.618 24583-25144/com.example.comunicate D/OkHttp: Content-Type: text/plain; charset=utf-8
04-07 16:00:11.618 24583-25144/com.example.comunicate D/OkHttp: Content-Length: 6
04-07 16:00:11.618 24583-25144/com.example.comunicate D/OkHttp: mobile
04-07 16:00:11.618 24583-25144/com.example.comunicate D/OkHttp: --958f2f27-e6d5-453b-836d-d2ef8073ef5a--
04-07 16:00:11.618 24583-25144/com.example.comunicate D/OkHttp: --> END POST (232-byte body)
04-07 16:00:11.804 24583-25144/com.example.comunicate D/OkHttp: <-- 404 Not Found http://192.168.1.22:8000/api/auth/queue/store (185ms)
04-07 16:00:11.804 24583-25144/com.example.comunicate D/OkHttp: Host: 192.168.1.22:8000
04-07 16:00:11.804 24583-25144/com.example.comunicate D/OkHttp: Connection: close
04-07 16:00:11.804 24583-25144/com.example.comunicate D/OkHttp: X-Powered-By: PHP/5.6.30
04-07 16:00:11.804 24583-25144/com.example.comunicate D/OkHttp: Cache-Control: no-cache, private
04-07 16:00:11.804 24583-25144/com.example.comunicate D/OkHttp: Content-Type: application/json
04-07 16:00:11.804 24583-25144/com.example.comunicate D/OkHttp: Date: Fri, 07 Apr 2017 13:00:12 GMT
04-07 16:00:11.813 24583-25144/com.example.comunicate D/OkHttp: {"error":{"message":"404 Not Found","status_code":404}}
04-07 16:00:11.813 24583-25144/com.example.comunicate D/OkHttp: <-- END HTTP (55-byte body)

1 个答案:

答案 0 :(得分:0)

您应拦截您的请求并在标头中添加令牌。如果您的令牌过期(它应该),Retrofit还支持重新验证。 您只需要以下教程:Retrofit — Token Authentication on Android