簡體   English   中英

使用RetroFit2和Android / Java的首次API調用

[英]First API call using RetroFit2 and Android/Java

我已經編寫了一個iOS應用程序和一個PHP后端。 現在,我想開始使用Android前端,但是我不知道自己在做什么。

我的項目結構如下:

在此處輸入圖片說明

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

我的ApiInterface:

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中似乎工作正常:

在此處輸入圖片說明

在onFailure()中添加Log.d語句后添加了此圖像

在此處輸入圖片說明

定義的基本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。

例如,使用真實的服務器,我使用托管服務,它可以免費提供上傳文件和數據庫的服務,然后檢查是否可以運行。 您的代碼沒有錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM