我正在开展一个项目。 Web团队已实现用于注册和登录的API。我将实现Android部分。因此,当我向服务器发布请求以使用Postman进行注册时,它正在运行。但是当我尝试在Android中注册时,它会返回500内部服务器错误。我使用Retrofit发送请求。这是我的代码:
SignUpActivity.java:
public class SingUpActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
final EditText email = (EditText) findViewById(R.id.input_email);
final EditText username = (EditText) findViewById(R.id.input_name);
final EditText password = (EditText) findViewById(R.id.input_password);
final EditText gender = (EditText) findViewById(R.id.gender);
final EditText location = (EditText) findViewById(R.id.location);
Button btn_signUp = (Button) findViewById(R.id.btn_signup);
btn_signUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email_str = email.getText().toString().trim();
String username_str = username.getText().toString().trim();
String password_str = password.getText().toString().trim();
if(!TextUtils.isEmpty(email_str) && !TextUtils.isEmpty(username_str) && !TextUtils.isEmpty(password_str))
{
// Toast.makeText(getApplicationContext(), "username: " + username_str + " email: "+ email_str + " password: " + password_str, Toast.LENGTH_SHORT).show();
sendPost(username_str, email_str, password_str);
}
}
});
}
public void sendPost(String email, String username, String password){
Retrofit retrofit = ApiClient.getApiClient();
ApiInterface apiInterface = retrofit.create(ApiInterface.class);
Call<JsonResponseSignUp> call = apiInterface.signUp(new SignUpBody(username, email, password));
call.enqueue(new Callback<JsonResponseSignUp>() {
@Override
public void onResponse(Call<JsonResponseSignUp> call, Response<JsonResponseSignUp> response) {
if (response.code() == 200) {
Toast.makeText(getApplicationContext(), "SUCCESS", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Sorry for inconvince server is down" + response.code(), Toast.LENGTH_SHORT).show();
Log.d("response", response.raw().body().toString());
}
}
@Override
public void onFailure(Call<JsonResponseSignUp> call, Throwable t) {
Toast.makeText(getApplicationContext(), "ERROR while posting", Toast.LENGTH_SHORT).show();
}
});
}}
ApiClient.java
public class ApiClient {
public static final String BASE_URL = "http://my-aws-url.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;
}}
ApiInterface.java
public interface ApiInterface {
@Headers( {"Content-Type: application/json" })
@POST("/api/users/signup")
Call<JsonResponseSignUp> signUp(@Body SignUpBody signup);
Call<User> signIn();}
JsonResponseSignUp.java
public class JsonResponseSignUp {
@SerializedName("user")
@Expose
private User user;
@SerializedName("profile")
@Expose
private Profile profile;
public User getUser() {
return user;
}
public Profile getProfile() {
return profile;
}
public JsonResponseSignUp(Profile profile,User user ) {
this.user = user;
this.profile = profile;
}
public void setUser(User user) {
this.user = user;
}
public void setProfile(Profile profile) {
this.profile = profile;
}}
Profile.java
public class Profile {
@SerializedName("id")
private int id;
@SerializedName("username")
private String username;
@SerializedName("location")
private String location;
@SerializedName("gender")
private String gender;
@SerializedName("photo_path")
private String photo_path;
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getLocation() {
return location;
}
public String getGender() {
return gender;
}
public String getPhoto_path() {
return photo_path;
}
public Profile(int id, String username, String location, String gender, String photo_path) {
this.id = id;
this.username = username;
this.location = location;
this.gender = gender;
this.photo_path = photo_path;
}
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setLocation(String location) {
this.location = location;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setPhoto_path(String photo_path) {
this.photo_path = photo_path;
}}
SignUpBody.java
public class SignUpBody {
public String username;
public String email;
public String password;
public SignUpBody(String username, String email, String password) {
this.username = username;
this.email = email;
this.password = password;
}}
User.java
public class User {
@SerializedName("username")
private String username;
@SerializedName("email")
private String email;
public String getEmail() {
return email;
}
public String getUsername() {
return username;
}
public User(String username, String email) {
this.username = username;
this.email = email;
}
public void setUsername(String username) {
this.username = username;
}
public void setEmail(String email) {
this.email = email;
}}
这是服务器日志(API使用Django-Rest-Framework完成):
raise AssertionError(msg) AssertionError: You must call `.is_valid()` before accessing `.errors`.
邮差Json请求正常工作 POST:
{"username" : "userfdsar","email":"userrr@gmail.com","password":"passs1234"}
响应:
{"profile":{"id":15,"username":"user5","location":"","gender":"","photo_path":""},"user":{"email":"userrr5@gmail.com","username":"user5"}}
我花了很多时间来解决这个问题,如果你发现我做错了什么,我会很感激,谢谢。
答案 0 :(得分:1)
您的服务器可能会以编码形式处理数据,请尝试使用此代码
@POST("/api/users/signup")
@FormUrlEncoded
Call<JsonResponseSignUp> signUp( @Field("username") String username, @Field("email") String email, @Field("password") String password);