【问题标题】:Retrofit and api, problems with imports改造和api,进口问题
【发布时间】:2020-02-27 15:50:12
【问题描述】:

我在 Android Studio 中使用 RetrofitAPI,但不确定我哪里出错了。那是我的 API java 代码。我在值、编码的表单 url 和帖子上遇到错误

<package com.example.coffeeshopapp.Retrofit;
import com.example.coffeeshopapp.Model.User;
import com.example.coffeeshopapp.Model.checkUserResponse;
 import retrofit2.http.FormUrlEncoded

public interface CoffeeShopAPI {
@FormUrlEncoded
@POST("checkuser.php")
Call<checkUserResponse> checkUserExists(@Field("phone")String phone);

@FormUrlEncoded
@POST("register.php")
Call<User> registerNewUser(@Field("phone")String phone,
                           @Field("name")String name,
                           @Field("address")String address,
                           @Field("birthdate")String birthdate);
}

这是我的改造代码。改造和 gson 转换器工厂的错误。

package com.example.coffeeshopapp.Retrofit;

import Retrofit;

public class RetrofitClient {

private static Retrofit retrofit = null;

public static Retrofit getClient(String baseUrl)
{
    if (retrofit == null)
    {
      retrofit = new Retrofit.Builder()
              .baseUrl(baseUrl)
              .addConverterFactory(GsonConverterFactory.create())
              .build();
    }
    return retrofit;
}
}

【问题讨论】:

  • 请分享你的错误日志。
  • 改造错误:无法解析符号改造和gson转换器工厂
  • api 错误:无法解析符号:post、formurlencoded、call 和 field
  • 好像你没有导入必要的包确保你已经导入它们你可以按 Alt+Enter 快捷键并导入必要的包
  • 用于改造、表单 url 编码、发布和调用的包不显示我是否打算将它们添加到 gradle 文件中的依赖项中?

标签: android rest gson retrofit


【解决方案1】:

尝试关注它可能对你有帮助:

首先将依赖项添加到您的 build.gradle 文件中:-

implementation 'com.squareup.retrofit2:retrofit:2.6.2'
implementation 'com.squareup.retrofit2:converter-gson:2.6.2'

接口类:-

@FormUrlEncoded
@POST("update-status")
Call<JsonObject> getUpdateStatus(@Field("token") String token, @Field("status") String status);

API 调用:-

private void statusUpdateApi() {

    Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.BASE_URL_LOGIN).addConverterFactory(GsonConverterFactory.create()).build();
    Api api = retrofit.create(Api.class);
    Call<JsonObject> jsonObjectCall = api.getUpdateStatus(UserSession.getKeepLogin(context).getuToken(), status);
    Log.e("macro", "asd" + status);
    jsonObjectCall.enqueue(new Callback<JsonObject>() {
        @Override
        public void onResponse(@NonNull Call<JsonObject> call, @NonNull Response<JsonObject> response) {
            Log.e("123", "456" + response);
            Log.e("123", "456-->" + response.body());

        }

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

        }
    });

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-17
    • 2021-02-13
    • 2021-09-27
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多