【发布时间】:2016-12-20 19:21:27
【问题描述】:
这是我的注册页面。我想通过 Json 对象或 bean 类使用 Retrofit 库将所有数据发布到服务器。我正在使用 Retrofit 1.9。
private void insertUser(String name, String password,String contact_Number,String email, String country_Code,String deviceId){
//Here we will handle the http request to insert user to mysql db
//Creating a RestAdapter
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(Constants.BASE_URL) //Setting the Root URL
.build(); //Finally building the adapter
//Creating object for our interface
RequestInterface api = adapter.create(RequestInterface.class);
//Defining the method insertuser of our interface
api.insertUser(
//Passing the values by getting it from editTexts
et_Name.getText().toString(),
et_ContactNumber.getText().toString(),
et_CountryCode.getText().toString(),
et_Email.getText().toString(),
et_Password.getText().toString(),
et_DeviceId.getText().toString(),
//Creating an anonymous callback
new Callback<Response>() {
@Override
public void success(Response result, Response response) {
//On success we will read the server's output using bufferedreader
//Creating a bufferedreader object
BufferedReader reader = null;
//An string to store output from the server
String output = "";
try {
//Initializing buffered reader
reader = new BufferedReader(new InputStreamReader(result.getBody().in()));
//Reading the output in the string
output = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
//Displaying the output as a toast
Toast.makeText(getActivity(), output, Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
//If any error occured displaying the error as toast
Toast.makeText(getActivity(), error.toString(),Toast.LENGTH_LONG).show();
}
}
);
}
}
这是我的接口类请求接口
@FormUrlEncoded
@POST("/create_site_client.php")
public void insertUser(
@Field("name") String name,
@Field("contact_no") String contact_Number,
@Field("country_code") String country_Code,
@Field("email") String email,
@Field("password") String password,
@Field("device_id") String device_id,
Callback<Response> callback);
我想使用 Retrofit 制作登录和注册页面 当我在上面运行代码时,它给出错误:
{"status":"fail","error":{"error_code":1062,"error_message":"键 'contact_no' 的重复条目 '7777777777'"},"data":null}
我想将用户输入发送到服务器:{"name":"dfgfg","contact_no":"7777777777","country_code":"91","email":"dd@gmail.com", "密码":"123","device_id":"1234"}
【问题讨论】: