预期在BEGIN_OBJECT进行翻新,但在POST方法中为BEGIN_ARRAY

时间:2018-10-13 16:56:10

标签: android retrofit2

我正在编写翻新的get和post方法来与http://www.json-generator.com/api/json/get/bUAQsEtVCG?indent=2上的json文件进行通信。我对get方法没有任何问题,因为它可以正常工作,但是post方法存在问题。它向我显示此错误:预期为BEGIN_OBJECT,但在第1行第2列路径$处为BEGIN_ARRAY。

API.Java

package com.example.yee_c.retrofit;

import java.util.List;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;

public interface API {

    String BASE_URL = "http://www.json-generator.com/api/json/";

    @GET("get/bUAQsEtVCG?indent=2")
    Call<List<User>> getUsers();

    @POST("get/bUAQsEtVCG?indent=2")
    Call<User> postUsers(@Body User user);
}

User.Java

package com.example.yee_c.retrofit;

import com.google.gson.annotations.SerializedName;

public class User {
    @SerializedName("id")
    private int id;

    @SerializedName("username")
    private String username;

    @SerializedName("password")
    private String password;

    @SerializedName("name")
    private String name;

    @SerializedName("balance")
    private double balance;

    @SerializedName("accountNumber")
    private String accountNumber;

    @SerializedName("mobileNumber")
    private String mobileNumber;

    @SerializedName("email")
    private String email;


    public User(int id, String username, String password, String name, double balance, String accountNumber, String mobileNumber, String email)
    {
        this.id = id;
        this.username = username;
        this.password = password;
        this.name = name;
        this.balance = balance;
        this.accountNumber = accountNumber;
        this.mobileNumber = mobileNumber;
        this.email = email;
    }
    public int getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getName() {
        return name;
    }

    public double getBalance() {
        return balance;
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public String getEmail() {
        return email;
    }
}

RetrofitClient.Java

package com.example.yee_c.retrofit;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {
    private static RetrofitClient mInstance;
    private Retrofit retrofit;

    private RetrofitClient() {
        retrofit = new Retrofit.Builder()
                .baseUrl(API.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
                .build();
    }
    public static synchronized RetrofitClient getmInstance(){
        if (mInstance == null) {
            mInstance = new RetrofitClient();
        }
        return mInstance;
    }

    public API getApi(){
        return retrofit.create(API.class);
    }
}

MainActivity.Java

package com.example.yee_c.retrofit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static java.lang.Double.parseDouble;

public class MainActivity extends AppCompatActivity {
    String TAG = "MainActivity.java";

    EditText usernameET,nameET,pwET,balET,accET,phoneET,emailET;
    Button btnSubmit;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        usernameET = findViewById(R.id.editUserName);
        nameET = findViewById(R.id.editName);
        pwET = findViewById(R.id.editPassword);
        balET = findViewById(R.id.editBal);
        accET = findViewById(R.id.editAccount);
        phoneET = findViewById(R.id.editMobile);
        emailET = findViewById(R.id.editEmail);
        btnSubmit = findViewById(R.id.submitBtn);

        //calling the method to display the heroes
        getUsers();

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                User user= new User(
                        '5',
                        usernameET.getText().toString(),
                        nameET.getText().toString(),
                        pwET.getText().toString(),
                        parseDouble(balET.getText().toString()),
                        accET.getText().toString(),
                        phoneET.getText().toString(),
                        emailET.getText().toString());
                postUsers(user);
            }
        });

    }

    private void getUsers() {
        Call<List<User>> call = RetrofitClient.getmInstance().getApi().getUsers();

        call.enqueue(new Callback<List<User>>() {
            @Override
            public void onResponse(Call<List<User>> call, Response<List<User>> response) {

                Log.d(TAG,"Successfully response");

                List<User> userList = response.body();

                //Creating an String array for the ListView

                //looping through the users and displaying user info where ID = 1
                for (int i = 0; i < userList.size(); i++) {
                    if(userList.get(i).getId() == 2) {
                        usernameET.setText(userList.get(i).getUsername());
                        pwET.setText(userList.get(i).getPassword());
                        nameET.setText(userList.get(i).getName());
                        String bal = Double.toString(userList.get(i).getBalance());
                        balET.setText(bal);
                        accET.setText(userList.get(i).getAccountNumber());
                        phoneET.setText(userList.get(i).getMobileNumber());
                        emailET.setText(userList.get(i).getEmail());
                    }
                }
            }

            @Override
            public void onFailure(Call<List<User>> call, Throwable t) {
                Log.d(TAG,t.getMessage());
            }
        });
    }


    private void postUsers(User user) {

        Call<User> userCall = RetrofitClient.getmInstance().getApi().postUsers(user);

        userCall.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                int statusCode = response.code();
                Toast.makeText(getApplicationContext(),"Response Code " + statusCode,Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Log.d(TAG,"onFailure" + t.getMessage());
            }
        });

    }
}

非常感谢您的帮助。谢谢。

0 个答案:

没有答案