应该是一个字符串,但在第1行第13列是begin_array

时间:2018-12-05 09:56:24

标签: java android retrofit gson

这是我的JSON输出:

{
"Message": [
    {
        "Emp_Id": 7,
        "Emp_First_Name": "Mohammad",
        "Emp_Last_Name": "Malek",
        "Emp_Email": "malek@cloudester.com",
        "Emp_Address": "A-82/2,Ahmed nagar Society, Manubar Road, Bharuch-392001",
        "Emp_Phone_No": "8140717602",
        "Emp_Password": "3f645aeff486fd9bda7ded484a43f701",
        "Emp_Photo": "/Images/2018_07_02_05_30_24.jpg",
        "Is_Resign": 0,
        "Joining_Date": "2017-12-12T00:00:00.000Z",
        "Resign_Date": "2017-12-12T00:00:00.000Z",
        "sms_status": 0
    }
]}

这是我的pojo课:

public class Result {
@SerializedName("Message")
private String message;

@SerializedName("Emp_Id")
@Expose
private Integer empId;
@SerializedName("Emp_First_Name")
@Expose
private String empFirstName;
@SerializedName("Emp_Last_Name")
@Expose
private String empLastName;
@SerializedName("Emp_Email")
@Expose
private String empEmail;
@SerializedName("Emp_Address")
@Expose
private String empAddress;
@SerializedName("Emp_Phone_No")
@Expose
private String empPhoneNo;
@SerializedName("Emp_Password")
@Expose
private String empPassword;
@SerializedName("Emp_Photo")
@Expose
private String empPhoto;
@SerializedName("Is_Resign")
@Expose
private Integer isResign;
@SerializedName("Joining_Date")
@Expose
private String joiningDate;
@SerializedName("Resign_Date")
@Expose
private String resignDate;
@SerializedName("sms_status")
@Expose
private Integer smsStatus;

public Integer getEmpId() {
    return empId;
}

public String getEmpFirstName() {
    return empFirstName;
}

public String getEmpLastName() {
    return empLastName;
}

public String getEmpEmail() {
    return empEmail;
}

public String getEmpAddress() {
    return empAddress;
}

public String getEmpPhoneNo() {
    return empPhoneNo;
}

public String getEmpPassword() {
    return empPassword;
}

public String getEmpPhoto() {
    return empPhoto;
}

public Integer getIsResign() {
    return isResign;
}

public String getJoiningDate() {
    return joiningDate;
}

public String getResignDate() {
    return resignDate;
}

public Integer getSmsStatus() {
    return smsStatus;
}

@SerializedName("user")
private User user;

public Result(String message, User user,String empFirstName,String empLastName,String empEmail,String empAddress,String joiningDate) {
    this.empFirstName = empFirstName;
    this.message = message;
    this.user = user;
    this.empLastName=empLastName;
    this.empEmail=empEmail;
    this.empAddress=empAddress;
    this.joiningDate=joiningDate;
}

public String getMessage() {
    return message;
}

public User getUser() {
    return user;
}}

当我运行活动失败时,我会收到一条吐司错误消息 “期望一个字符串,但在第1行第13列是begin_array”

这是我的片段活动类:

public class ProfileFragment extends Fragment implements View.OnClickListener {
public static final int PICK_IMAGE = 1;
public static final int REQUEST_CAMERA = 100;
public ImageView pick;
public Uri filePath;
ProgressDialog progressDialog;
EditText etfname,etlname,etemail,etaddress,etjoindt,etpos;
Context context;
public ProfileFragment() {
    // Required empty public constructor
}


public static ProfileFragment newInstance(String param1, String param2) {
    ProfileFragment fragment = new ProfileFragment();
    Bundle args = new Bundle();
    return fragment;

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Dexter.withActivity(getActivity())
            .withPermission(Manifest.permission.CAMERA)
            .withListener(new PermissionListener() {
                @Override public void onPermissionGranted(PermissionGrantedResponse response) {/* ... */}
                @Override public void onPermissionDenied(PermissionDeniedResponse response) {PermissionListener dialogPermissionListener =
                        DialogOnDeniedPermissionListener.Builder
                                .withContext(getContext())
                                .withTitle("Camera permission")
                                .withMessage("Camera permission is needed to take pictures")
                                .withButtonText(android.R.string.ok)
                                .withIcon(R.mipmap.ic_launcher)
                                .build();}
                @Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */}
            }).check();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_profile, container, false);
    pick=view.findViewById(R.id.ivEditProfileImg);
    pick.setOnClickListener(this);
    etfname=view.findViewById(R.id.etfname);
    etlname=view.findViewById(R.id.etlname);
    etemail=view.findViewById(R.id.etemail);
    etaddress=view.findViewById(R.id.etaddress);
    etjoindt=view.findViewById(R.id.etjoindate);

    final ProgressDialog progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("Fetching Data...");
    progressDialog.show();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(ApiClient.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    Fetchemployeedetailsinterface service = retrofit.create(Fetchemployeedetailsinterface.class);//Result is our pojo class
    SharedPreferences settings = getActivity().getSharedPreferences("PREFS_NAME", 0);
   String emailtoken= settings.getString("email", "").toString();
    Call<Result> call = service.Bind_Employee_Details_Based_On_Id(emailtoken);

    call.enqueue(new Callback<Result>() {
        @Override
        public void onResponse(Call<Result> call, Response<Result> response) {
            //.getMessage is POJO method to listen for final json output
            String fname=response.body().getEmpFirstName();
            String lname=response.body().getEmpLastName();
            String email=response.body().getEmpEmail();
            String address=response.body().getEmpAddress();
            String joindt=response.body().getJoiningDate();

            etfname.setText(fname);
            etlname.setText(lname);
           etemail.setText(email);
           etaddress.setText(address);
           etjoindt.setText(joindt);
            progressDialog.dismiss();
        }

        @Override
        public void onFailure(Call<Result> call, Throwable t) {
            progressDialog.dismiss();
            Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
            return view;
}


// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_leave, menu);
              super.onCreateOptionsMenu(menu,inflater);
}
@Override
public void onDetach() {
    super.onDetach();

}

@Override
public void onClick(View v) {
    PickImageDialog.build(new PickSetup()).show(getActivity());
    PickSetup setup = new PickSetup();
    PickImageDialog.build(setup)
            .setOnClick(new IPickClick() {
                @Override
                public void onGalleryClick() {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
                }

                @Override
                public void onCameraClick() {
                    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(cameraIntent, REQUEST_CAMERA);

                }
            }).show(getActivity());
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
            filePath = data.getData();
        pick.setImageURI(filePath);

    }
    else if(requestCode == REQUEST_CAMERA && resultCode == Activity.RESULT_OK){
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        pick.setImageBitmap(photo);
    }
}}

当调用oncreateview时,它将变为onfailure,并且如上所示烘烤消息。 我做错了任何帮助,我们将不胜感激。

3 个答案:

答案 0 :(得分:0)

以此更改您的pojo课

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("Message")
@Expose
private List<Message> message = null;

public List<Message> getMessage() {
return message;
}

public void setMessage(List<Message> message) {
this.message = message;
}

}


package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Message {

@SerializedName("Emp_Id")
@Expose
private Integer empId;
@SerializedName("Emp_First_Name")
@Expose
private String empFirstName;
@SerializedName("Emp_Last_Name")
@Expose
private String empLastName;
@SerializedName("Emp_Email")
@Expose
private String empEmail;
@SerializedName("Emp_Address")
@Expose
private String empAddress;
@SerializedName("Emp_Phone_No")
@Expose
private String empPhoneNo;
@SerializedName("Emp_Password")
@Expose
private String empPassword;
@SerializedName("Emp_Photo")
@Expose
private String empPhoto;
@SerializedName("Is_Resign")
@Expose
private Integer isResign;
@SerializedName("Joining_Date")
@Expose
private String joiningDate;
@SerializedName("Resign_Date")
@Expose
private String resignDate;
@SerializedName("sms_status")
@Expose
private Integer smsStatus;

public Integer getEmpId() {
return empId;
}

public void setEmpId(Integer empId) {
this.empId = empId;
}

public String getEmpFirstName() {
return empFirstName;
}

public void setEmpFirstName(String empFirstName) {
this.empFirstName = empFirstName;
}

public String getEmpLastName() {
return empLastName;
}

public void setEmpLastName(String empLastName) {
this.empLastName = empLastName;
}

public String getEmpEmail() {
return empEmail;
}

public void setEmpEmail(String empEmail) {
this.empEmail = empEmail;
}

public String getEmpAddress() {
return empAddress;
}

public void setEmpAddress(String empAddress) {
this.empAddress = empAddress;
}

public String getEmpPhoneNo() {
return empPhoneNo;
}

public void setEmpPhoneNo(String empPhoneNo) {
this.empPhoneNo = empPhoneNo;
}

public String getEmpPassword() {
return empPassword;
}

public void setEmpPassword(String empPassword) {
this.empPassword = empPassword;
}

public String getEmpPhoto() {
return empPhoto;
}

public void setEmpPhoto(String empPhoto) {
this.empPhoto = empPhoto;
}

public Integer getIsResign() {
return isResign;
}

public void setIsResign(Integer isResign) {
this.isResign = isResign;
}

public String getJoiningDate() {
return joiningDate;
}

public void setJoiningDate(String joiningDate) {
this.joiningDate = joiningDate;
}

public String getResignDate() {
return resignDate;
}

public void setResignDate(String resignDate) {
this.resignDate = resignDate;
}

public Integer getSmsStatus() {
return smsStatus;
}

public void setSmsStatus(Integer smsStatus) {
this.smsStatus = smsStatus;
}

}

