【问题标题】:How to create Json Object and send to server using retrofit in Android如何在 Android 中使用改造创建 Json 对象并发送到服务器
【发布时间】: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"}

【问题讨论】:

    标签: android retrofit


    【解决方案1】:

    请为您发送的数据粘贴 logcat。我从错误消息中观察到电话号码 7777777777 已经保存在您的服务器数据库中,并且它是唯一的。所以你得到了错误。请尝试使用其他真实电话号码。

    【讨论】:

    • {"status":"fail","error":{"errorcode":"9001","errormesssage":"请输入所有必填字段"},"data":"null" }
    • 请粘贴您要发送的数据。如果改造日志没有启用,启用它并检查。您尚未添加任何签入代码,因此请确保您填写了所有必要的字段。如果api中需要任何header,请检查。
    • 这是我发送服务器的 Api。 example.com/site/create_site_client.php。上面我在我的界面中提到我发​​送到服务器的所有字段。
    【解决方案2】:
    1. 通过创建占位符类对象并在其中传递字符串值。
    2. 如果我使用改造 1.9.0,你必须传递用户对象和回调,如下所示。

      User user = new User(name,email,contact_no,country_code,password,device_id);
      
      //Defining the method insertuser of our interface
      
      api.insertUser(user,new Callback<User>() {
      
          @Override
          public void success(User result, Response response) {
              BufferedReader reader = null;
      
              //An string to store output from the server
              String output = "";
      
              try {
                  //Initializing buffered reader
                  reader = new BufferedReader(new InputStreamReader(response.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();
      
          }
      

    我的界面是这样的:

    @POST("/create_site_client.php")
     void insertUser(@Body User user,Callback<User> cb);
    

    【讨论】:

      猜你喜欢
      • 2015-04-05
      • 2018-09-24
      • 2017-06-02
      • 2015-10-18
      • 1970-01-01
      • 2019-10-03
      • 2011-09-16
      • 2017-04-17
      • 1970-01-01
      相关资源
      最近更新 更多