无法使用改造从api获得响应

时间:2019-05-09 09:00:16

标签: android retrofit

我的界面如下,

@GET("logCheck.php?username=mhaa96")
Call<Splash> getDaysLeft();

以下是我的API链接

https://www.aidedtrade.com/aidedTrade_app/logCheck.php?username=mhaa96

以下是我的api响应

 {
  "status": "success",
  "data": {
  "userName": "MHaa96",
  "days_left": "328"
 }
 }

以下是我的模型课

 public class Splash {
  @SerializedName("userName")
  @Expose
  private String userName;
  @SerializedName("days_left")
  @Expose
  private String daysLeft;

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getDaysLeft() {
    return daysLeft;
}

public void setDaysLeft(String daysLeft) {
    this.daysLeft = daysLeft;
}

以下是我正在调用api但Daysleft中没有响应的活动

public void apiCalling() {
    APIinterface apIinterface = 
  APIClient.getClient().create(APIinterface.class);
    retrofit2.Call<Splash> call = apIinterface.getDaysLeft();
    Log.e("callMethod", "apiCalling: " + call);
    call.enqueue(new Callback<Splash>() {
        @Override
        public void onResponse(retrofit2.Call<Splash> call, 
Response<Splash> response) {
            String alerts = response.body().getDaysLeft();
            Log.e("DaysLeft", "onResponse: " + alerts);
            Log.e("responce", "onResponse: " + 
response.body().getUserName());
        }

        @Override
        public void onFailure(retrofit2.Call<Splash> call, Throwable t) {
            Toast.makeText(getApplicationContext(), "Please Check your 
 internet connection", Toast.LENGTH_SHORT).show();
        }
    });
 }

以下是我设置基本URL的API客户端

public static final String BASE_URL = 

https://www.aidedtrade.com/aidedTrade_app/”;     私人静态改造Retrofit = null;

public static Retrofit getClient() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new 
  OkHttpClient.Builder().addInterceptor(interceptor).build();

    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
    }
    return retrofit;
}

以下是我的第二堂课

public class DaysLeft {

@SerializedName("status")
@Expose
private String status;
@SerializedName("data")
@Expose
private Data data;

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public Data getData() {
    return data;
}

public void setData(Data data) {
    this.data = data;
}

2 个答案:

答案 0 :(得分:0)

您的模型是错误的 您必须使用两个类,一个用于数据模型,一个用于响应模型,如下所示:

public class DataResponse{
     private String userName;
     pricate String days_left;

    public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getDaysLeft() {
return daysLeft;
}

public void setDaysLeft(String daysLeft) {
this.daysLeft = daysLeft;
}
}

和其他类似belew的类:

public calss Splash
{
   private String status;
   private DataResponse data;
   // use geter & setter for this
}

您的界面

@GET("logCheck.php?username=mhaa96")
Call<Splash> getDaysLeft();

希望有用

答案 1 :(得分:0)

请检查以下代码示例。我已经更新了您的回调。一切正常。

界面如下,

@GET("logCheck.php?username=mhaa96")
Call<DaysLeft> getDaysLeft();

模型类如下: DaysLeft.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class DaysLeft {
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("data")
    @Expose
    private Splash data;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Splash getData() {
        return data;
    }

    public void setData(Splash data) {
        this.data = data;
    }
}

Splash.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Splash {
    @SerializedName("userName")
    @Expose
    private String userName;
    @SerializedName("days_left")
    @Expose
    private String daysLeft;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getDaysLeft() {
        return daysLeft;
    }

    public void setDaysLeft(String daysLeft) {
        this.daysLeft = daysLeft;
    }

}

在主要活动课中

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    public static final String BASE_URL = "https://www.aidedtrade.com/aidedTrade_app/";
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new
                OkHttpClient.Builder().addInterceptor(interceptor).build();

        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client)
                    .build();
        }
        return retrofit;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        apiCalling();
    }

    public void apiCalling() {
        APIInterface apIinterface =
                getClient().create(APIInterface.class);
        retrofit2.Call<DaysLeft> call = apIinterface.getDaysLeft();
        Log.e("callMethod", "apiCalling: " + call);
        call.enqueue(new Callback<DaysLeft>() {
            @Override
            public void onResponse(retrofit2.Call<DaysLeft> call,
                                   Response<DaysLeft> response) {
                Splash alerts = response.body().getData();
                Log.e("DaysLeft", "onResponse: " + alerts);
                Log.e("responce", "onResponse: " +
                        response.body().getData().getUserName());
            }

            @Override
            public void onFailure(retrofit2.Call<DaysLeft> call, Throwable t) {
                Toast.makeText(getApplicationContext(), "Please Check your internet connection", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

在明显的互联网许可中。

<uses-permission android:name="android.permission.INTERNET"/>