gpt4 book ai didi

java - 使用 RetroFit2 和 Android/Java 的首次 API 调用

转载 作者:行者123 更新时间:2023-11-29 04:36:04 27 4
gpt4 key购买 nike

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

我的项目结构是这样的:

enter image description here

我的 ApiClient:

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 接口(interface):

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;
}

还有我的 HomeActivity:

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 中运行良好:

enter image description here

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

enter image description here

最佳答案

定义的基本 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/";

即使你使用模拟器(这是一个自己的虚拟设备),你也不能使用本地主机。您必须输入您机器的 IP。

关于java - 使用 RetroFit2 和 Android/Java 的首次 API 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41459385/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com