如何在Retrofit2中发布消息?

时间:2018-06-07 14:21:07

标签: java android json retrofit2

此项目以web server运行。当用户点击按钮时,它应该在EditText内发布消息。我使用Retrofit2。单击按钮时程序已停止。

ApiInterface.java

@POST("api/EmergencyNotification/SendNotification")
Call<SendMessageModel>postMessage(@Header("Authorization") String token,
                              //  @Field(("PhotoRequest")) String photoRequest,
                              //  @Field(("Location")) String location,
                                @Field(("MessageBody")) String messageBody);
                                //  @Field(("AnswerValue")) String answerValue);   

在按钮OnClick中,此功能运行:

protected void postMessage(){
    startProgress();
    String authorization = SessionHelper.getCustomerTokenWithBearer();
   // Loc = lattitude + longitude;
    Call<SendMessageModel> call = ApiService.apiInterface.postMessage(authorization,
            mesaj.getText().toString().trim());

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

            if (response.isSuccessful()){

                if (response.body() != null){
                    DialogHelper.showDialogWithOneButton("",response.body().getData());
               }
            }
            else {
                ApiErrorUtils.parseError(response);
            }
        }

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

            stopProgress();
            DialogHelper.showFailedDialog();
        }
    });
}

2 个答案:

答案 0 :(得分:1)

确定。我现在解决了。我的api网址错误,我添加了新的@Multipart@Part,而不是@Field

@POST("api/EmergencyNotification/SendMessage")
  Call<SendMessageModel>postMessage(@Header("Authorization") String token,

                                    @Part(("MessageBody")) String messageBody);

答案 1 :(得分:0)

您缺少@POST("api/EmergencyNotification/SendNotification") @FormUrlEncoded Call<SendMessageModel>postMessage(@Header("Authorization") String token, ... 属性,因为您使用的是字段属性而不是正文

O(m * n * p)
相关问题