2017-06-10 55 views
0

用于获取改装实例..这是正确的方法吗?我正在尝试改造,但我不断收到404的错误

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

用于进行调用的接口。

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

APIClient.getApiClient()。create(APIInterface.class); – Nikunj

+0

试过它不起作用 –

回答

0

改变你BASE_URL和POST-帕拉姆

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

似乎没有解决它 –

+0

从AS发布您的LogCat。 –

+0

似乎Logcat中没有错误 –

相关问题