如何使用post方法使用改进获得阵列响应

时间:2018-06-18 07:17:19

标签: android android-studio retrofit2

我的回答是

 {
"status": 1,
"vet_array": [
{
  "vet_ID": "17",
  "vet_Name": "sainadh",
  "vet_practice_Name": "Animals Wellness Center",
  "vet_address_street_address1": "550 Haynes St",
  "vet_address_street_address2": "690 Haynes St",
  "vet_address_city": "Chicago",
  "vet_address_state": "TN",
  "vet_address_zip": "540",
  "vet_email": "sainadh9@gmail.com",
  "vet_contact_home": "",
  "time": "00:00:00",
  "timeZone": "",
  "status": "1",
  "vet_contact_mobile": "6148327241523",
  "created_by": "",
  "created_datetime": "2018-03-13 08:45:31",
  "updated_by": "",
  "update_datetime": "2018-04-22 21:22:14"
},
{
  "vet_ID": "18",
  "vet_Name": "kumar",
  "vet_practice_Name": "Animal Hospital",
  "vet_address_street_address1": "202 E Main St.",
  "vet_address_street_address2": "",
  "vet_address_city": "vijayawada",
  "vet_address_state": "VZA",
  "vet_address_zip": "29",
  "vet_email": "kumar@gmail.com"
  "vet_contact_home": "",
  "time": "00:00:00",
  "timeZone": "",
  "status": "1",
  "vet_contact_mobile": "7061942494",
  "created_by": "",
  "created_datetime": "2018-03-14 10:48:14",
  "updated_by": "",
  "update_datetime": "2018-04-20 03:47:47"
}
      ]
  }

我正在使用改装帖子。如果我传递关键变量,我将获得以上数据.Below是我的代码

DataResponse.java

     public class DataResponse {
    @SerializedName("status")
    @Expose
    private Integer status;
    @SerializedName("vet_array")
    @Expose
    private List<VetArray> vetArray = null;

    public Integer getStatus() {
        return status;
    }

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

    public List<VetArray> getVetArray() {
        return vetArray;
    }

    public void setVetArray(List<VetArray> vetArray) {
        this.vetArray = vetArray;

    }
}

