尝试使用改造

时间:2017-08-17 09:05:39

标签: android json web-services retrofit

我正在尝试使用改造将数据发布到http://biokod.net:8990/api/users/,但我没有获得成功的响应。

json请求示例: -

   {"params": {"password": 
"8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92", "email": 
"jkowalski@test.pl"}, "method": "login", "id": "12345", "jsonrpc": "2.0"}

成功回应的例子: -

{'jsonrpc': '2.0', 'id': '12345', 'result': {'roles': [0], 'token': 
'VDKlZYXucIWdhvnbgFEPitkN', 'uid': 1}}

用户模型: -

public class User {

    private Params params;

    private String method;

    private String id;

    private String jsonrpc;

    public Params getParams() {
        return params;
    }

    public void setParams(Params params) {
        this.params = params;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getJsonrpc() {
        return jsonrpc;
    }

    public void setJsonrpc(String jsonrpc) {
        this.jsonrpc = jsonrpc;
    }
}

Params模型:

public class Params {

    private String password;

    private String email;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

服务:

public interface UserClient {
    @POST("user")
    Call<User> login(@Body User user);
}

然后在按钮上单击我创建用户对象并将其作为参数发送到sendNetworkRequest方法

Params params = new Params();
params.setEmail("jkowalski@test.pl");
params.setPassword("8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92");
User user = new User();
user.setParams(params);
user.setMethod("login");
user.setId("12345");
user.setJsonrpc("2.0");

sendNetworkRequest(user);

然而,当我检查响应是否成功时,我得到了错误

private void sendNetworkRequest(User user){
        Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl("http://biokod.net:8990/api/users/")
                .addConverterFactory(GsonConverterFactory.create());

        Retrofit retrofit = builder.build();
        UserClient client = retrofit.create(UserClient.class);
        Call<User> call = client.login(user);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if(response.isSuccessful())
                    Log.d("testtest", response.body().toString());
                else
                    Log.d("testtest", "fail");
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {

            }
        });
    }

无法弄清楚我做错了什么,感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

感谢Satan Pandeya的评论我发现我有错误的端点和网址,正确的网址应该是biokod.net:8990/api和端点用户。谢谢你的帮助。