第一个使用改型2和Android/Java的API调用

第一个使用改型2和Android/Java的API调用,java,android,retrofit2,Java,Android,Retrofit2,我已经编写了一个iOS应用程序和一个PHP后端。现在我想开始Android前端,但我不知道我在做什么 我的项目结构如下所示: 我的客户: package com.dimsumdev.runk.rest; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; public class ApiClient { public static final String BASE_UR

我已经编写了一个iOS应用程序和一个PHP后端。现在我想开始Android前端,但我不知道我在做什么

我的项目结构如下所示:

我的客户:

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.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");
            }
        });
    }
}
package com.dimsumdev.runk.activity;
导入android.support.v7.app.AppActivity;
导入com.dimsumdev.runk.R;
导入com.dimsumdev.runk.model.*;
导入android.os.Bundle;
导入com.dimsumdev.runk.rest.ApiClient;
导入com.dimsumdev.runk.rest.apinterface;
2.电话;;
2.回拨;
2.回应;;
公共类HomeActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ApiInterface apiService=ApiClient.getClient().create(ApiInterface.class);
Call Call=apiService.getChallenge(2);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
系统输出打印(“成功”);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
系统输出打印(“错误”);
}
});
}
}
问题是,在HomeActivity类中,它一直运行到错误打印。这不是我所期望的程序要做的

后端在Postman中似乎运行良好:

在onFailure()中添加Log.d语句后添加此映像


定义的基本url指向localhost,这意味着它希望您的手机正在运行PHP后端

替换

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

public静态最终字符串BASE\u URL=”http:///index.php/api/";

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

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

你能把你的logcat输出放在这里吗?你得到了什么错误..你在清单中添加了internet权限了吗?程序跳入onFailure函数,导致打印“error”。我在清单中添加了互联网许可;你的PHP代码真的在你的手机上运行吗?您应该将URL中的“localhost”替换为托管php后端的计算机的IP。不过,请更改IP。模拟器是一个具有自己ip的虚拟设备。正确!真棒,谢谢。我想这是Android和iOS开发者之间的一个区别。我记得我错了,但不是
localhost
在所有设备中都是通用的吗?同样的行为也会发生在iOSIt中,它是通用的,并且始终是运行应用程序的机器的地址。我认为Android仿真器和iOS仿真器之间有区别,Android仿真器总是一个有自己的虚拟网络适配器的虚拟设备,而iOS仿真器没有自己的“虚拟”网络适配器?如果我错了,请纠正我。
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/";