VetArray.java

 public class VetArray {
 @SerializedName("vet_ID")
@Expose
private String vetID;
@SerializedName("vet_Name")
@Expose
private String vetName;
@SerializedName("vet_practice_Name")
@Expose
private String vetPracticeName;
@SerializedName("vet_address_street_address1")
@Expose
private String vetAddressStreetAddress1;
@SerializedName("vet_address_street_address2")
@Expose
private String vetAddressStreetAddress2;
@SerializedName("vet_address_city")
@Expose
private String vetAddressCity;
@SerializedName("vet_address_state")
@Expose
private String vetAddressState;
@SerializedName("vet_address_zip")
@Expose
private String vetAddressZip;
@SerializedName("vet_email")
@Expose
private String vetEmail;
@SerializedName("vet_contact_home")
@Expose
private String vetContactHome;
@SerializedName("time")
@Expose
private String time;
@SerializedName("timeZone")
@Expose
private String timeZone;
@SerializedName("status")
@Expose
private String status;
@SerializedName("vet_contact_mobile")
@Expose
private String vetContactMobile;
@SerializedName("created_by")
@Expose
private String createdBy;
@SerializedName("created_datetime")
@Expose
private String createdDatetime;
@SerializedName("updated_by")
@Expose
private String updatedBy;
@SerializedName("update_datetime")
@Expose
private String updateDatetime;
public VetArray(String vetID){
    this.vetID=vetID;
}

public String getVetID() {
    return vetID;
}

public void setVetID(String vetID) {
    this.vetID = vetID;
}

public String getVetName() {
    return vetName;
}

public void setVetName(String vetName) {
    this.vetName = vetName;
}

public String getVetPracticeName() {
    return vetPracticeName;
}

public void setVetPracticeName(String vetPracticeName) {
    this.vetPracticeName = vetPracticeName;
}

public String getVetAddressStreetAddress1() {
    return vetAddressStreetAddress1;
}

public void setVetAddressStreetAddress1(String vetAddressStreetAddress1) {
    this.vetAddressStreetAddress1 = vetAddressStreetAddress1;
}

public String getVetAddressStreetAddress2() {
    return vetAddressStreetAddress2;
}

public void setVetAddressStreetAddress2(String vetAddressStreetAddress2) {
    this.vetAddressStreetAddress2 = vetAddressStreetAddress2;
}

public String getVetAddressCity() {
    return vetAddressCity;
}

public void setVetAddressCity(String vetAddressCity) {
    this.vetAddressCity = vetAddressCity;
}

public String getVetAddressState() {
    return vetAddressState;
}

public void setVetAddressState(String vetAddressState) {
    this.vetAddressState = vetAddressState;
}

public String getVetAddressZip() {
    return vetAddressZip;
}

public void setVetAddressZip(String vetAddressZip) {
    this.vetAddressZip = vetAddressZip;
}

public String getVetEmail() {
    return vetEmail;
}

public void setVetEmail(String vetEmail) {
    this.vetEmail = vetEmail;
}

public String getVetContactHome() {
    return vetContactHome;
}

public void setVetContactHome(String vetContactHome) {
    this.vetContactHome = vetContactHome;
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}

public String getTimeZone() {
    return timeZone;
}

public void setTimeZone(String timeZone) {
    this.timeZone = timeZone;
}

public String getStatus() {
    return status;
}

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

public String getVetContactMobile() {
    return vetContactMobile;
}

public void setVetContactMobile(String vetContactMobile) {
    this.vetContactMobile = vetContactMobile;
}

public String getCreatedBy() {
    return createdBy;
}

public void setCreatedBy(String createdBy) {
    this.createdBy = createdBy;
}

public String getCreatedDatetime() {
    return createdDatetime;
}

public void setCreatedDatetime(String createdDatetime) {
    this.createdDatetime = createdDatetime;
}

public String getUpdatedBy() {
    return updatedBy;
}

public void setUpdatedBy(String updatedBy) {
    this.updatedBy = updatedBy;
}

public String getUpdateDatetime() {
    return updateDatetime;
}

public void setUpdateDatetime(String updateDatetime) {
    this.updateDatetime = updateDatetime;
}

    }

APIService界面

public interface APIService {
    //The register call
    @FormUrlEncoded
    @POST("x.php")
    Call<DataResponse> createUser(
            @Field("key") String key);
     }

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        userData();
      }

    private void userData() {
        //defining a progress dialog to show while signing up
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Signing Up...");
        progressDialog.show();
        //building retrofit object
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("parenturl")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //Defining retrofit api service
        APIService service = retrofit.create(APIService.class);
        //defining the call
        String key = "xxxxxxxxxxxx";
        Call<DataResponse> call = service.createUser(key);
        //calling the api
        call.enqueue(new Callback<DataResponse>() {
            @Override
            public void onResponse(Call<DataResponse> call, Response<DataResponse> response) {
                //hiding progress dialog
                progressDialog.dismiss();

                //displaying the message from the response as toast
                String getvetResponse = String.valueOf(response.body().getVetArray());
                Toast.makeText(getApplicationContext(), "hi" + response.body().getVetArray(), Toast.LENGTH_LONG).show();
                Log.d("response", getvetResponse);


            }

            @Override
            public void onFailure(Call<DataResponse> call, Throwable t) {

            }

            });
    }
     }

我正在获得状态值。但是当我试图获得vetarray时,它会显示“VetArray @ e786e85”这样的反应。但我需要像vetarray的上述反应。

2 个答案:

答案 0 :(得分:0)

初始化ArrayList

ArrayList<VetArray> mVetArrayList=new ArrayList();

现在在您的通话方法

