2017-02-11 27 views
0

我想發送POST請求我的網址RestFul API:改造不能發送或接收任何

我的網址:https://api.vavkhan.com/users/

我gradle這個:

// Retrofit 
    compile 'com.squareup.retrofit2:retrofit:2.1.0' 

// JSON Parsing 
    compile 'com.google.code.gson:gson:2.6.1' 
    compile 'com.squareup.retrofit2:converter-gson:2.1.0' 

    compile 'io.reactivex:rxjava:1.1.6' 
    compile 'io.reactivex:rxandroid:1.2.1' 
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' 
    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' 

我班上的:

enter image description here

public interface APIService 
{ 
    @POST("/posts") 
    @FormUrlEncoded 
    Call<MainActivity> savePost(@Field("email") String email, 
            @Field("firstname") String firstname, 
            @Field("password") String password, 
            @Field("lastname") String lastname); 

} 

public class RetrofitClient 
{ 

    private static Retrofit retrofit = null; 

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

} 

public class ApiUtils 
{ 

    private ApiUtils() {} 

    public static final String BASE_URL = "https://api.vavkhan.com/users/"; 

    public static APIService getAPIService() { 

     return RetrofitClient.getClient(BASE_URL).create(APIService.class); 
    } 

} 

public class MainActivity extends AppCompatActivity { 

    private APIService mAPIService; 
    //REST API 
    @SerializedName("id") 
    @Expose 
    private Integer id; 
    @SerializedName("firstname") 
    @Expose 
    private String firstname; 
    @SerializedName("lastname") 
    @Expose 
    private String lastname; 
    @SerializedName("email") 
    @Expose 
    private String email; 
    @SerializedName("cell_phone") 
    @Expose 
    private Object cellPhone; 
    @SerializedName("profile_picture") 
    @Expose 
    private Object profilePicture; 
    @SerializedName("credit") 
    @Expose 
    private Integer credit; 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getFirstname() { 
     return firstname; 
    } 

    public void setFirstname(String firstname) { 
     this.firstname = firstname; 
    } 

    public String getLastname() { 
     return lastname; 
    } 

    public void setLastname(String lastname) { 
     this.lastname = lastname; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public Object getCellPhone() { 
     return cellPhone; 
    } 

    public void setCellPhone(Object cellPhone) { 
     this.cellPhone = cellPhone; 
    } 

    public Object getProfilePicture() { 
     return profilePicture; 
    } 

    public void setProfilePicture(Object profilePicture) { 
     this.profilePicture = profilePicture; 
    } 

    public Integer getCredit() { 
     return credit; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setCredit(Integer credit) { 
     this.credit = credit; 
    } 

    @Override 
    public String toString() { 
     Toast.makeText(MainActivity.this,"1 + + + + + + + + + + ",Toast.LENGTH_LONG).show(); 

     return "MainActivity{" + 
       "id=" + id + 
       ", firstname='" + firstname + '\'' + 
       ", lastname='" + lastname + '\'' + 
       ", email='" + email + '\'' + 
       ", cellPhone=" + cellPhone + 
       ", profilePicture=" + profilePicture + 
       ", credit=" + credit + 
       '}'; 

    } 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mAPIService = ApiUtils.getAPIService(); 

     sendPost("[email protected]", "test","test","test"); 
    } 



    public void sendPost(String email, String firstname,String lastname,String password) { 
     mAPIService.savePost(email, firstname,lastname, password).enqueue(new Callback<MainActivity>() { 
      @Override 
      public void onResponse(Call<MainActivity> call, Response<MainActivity> response) { 

       Toast.makeText(MainActivity.this,"1 + + + + + + + + + + ",Toast.LENGTH_LONG).show(); 

       if(response.isSuccessful()) { 
        showResponse(response.body().toString()); 
        Log.e("TEst", "post submitted to API." + response.body().toString()); 

        Toast.makeText(MainActivity.this,"1"+response.body().toString(),Toast.LENGTH_LONG).show(); 
       } 
      } 

      @Override 
      public void onFailure(Call<MainActivity> call, Throwable t) { 
       Log.e("TAG", "Unable to submit post to API."); 
       Toast.makeText(MainActivity.this,"2 error",Toast.LENGTH_LONG).show(); 
      } 
     }); 
    } 

    public void showResponse(String response) { 
     Toast.makeText(this,"3"+response,Toast.LENGTH_LONG).show(); 
     Log.e("taaag",response); 
    } 



} 

在我的控制檯或我的屏幕不顯示任何東西!

回答

2

將@POST(「/ posts」)更改爲@POST(「posts」)並嘗試。

+0

我chnaged和不工作。 –

0

由於'/'已包含在BASE_URL的末尾,因此不需要將其包含在@POST("/posts")中。只需將其更改爲@POST("posts")即可。