简体   繁体   English

如何在Android中使用Retrofit登录Django服务器?

[英]How to login Django server using Retrofit in android?

Hi I just created an interface as below: 嗨,我刚刚创建了如下界面:

public interface UserClient {

    @POST("login")
    Call<UserInfo> login(@Body Login login);

}

UserInfo Class: UserInfo类别:

public class UserInfo {
private String token;

public String getToken(){
    return token;
}

public void setToken(String token){
   this.token = token;
  }
}

And here is the main code: 这是主要代码:

Retrofit.Builder builder = new  Retrofit.Builder()         
 .baseUrl("http://amirhoseinbidar.pythonanywhere.com/")
                .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();

    UserClient userClient = retrofit.create(UserClient.class);
    Login login = new Login("plantdg", "1234");

    Call<UserInfo> userCall = userClient.login(login);
    userCall.enqueue(new Callback<UserInfo>() {
        @Override
        public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
            if (response.isSuccessful()){
                Toast.makeText(Main2Activity.this, "connection successful " +
                        response.body().getToken(), Toast.LENGTH_SHORT).show();
            }else {
                textView.setText(response.raw().toString());
                //The message is : Response{protocol=http/1.1, code=500, message=Internal Server Error, url=http://amirhoseinbidar.pythonanywhere.com/login}
            }
        }

        @Override
        public void onFailure(Call<UserInfo> call, Throwable t) {
            Toast.makeText(Main2Activity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

So the problem is response is not successful. 所以问题是响应不成功。 Anyone can Help? 有人可以帮忙吗?

note: Login class contains two vars (username, password) and a constructor. 注意:登录类包含两个变量(用户名,密码)和一个构造函数。

I solved it. 我解决了 I posted raw data type to server instead of form data type. 我将原始数据类型发布到服务器,而不是表单数据类型。 so I did some little changes in the interface: 所以我在界面上做了一些小改动:

@Headers( "Content-Type: application/json" )
@POST("login/")
Call<UserInfo> login(@Body JsonObject login);

note: Backslash was important for me. 注意:反斜杠对我很重要。

and I used a method that returns a JsonObject: 并且我使用了一个返回JsonObject的方法:

private JsonObject returnRaw() {

    JsonObject gson = new JsonObject();

    try {

        JSONObject jsonObj_ = new JSONObject();
        jsonObj_.put("username", "plantdg");
        jsonObj_.put("password", "1234");
        JsonParser jsonParser = new JsonParser();
        gson = (JsonObject) jsonParser.parse(jsonObj_.toString());

    } catch (JSONException e) {
        e.printStackTrace();
    }

    return gson;
}

And in the main code I passed the returnRaw() as the parameter to the login method: 在主代码中,我将returnRaw()作为参数传递给登录方法:

Call<UserInfo> userCall = userClient.login(returnRaw());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM