本站消息

站长简介/公众号


站长简介:高级工程师,爱好交友,无偿辅导python和前端,技术交流,面试指导,找工作指导,瞎聊都可加我微信i88811i哈,欢迎欢迎!也欢迎加入程序员交流群,专属程序员的圈子,加我微信拉你进群.欢迎关注我的微信公众号:程序员总部,程序员的家,探索程序员的人生之路!分享IT最新技术,关注行业最新动向,让你永不落伍。了解同行们的工资,生活工作中的酸甜苦辣,谋求程序员的最终出路!

  价值13000svip视频教程,python大神匠心打造,零基础python开发工程师视频教程全套,基础+进阶+项目实战,包含课件和源码

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

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

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

发布于2022-04-28 07:37     阅读(1691)     评论(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);

站长简介:高级工程师,爱好交友,无偿辅导python和前端,技术交流,面试指导,找工作指导,瞎聊都可加我微信i88811i哈,欢迎欢迎!也欢迎加入程序员交流群,专属程序员的圈子,加我微信拉你进群
欢迎关注我的公众号:程序员总部,关注公众号回复python,免费领取 全套python视频教程,关注公众号回复充值+你的账号,免费为您充值1000积分







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

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

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

来源:python黑洞网

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

26 0
收藏该文
已收藏

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