程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

改造:响应 500,内部服务器错误 [重复]

发布于2022-04-28 07:37     阅读(1971)     评论(0)     点赞(26)     收藏(3)


我正在做一个项目。Web 团队已经实现了用于注册和登录的 API。我将实现 Android 部分。因此,当我使用 Postman 向服务器发布注册请求时,它正在工作。但是当我尝试在 Android 中注册时,它返回 500-Internal Server Error。我使用改造来发送请求。这是我的代码:

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

接口接口.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;
}}

配置文件.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;
}}

用户.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"}}

我花了很多时间来解决这个问题,如果您发现我做错了,我将不胜感激,谢谢。


解决方案


Your server might handling data in encoded form, Try this code

@POST("/api/users/signup")
@FormUrlEncoded
Call<JsonResponseSignUp> signUp( @Field("username") String username, @Field("email") String email, @Field("password") String password);


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/1483745/26daddf9d4f4cdc36733/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

26 0
收藏该文
已收藏

评论内容:(最多支持255个字符)