比用此更改代码

Call<Example> call = service.Bind_Employee_Details_Based_On_Id(emailtoken);

call.enqueue(new Callback<Example>() {
    @Override
    public void onResponse(Call<Example> call, Response<Result> response) {
        //.getMessage is POJO method to listen for final json output
        List<Example> listResponse =response.body().getMessage();
        String fname=listResponse.get(0).getEmpFirstName();
        String lname=listResponse.get(0).getEmpLastName();
        String email=listResponse.get(0).getEmpEmail();
        String address=listResponse.get(0).getEmpAddress();
        String joindt=listResponse.get(0).getJoiningDate();

        etfname.setText(fname);
        etlname.setText(lname);
       etemail.setText(email);
       etaddress.setText(address);
       etjoindt.setText(joindt);
        progressDialog.dismiss();
    }

答案 1 :(得分:0)

使此pojo类像..

public class MessageItem{

@SerializedName("Emp_Id")
private int empId;

@SerializedName("Emp_Email")
private String empEmail;

@SerializedName("Emp_First_Name")
private String empFirstName;

@SerializedName("Emp_Address")
private String empAddress;

@SerializedName("Joining_Date")
private String joiningDate;

@SerializedName("Is_Resign")
private int isResign;

@SerializedName("Emp_Last_Name")
private String empLastName;

@SerializedName("Emp_Photo")
private String empPhoto;

@SerializedName("Resign_Date")
private String resignDate;

@SerializedName("Emp_Phone_No")
private String empPhoneNo;

@SerializedName("Emp_Password")
private String empPassword;

@SerializedName("sms_status")
private int smsStatus;

public void setEmpId(int empId){
    this.empId = empId;
}

public int getEmpId(){
    return empId;
}

public void setEmpEmail(String empEmail){
    this.empEmail = empEmail;
}

public String getEmpEmail(){
    return empEmail;
}

public void setEmpFirstName(String empFirstName){
    this.empFirstName = empFirstName;
}

public String getEmpFirstName(){
    return empFirstName;
}

public void setEmpAddress(String empAddress){
    this.empAddress = empAddress;
}

public String getEmpAddress(){
    return empAddress;
}

public void setJoiningDate(String joiningDate){
    this.joiningDate = joiningDate;
}

public String getJoiningDate(){
    return joiningDate;
}

public void setIsResign(int isResign){
    this.isResign = isResign;
}

public int getIsResign(){
    return isResign;
}

public void setEmpLastName(String empLastName){
    this.empLastName = empLastName;
}

public String getEmpLastName(){
    return empLastName;
}

public void setEmpPhoto(String empPhoto){
    this.empPhoto = empPhoto;
}

public String getEmpPhoto(){
    return empPhoto;
}

public void setResignDate(String resignDate){
    this.resignDate = resignDate;
}

public String getResignDate(){
    return resignDate;
}

public void setEmpPhoneNo(String empPhoneNo){
    this.empPhoneNo = empPhoneNo;
}

public String getEmpPhoneNo(){
    return empPhoneNo;
}

public void setEmpPassword(String empPassword){
    this.empPassword = empPassword;
}

public String getEmpPassword(){
    return empPassword;
}

public void setSmsStatus(int smsStatus){
    this.smsStatus = smsStatus;
}

public int getSmsStatus(){
    return smsStatus;
}

}

public class ResponseData {

@SerializedName("Message")
private List<MessageItem> message;

public void setMessage(List<MessageItem> message){
    this.message = message;
}

public List<MessageItem> getMessage(){
    return message;
}

}

在那之后..

 Call<ResponseData>

收到类似

的回复时
        call.enqueue(new Callback<ResponseData>() {
        @Override
        public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
            //.getMessage is POJO method to listen for final json output

            if (response.isSuccessful() && response!=null && response.body()!=null){
                List<MessageItem> message=response.body().getMessage();
                if (message!=null && !message.isEmpty()){
                    for(MessageItem messageItem:message){
                        etfname.setText(messageItem.getEmpFirstName());
                        etlname.setText(messageItem.getEmpLastName());
                        //same for others

                    }
                }
            }
        }

        @Override
        public void onFailure(Call<Result> call, Throwable t) {
            progressDialog.dismiss();
            Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_LONG).show();
        }
    });

答案 2 :(得分:0)

检查您的Result类。 Message是数组,而不是String。 要从Json生成对象,请转到http://pojo.sodhanalibrary.com/并粘贴您的Json。