使用改造将发布请求发送到服务器

时间:2019-02-18 12:12:59

标签: java android json post retrofit

我正在尝试使用改造将用户信息从注册表单发送到服务器,并且我遵循了this链接     一切看起来都很好,但是当我单击发送按钮时,什么也没发生,这是怎么回事?

这是注册活动:

public class SignUpActivity extends AppCompatActivity {

    EditText username_field;
    EditText password_field;
    EditText re_password_field;
    EditText cell_phone_field;
    EditText email_field;
    Button button;
    String username;
    String password;
    String re_pass;
    String cell_phone;
    String email;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        username_field = findViewById(R.id.sign_up_username);
        password_field = findViewById(R.id.password);
        re_password_field = findViewById(R.id.re_password);
        cell_phone_field = findViewById(R.id.sign_up_phone);
        email_field = findViewById(R.id.sign_up_email);
        button = findViewById(R.id.sign_up_btn);
        button.setOnClickListener((view)->{
            User newuser = new User(username_field.getText().toString(),password_field.getText().toString()
                                    , cell_phone_field.getText().toString(),email_field.getText().toString());
            Senddatatoserver(newuser);
        });
    }

    private void Senddatatoserver(User user){

        Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl("http://dere.ir/api/")
                .addConverterFactory(GsonConverterFactory.create());
        Retrofit retrofit = builder.build();

        UserClient client = retrofit.create(UserClient.class);

        Call<ApiToken> call = client.createAccount(user);
        call.enqueue(new Callback<ApiToken>() {
            @Override
            public void onResponse(Call<ApiToken> call, Response<ApiToken> response) {
                Toast.makeText(SignUpActivity.this,"yeah babe, yeah and user token is :"+response.body().getAPI_TOKEN(),Toast.LENGTH_SHORT);
            }

            @Override
            public void onFailure(Call<ApiToken> call, Throwable t) {
                Toast.makeText(SignUpActivity.this,"something wenr worong ):",Toast.LENGTH_SHORT);
            }
        });

    }

这是user.java类:

package Model;

public class User {
    private String name;
    private String cell_phone;
    private String password;
    private String email;

    public User(String userName,  String password,String phone, String email) {
        this.name = userName;
        this.cell_phone = phone;
        this.password = password;
       this.email = email;
    }

}

这是userclient.java接口:     包裹服务;

import Model.ApiToken;
import Model.User;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;

public interface UserClient {
    @POST("signup")
    Call<ApiToken> createAccount(@Body User user);

}

0 个答案:

没有答案