改进POST和NodeJS

时间:2017-06-14 01:47:01

标签: android node.js web-services retrofit retrofit2

我无法通过成功回复运行我的应用。我想报名参加。 我在活动中收到了关于onResponse的回复:

“Response {protocol = http / 1.1,code = 400,message = Bad Request,url = http://myip:8122/consumer/signup}”

我不确定,但我认为这不是发送参数。

我在PostMan上测试了它并且它有效。我选择了“POST”并写了这个网址:http://myip:8122/consumer/signup?f=Maria&l=Lucia&e=marlucia@hotmail.com&p=pass123

在Postman上它成功运作:{“status”:“成功”,“消息”:“创建用户”}

我的活动:

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BonaService.BASE_URL_SIGN_UP)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        BonaService service = retrofit.create(BonaService.class);

        Call<ResponseInsertUser> callRegisterUser = service.registerUser("John", "Clark", "jclark@gmail.com", "vtnctrump");

        callRegisterUser.enqueue(new Callback<ResponseInsertUser>() {
            @Override
            public void onResponse(Call<ResponseInsertUser> call, Response<ResponseInsertUser> response) {
                if(!response.isSuccessful()){
                    // HERE!!!!
                    Log.i("TAG", "Error: "+ response.code());
                } else {
                    ResponseInsertUser body = response.body();
                }
            }

            @Override
            public void onFailure(Call<ResponseInsertUser> call, Throwable t) {
                Log.e("TAG", "ERROR: "+ t.getMessage());
            }
        });

BonaService:

public interface BonaService {
    String BASE_URL         = "http://myip:5000/";
    String BASE_URL_SIGN_UP = "http://myip:8122/";

    @GET("listRandom")
    Call<BonaCatalog> listCatalog();

    @FormUrlEncoded
    @POST("consumer/signup")
    Call<ResponseInsertUser> registerUser(@Field("f") String f,
                                          @Field("l") String l,
                                          @Field("e") String e,
                                          @Field("p") String p);
}

ResponseInsertUser:

public class ResponseInsertUser {

    private String status, message;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

我的节点代码:

function insertUser(req, res, next) {
var first = req.query.f;
  db.any('insert into user(id, first_name, last_name, email, password, user_status_id) values((select max(id) from user)+1, $1, $2, $3, $4, 1)', [req.query.f, req.query.l, req.query.e, req.query.p])
    .then(function (data) {
      res.status(201).json({
        status: 'success',
        message: 'created user'
      })
    })
    .catch(function (err){
     res.status(400).json({
      status:'fail', 
      message: 'Error!', 
      test: first
    });
  });
}

1 个答案:

答案 0 :(得分:1)

您需要@Query个参数,而不是@Field参数,因为它使用?key=value&语法添加到网址

应该是快速更改,但您可能还想删除FormUrlEncoded