【问题标题】:Retrofit don't send the body or header (Content-type: application/json) is missed改造不发送正文或标题(内容类型:应用程序/json)被遗漏
【发布时间】:2017-07-28 05:26:56
【问题描述】:

retrofit,我的 Rotrofit 有问题,我用@Body 发送所有字符串,这个字符串是一个使用 Gson 将其转换为字符串的对象。使用 Postman 复制粘贴 Gson 的结果并打开,但使用 Restrofit 时服务器返回,正文中没有任何内容。

接口改造

@POST("validation")
@Headers("Content-type: application/json")
Call<Validator> makeValidation(@Header("WWW-Authenticate") String token, @Body String body);

Gson 和改造的要求

            Validator validator = getIntent().getParcelableExtra(Finish_Game.validator);

        Gson gson = new Gson();
        String validatorstring = gson.toJson(validator);


        String token = Bearer.getDefaults(Bearer.BEARER_KEY, this);
        RetrofitControler retrofitControler = new RetrofitControler();

        try {
            Call<Validator> listCall = retrofitControler.postValidationPoints(token, validatorstring);
            listCall.enqueue(new CallBackProgress<Validator>(this, this));
        } catch (Exception e) {
            //TODO for depure
            e.printStackTrace();
        }

简单的回调

package es.iesnervion.qa.Model;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;

import es.iesnervion.qa.ui.View.CategoriesActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by apol on 1/02/17.
 */

public class CallBackProgress<T> implements Callback<T> {
    private Context c;
    private T lisato;
    Responser res;


    public CallBackProgress(Responser r, Context c) {
        this.c = c;
        res = r;
    }

    /**
     * Invoked for a received HTTP response.
     * <p>
     * Note: An HTTP response may still indicate an application-level failure such as a 404 or 500.
     * Call {@link Response#isSuccessful()} to determine if the response indicates success.
     *
     * @param call
     * @param response
     */
    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        if (response.body() == null) {
            onFailure(call, new Throwable(response.message()));
        } else {
            lisato = response.body();
            String bearer = response.headers().get("WWW-Authenticate");
            res.onFinish(lisato, bearer);
        }
    }

    /**
     * Invoked when a network exception occurred talking to the server or when an unexpected
     * exception occurred creating the request or processing the response.
     *
     * @param call
     * @param t
     */
    @Override
    public void onFailure(Call<T> call, Throwable t) {
        //TODO solo para el depurado
        Toast.makeText(c, t.getMessage(), Toast.LENGTH_LONG).show();
        t.printStackTrace();
        res.onFailure(t);
    }
}

如果您需要更多信息,请告诉我:(

【问题讨论】:

    标签: android json gson retrofit


    【解决方案1】:

    好的,我刚刚发现问题:

    这个:

    Call<Validator> makeValidation(@Header("WWW-Authenticate") String token, @Body String body);
    

    改为:

    Call<Validator> makeValidation(@Header("WWW-Authenticate") String token, @Body Validator body);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 2018-09-05
      • 1970-01-01
      • 2013-08-16
      • 2017-11-17
      • 2016-12-24
      • 2022-10-15
      相关资源
      最近更新 更多