第一个使用android/java的api调用

jhdbpxl9  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(324)

我已经编写了一个ios应用程序和一个php后端。现在我想开始安卓前端,但我不知道我在做什么。
我的项目结构如下所示:

我的客户:

package com.dimsumdev.runk.rest;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {

    public static final String BASE_URL = "http://localhost/index.php/api/";
    private static Retrofit retrofit = null;

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

我的API接口:

package com.dimsumdev.runk.rest;

import com.dimsumdev.runk.model.*;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;

public interface ApiInterface {
    @GET("challenge/challenge/id/{id}")
    Call<Challenge> getChallenge(@Path("id") int id);
}

我的挑战模式:

package com.dimsumdev.runk.model;

import com.google.gson.annotations.SerializedName;

public class Challenge {
    @SerializedName("challenge_id")
    Integer challengeId;
    @SerializedName("challenge_name")
    String challengeName;
    @SerializedName("challenge_description")
    String challengeDescription;
    @SerializedName("created")
    String created;
    @SerializedName("challenge_status")
    String challengeStatus;
}

我的家庭活动:

package com.dimsumdev.runk.activity;

import android.support.v7.app.AppCompatActivity;
import com.dimsumdev.runk.R;
import com.dimsumdev.runk.model.*;
import android.os.Bundle;
import com.dimsumdev.runk.rest.ApiClient;
import com.dimsumdev.runk.rest.ApiInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeActivity extends AppCompatActivity {

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

        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

        Call<Challenge> call = apiService.getChallenge(2);

        call.enqueue(new Callback<Challenge>() {

            @Override
            public void onResponse(Call<Challenge> call, Response<Challenge> response) {
                System.out.print("success");
            }

            @Override
            public void onFailure(Call<Challenge> call, Throwable t) {
                System.out.print("error");
            }
        });
    }
}

问题是,在homeactivity类中,它一直运行到错误打印中。这不是我所期望的程序。
后端在postman中似乎运行良好:

在onfailure()中添加log.d语句后添加了此映像

yks3o0rb

yks3o0rb1#

使用真正的服务器,例如我使用hostinger提供免费服务上传你的文件和数据库,然后检查,它会工作。你的代码没有错误

sz81bmfz

sz81bmfz2#

定义的基本url指向localhost,这意味着它希望您的手机运行的是php后端。
代替

public static final String BASE_URL = "http://localhost/index.php/api/";

具有

public static final String BASE_URL = "http://<<ENTER-YOUR-PHP-BACKEND-MACHINE_IP>>/index.php/api/";

即使使用仿真器(它是自己的虚拟设备),也不能使用localhost。你必须输入你机器的ip地址。

相关问题