call.enqueue(new Callback<DataResponse>() {
            @Override
            public void onResponse(Call<DataResponse> call, Response<DataResponse> response) {
                //hiding progress dialog
                progressDialog.dismiss();
            DataResponse mResponse=response.body(); 
            mVetArrayList=mResponse.getVetArray();
            //This "mVetArrayList" contains your full VetArray
            }
            @Override
            public void onFailure(Call<DataResponse> call, Throwable t) {

            }
          });

答案 1 :(得分:0)

DataResponse.java

public class DataResponse {
    @SerializedName("status")
    @Expose
    private Integer status;
    @SerializedName("vet_array")
    @Expose
    private List<VetArray> vetArray = null;

    public Integer getStatus() {
        return status;
    }

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

    public List<VetArray> getVetArray() {
        return vetArray;
    }

    public void setVetArray(List<VetArray> vetArray) {
        this.vetArray = vetArray;

    }

VetArray.java

public class VetArray {
    @SerializedName("vet_ID")
    @Expose
    private String vetID;
    @SerializedName("vet_Name")
    @Expose
    private String vetName;
    @SerializedName("vet_practice_Name")
    @Expose
    private String vetPracticeName;
    @SerializedName("vet_address_street_address1")
    @Expose
    private String vetAddressStreetAddress1;
    @SerializedName("vet_address_street_address2")
    @Expose
    private String vetAddressStreetAddress2;
    @SerializedName("vet_address_city")
    @Expose
    private String vetAddressCity;
    @SerializedName("vet_address_state")
    @Expose
    private String vetAddressState;
    @SerializedName("vet_address_zip")
    @Expose
    private String vetAddressZip;
    @SerializedName("vet_email")
    @Expose
    private String vetEmail;
    @SerializedName("vet_contact_home")
    @Expose
    private String vetContactHome;
    @SerializedName("time")
    @Expose
    private String time;
    @SerializedName("timeZone")
    @Expose
    private String timeZone;
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("vet_contact_mobile")
    @Expose
    private String vetContactMobile;
    @SerializedName("created_by")
    @Expose
    private String createdBy;
    @SerializedName("created_datetime")
    @Expose
    private String createdDatetime;
    @SerializedName("updated_by")
    @Expose
    private String updatedBy;
    @SerializedName("update_datetime")
    @Expose
    private String updateDatetime;

    public VetArray(String vetID) {
        this.vetID = vetID;
    }

    public String getVetID() {
        return vetID;
    }

    public void setVetID(String vetID) {
        this.vetID = vetID;
    }

    public String getVetName() {
        return vetName;
    }

    public void setVetName(String vetName) {
        this.vetName = vetName;
    }

    public String getVetPracticeName() {
        return vetPracticeName;
    }

    public void setVetPracticeName(String vetPracticeName) {
        this.vetPracticeName = vetPracticeName;
    }

    public String getVetAddressStreetAddress1() {
        return vetAddressStreetAddress1;
    }

    public void setVetAddressStreetAddress1(String vetAddressStreetAddress1) {
        this.vetAddressStreetAddress1 = vetAddressStreetAddress1;
    }

    public String getVetAddressStreetAddress2() {
        return vetAddressStreetAddress2;
    }

    public void setVetAddressStreetAddress2(String vetAddressStreetAddress2) {
        this.vetAddressStreetAddress2 = vetAddressStreetAddress2;
    }

    public String getVetAddressCity() {
        return vetAddressCity;
    }

    public void setVetAddressCity(String vetAddressCity) {
        this.vetAddressCity = vetAddressCity;
    }

    public String getVetAddressState() {
        return vetAddressState;
    }

    public void setVetAddressState(String vetAddressState) {
        this.vetAddressState = vetAddressState;
    }

    public String getVetAddressZip() {
        return vetAddressZip;
    }

    public void setVetAddressZip(String vetAddressZip) {
        this.vetAddressZip = vetAddressZip;
    }

