我正在尝试改造,但我一直得到404的错误

时间:2017-06-10 12:34:37

标签: android retrofit2

用于获取改造实例..这是正确的方法吗?

public class APIClient {
public static final String 
BASE_URL="http://services.hanselandpetal.com/";
public static Retrofit retrofit=null;

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

使用接口进行CALL。

public interface APIInterface {
          @POST("feeds/flowers.json")
          Call<List<Flowers>> getFlowers();
             }

这里我得到了404的错误..我已经检查了它的工作原理就好了。我甚至尝试使用localhost我遇到同样的问题。

public class MainActivity extends AppCompatActivity {
APIInterface apiInterface;
List<Flowers> flowers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
apiInterface= APIClient.getApiClient().create(APIInterface.class);//Getting Retrofit Instance
Call<List<Flowers>> call= apiInterface.getFlowers();
call.enqueue(new Callback<List<Flowers>>() {
        @Override
        public void onResponse(Call<List<Flowers>> call, 
          Response<List<Flowers>> response) {
            flowers=response.body();
        }
        @Override
        public void onFailure(Call<List<Flowers>> call, Throwable t) {

        }
          }  );
      }
}

日志

06-10 19:25:03.076 2475-2891/com.google.android.gms I/FA-SVC: App measurement is starting up, version: 10298
06-10 19:25:03.166 2475-3089/com.google.android.gms I/FA-SVC: This instance being marked as an uploader
06-10 19:25:07.854 2475-2484/com.google.android.gms W/SQLiteConnectionPool: 
A SQLiteConnection object for database'/data/user/0/com.google.android.gms/databases/networkstatistics.sqlite 'was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
06-10 19:25:11.934 2475-2484/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/metrics.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
06-10 19:25:11.946 2475-2484/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/help_responses.db.18' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
06-10 19:25:12.234 2475-2484/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/auto_complete_suggestions.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

Flower.class

  public class Flowers {
@SerializedName("category")
private String Category;
@SerializedName("instructions")
private String Instructions;
@SerializedName("price")
private String Price;

public String getCategory() {
    return Category;
}

public String getInstructions() {
    return Instructions;
}

public String getPrice() {
    return Price;
}

public String getName() {
    return Name;
}

public String getProductId() {
    return ProductId;
}

public String getPhoto() {
    return Photo;
}

@SerializedName("name")
private String Name;
@SerializedName("productId")
private String ProductId;
@SerializedName("photo")
private String Photo;
}

1 个答案:

答案 0 :(得分:0)

更改您的BASE_URL和POST-Param

public static final String BASE_URL="http://services.hanselandpetal.com/";

public static final String BASE_URL="http://services.hanselandpetal.com";

@POST("feeds/flowers.json")

@POST("/feeds/flowers.json")

编辑:

dependencies {
    compile'com.squareup.retrofit2:retrofit:2.3.0'
    compile'com.squareup.retrofit2:converter-gson:2.3.0'
}

MainActivity.java

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

    private APIInterface apiInterface;
    private List<Flowers> flowers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        apiInterface = APIClient.getApiClient().create(APIInterface.class);//Getting Retrofit Instance
        Call<List<Flowers>> call = apiInterface.getFlowers();
        call.enqueue(new Callback<List<Flowers>>() {
            @Override
            public void onResponse(Call<List<Flowers>> call,
                                   Response<List<Flowers>> response) {
                flowers = response.body();
                if (flowers != null) {
                    for (Flowers flower : flowers) {
                        Log.e("flowers Category", flower.getCategory());
                    }
                }
            }

            @Override
            public void onFailure(Call<List<Flowers>> call, Throwable t) {

            }
        });
    }
}

Flowers.java

import com.google.gson.annotations.SerializedName;

public class Flowers {

    @SerializedName("category")
    private String category;

    public String getCategory() {
        return category;
    }
}

APIInterface.java

import java.util.List;

import retrofit2.Call;
import retrofit2.http.POST;


public interface APIInterface {
    @POST("feeds/flowers.json")
    Call<List<Flowers>> getFlowers();
}

APIClient.java

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


public class APIClient {
    public static final String
            BASE_URL="http://services.hanselandpetal.com/";
    public static Retrofit retrofit=null;

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