在发布休息api时,改造400不良请求

问题描述 投票:0回答:1

我正试图使用Retrofit rest客户端消费用asp.net core 2.2编写的web api,成功地从Api's获取数据,但无法使用Retrofit将数据发布到服务器上,webapi通过postman和Javascript工作正常。

以下是我已经尝试过的...

我的活动类

    private Retrofit retrofit = new Retrofit.Builder().baseUrl("https://sga.somee.com/api/")
        .addConverterFactory(ScalarsConverterFactory.create()).addConverterFactory(GsonConverterFactory.create()).build();
    private ShopApi _shopsApi;
    //constructor
    public MainActivity2(){
        //api classes must be initialized in constructors
         _shopsApi = retrofit.create(ShopApi.class);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
   Shop _shop = new Shop();
        _shop.shop_name = "Starbucks";
        _shop.shop_owner = "Unknown";
        _shop.shop_longitude = 49545.3455;
        _shop.shop_latitude = 55353.554;
        _shop.shop_image = "path/path.png";
        PostShop(_shop);
    }
    public void PostShop(Shop shop){
        /*
        JsonObject obj = new JsonObject();
        obj.addProperty("shop_name",shop.shop_name);
        obj.addProperty("shop_owner",shop.shop_owner);
        obj.addProperty("shop_longitude",shop.shop_longitude);
        obj.addProperty("shop_latitude",shop.shop_latitude);
        obj.addProperty("shop_image",shop.shop_image);
        Log.d(TAG, obj.toString());
         */
        Call<ResponseBody> AddShop = _shopsApi.AddShop(shop);
        AddShop.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Toast.makeText(MainActivity2.this,response.raw().body().toString(),Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(MainActivity2.this,"Failded to add new shop",Toast.LENGTH_LONG).show();
            }
        });
    }

我的ShopApi类

package com.example.madee.sga.API;

import com.example.madee.sga.Models.Shop;

import java.util.List;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.POST;
import retrofit2.http.Path;

public interface ShopApi {
    @GET("Shops/GetAll/{page}")
    Call<List<Shop>> GetAllShops(@Path("page") int page );


    @Headers({"Content-Type: application/json"})
    @POST("Shops/AddNew")
    Call<ResponseBody> AddShop(@Body Shop _shop);

}

你也可以看到,我也试过在MainActivity中用JsonObject发布数据,但同样失败。400个坏蛋

android retrofit retrofit2 webapi
1个回答
0
投票

这个错误的出现是因为你在主体中设置了params。POST 请求。添加这样的Paramtere。

@Headers({"Content-Type: application/json"})
@FormUrlEncoded
@POST("Shops/AddNew")
Call<ResponseBody> AddShop(@Field("ShopName") String ShopName,  @Field("OwnerName") String OwnerName, @Field("Longitude") int Longitude,@Field("Latitude") int Latitude,@Field("Imagepath") String Imagepath,@Field("ShopId") String ShopId);

然后调用这个函数。

Call<ResponseBody> AddShop = _shopsApi.AddShop(shop.shop_name, shop.shop_owner, shop.shop_longitude, shop.shop_latitude, shop.shop_image,shop.shop_id);

我假设你的服务器接受和你代码中写的一样的参数名称,如果不一样的话,就相应的修改一下。

© www.soinside.com 2019 - 2024. All rights reserved.