我想将帖子请求发送到我的网址RestFul
api:
我的网址:https://api.vavkhan.com/users/
我的朋友:
// 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'
我的班级:
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("TTTTTTTTTest@gmail.com", "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);
}
}
在我的控制台或我的屏幕上没有显示任何内容!
答案 0 :(得分:2)
将@POST(“/ posts”)更改为@POST(“posts”)并尝试。
答案 1 :(得分:0)
由于'/'已包含在BASE_URL的末尾,因此您无需将其包含在@POST("/posts")
中。只需将其更改为@POST("posts")
。