    public String getVetEmail() {
        return vetEmail;
    }

    public void setVetEmail(String vetEmail) {
        this.vetEmail = vetEmail;
    }

    public String getVetContactHome() {
        return vetContactHome;
    }

    public void setVetContactHome(String vetContactHome) {
        this.vetContactHome = vetContactHome;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getTimeZone() {
        return timeZone;
    }

    public void setTimeZone(String timeZone) {
        this.timeZone = timeZone;
    }

    public String getStatus() {
        return status;
    }

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

    public String getVetContactMobile() {
        return vetContactMobile;
    }

    public void setVetContactMobile(String vetContactMobile) {
        this.vetContactMobile = vetContactMobile;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public String getCreatedDatetime() {
        return createdDatetime;
    }

    public void setCreatedDatetime(String createdDatetime) {
        this.createdDatetime = createdDatetime;
    }

    public String getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }

    public String getUpdateDatetime() {
        return updateDatetime;
    }

    public void setUpdateDatetime(String updateDatetime) {
        this.updateDatetime = updateDatetime;
    }

    @Override
    public String toString() {
        return new Gson().toJson(this);
    }

}

ApI服务接口

public interface APIService {
    //The register call
    @FormUrlEncoded
    @POST("xxxxx.php")
    Call<DataResponse> createUser(
            @Field("key") String key);
}

VetAdapter.java

public class VetAdapter extends RecyclerView.Adapter<VetAdapter.ViewHolder> {

    private List<VetArray> vetArrays = new ArrayList<>();
    private Context mContext;

    public VetAdapter() {
    }

    public VetAdapter(List<VetArray> vetArrays,Context context) {
        this.vetArrays = vetArrays;
        this.mContext=context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_vetdata, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        VetArray item = vetArrays.get(position);
        holder.mtv_text.setText(item.getVetName());
    }

    @Override
    public int getItemCount() {
        return vetArrays.size();
    }

    public void updateAnswers(List<VetArray> items) {
        vetArrays = items;
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView mtv_text;

        ViewHolder(View itemView) {
            super(itemView);
            mtv_text = itemView.findViewById(R.id.tv_text);
        }

        void bind(VetArray vetArray) {

            Log.d("VetID", vetArray.getVetID());

        }

    }

}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    ArrayList<VetArray> mVetArrayList = new ArrayList();
    private RecyclerView mrv_vetname;
    private VetAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mrv_vetname = findViewById(R.id.rv_vetname);
        userData();
    }

    private void userData() {
        //defining a progress dialog to show while signing up
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Signing Up...");
        progressDialog.show();
        //building retrofit object
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("parent url")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //Defining retrofit api service
        APIService service = retrofit.create(APIService.class);
        //defining the call
        String key = "awgfuhsef1347s7d8";
        Call<DataResponse> call = service.createUser(key);
        //calling the api
        call.enqueue(new Callback<DataResponse>() {
            @Override
            public void onResponse(Call<DataResponse> call, Response<DataResponse> response) {
                //hiding progress dialog
                progressDialog.dismiss();

                //displaying the message from the response as toast
//                String getvetResponse = response.body().getVetArray();
                Toast.makeText(getApplicationContext(), "hi" + 
              response.body().getVetArray(), Toast.LENGTH_LONG).show();
                Log.d("response", response.body().getVetArray().toString());
                String respond = response.body().getVetArray().toString();
                generateEmployeeList(response.body().getVetArray());


            }

            @Override
            public void onFailure(Call<DataResponse> call, Throwable t) {

            }

        });
    }

    private void generateEmployeeList(List<VetArray> vetArray) {
        mAdapter = new VetAdapter(vetArray,getApplicationContext());

        RecyclerView.LayoutManager layoutManager = new 
     LinearLayoutManager(MainActivity.this);

        mrv_vetname.setLayoutManager(layoutManager);

        mrv_vetname.setAdapter(mAdapter);
    }
}