2015-10-15 53 views
0

我想第一次使用Android Retrofit,我正在學習下面的教程並陷入了第一步... 1.使用Gson2.3,okhttp2.1.0,okhttp -urlconnection2.1.0,retrofit-1.8.0 2.我的web服務在.php腳本中並返回這種數據{「idInfo」:「12」,「typeInfo」:「2」} 3.我創建了模型類gitmodelAndroid Retrofit簡單使用

public class gitmodel { 
    public gitmodel(int idInfo, int typeInfo) { 
     this.idInfo = idInfo; 
     this.typeInfo = typeInfo; 
    } 

    private int idInfo; 
    private int typeInfo; 
    public gitmodel() { 
    } 
    public int getIdInfo() { 
     return idInfo; 
    } 
    public void setIdInfo(int idInfo) { 
     this.idInfo = idInfo; 
    } 
    public int getTypeInfo() { 
     return typeInfo; 
    } 
    public void setTypeInfo(int typeInfo) { 
     this.typeInfo = typeInfo; 
    } 
} 

「 4. 我創建的接口類gitapi,看起來像這樣

public interface gitapi { 

    @GET("https://stackoverflow.com/users/{user}")  
    public void getFeed(@Path("user") String user,Callback<gitmodel> response);  
} 

我在這個類中出現錯誤「無法解析路徑」,無法解析gitmodel。我知道這只是一個開始,但我不能繼續。

回答

0

使用1.9版本改造的,我只是複製粘貼我的基本API類:

compile 'com.squareup.retrofit:retrofit:1.9.0' 

public class Api { 

    public interface Service { 

     @GET("/api/url/something") 
     ResponseModelThatYouCreated getSomethingFromGithub(@Body RequestModel requestModelThatYouCreated); 

     //other methods can be here 
    } 

    private static final String API_URL = "http://api.github.com"; //or something else 

    private static final RestAdapter REST_ADAPTER = new RestAdapter.Builder() 
      .setEndpoint(API_URL) 
      .setLogLevel(RestAdapter.LogLevel.BASIC) 
      .build(); 

    private static final Service ServiceInstance = REST_ADAPTER.create(Service.class); 

    public static Service getService() { 
     return ServiceInstance; 
    } 

} 

用法:

ResponseModelThatYouCreated fromGit = Api.getInstance().getSomethingFromGithub(/*parameter object*/) 
+0

能否請你列出所有的類,文件使用的是? – Jure

+0

沒有什麼可以列出的。你只需要你的請求和響應對象,如gitModel。 – Ozgur

+0

我有2個文件,POJO與getter和setter(gitmodel)以及API包中的文件引發了我的錯誤。你可以粘貼你的工作示例中的所有文件,我會嘗試重用它嗎? – Jure