JSON中的错误提取

时间:2017-04-11 05:56:24

标签: android json

我正面临这个错误:

04-11 11:11:52.395 28573-32038/com.allianz.azemployee D/OkHttpClient: servicePath = https://api-test.allianz.com/digithonempwebservice/rest/authService/empAppAuthentication
04-11 11:11:52.395 28573-32038/com.allianz.azemployee D/postBody: {'serviceName':'fetchleavehistory','emailID':'sayli.kapale@allianz.com','token':'837999'}
04-11 11:11:53.926 28573-32038/com.allianz.azemployee D/ServiceResponseString: {"serviceName": "fetchleavesummary", "retCode": "1", "status": "Error Fetch"}
04-11 11:11:53.954 28573-28573/com.allianz.azemployee I/FragLeaveSummary: Error: {"serviceName": "fetchleavesummary", "retCode": "1", "status": "Error Fetch"}
04-11 11:11:53.955 28573-28573/com.allianz.azemployee I/ActivityLAMS: status===Error Fetch

我在这里做的是获取员工的休假摘要,但似乎返回代码是从服务器发送错误而不是数据。我可以成功登录,但甚至看不到applied leaves。我会将以下代码粘贴到FragLeaveSummary

public class FragLeaveSummary extends Fragment{

    ListView listView;
    LeaveInfoRowAdapter adapter;
    public static final String TAG="FragLeaveSummary";
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public FragLeaveSummary() {}

    public static FragLeaveSummary newInstance(String param1, String param2) {
        FragLeaveSummary fragment = new FragLeaveSummary();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);

        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
        final View view =  inflater.inflate(R.layout.activity_my_lams,null);
        listView = (ListView)view.findViewById(R.id.listView1);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new LeaveInfoRowAdapter(getActivity());
        adapter.items.add("{'text1':'Carry Forward Leave','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Paid Leave','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Sick Leave','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Compassionate leave','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Optional Holiday','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Paternity Leave','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Compensatory Off','text2':'0 - 0'}");
        adapter.items.add("{'text1':'On duty','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Training On Duty','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Work From Home','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Flexi Weekend','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Sabbatical','text2':'0 - 0'}");
        listView.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        //fetch leave summary
        String email = Net.getInstance().getSavedUserInfoWithKey(getActivity(),Net.kKeyUserEmail);
        String token = Net.getInstance().getSavedUserInfoWithKey(getActivity(),Net.kKeyUserToken);

        String postBody = Net.getInstance().getJSONForFetchLeaveHistory(email,token);
        Net.getInstance().callServiceWithURLPart( getActivity(), Net.kURLPartEmpAuthentication, postBody, new Net.ICallServiceResult() {
            @Override
            public void serviceResult(String urlPart, boolean expected, String errorMsg) {

                if(!expected){
                    try {
                        if (errorMsg != null) {
                            Net.fastToast(getActivity(), "Unable to fetch leave summary.\n\n" + errorMsg);
                        } else {
                            Net.fastToast(getActivity(), "Unable to fetch leave summary.");
                        }
                    }catch (Exception ex){
                        Log.i(TAG,"Unable to fetch leave summary. connection Timeout");
                    }
                }
                else {
                    try {
                        Log.i(TAG, "Error: " + errorMsg);
                        JSONObject jsonObject = new JSONObject(errorMsg);

                        //changed here getString --> optString
                        String retCode = jsonObject.optString("retCode");
                        String status = jsonObject.getString("status");

                        int retCodeInt = Integer.parseInt(retCode);

                        Log.i("ActivityLAMS","status===" +status);
                        // Net.fastToast(ActivityLAMS.this,status);
                        if (retCodeInt==0){ //Fetch Leave Summary success

                            //Store user-email and token
                            Log.d("fetchLeaveSummary",errorMsg);
                            final JSONObject _jsonObject = jsonObject;
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {

                                    JSONArray leaveSummaries = null;
                                    try {
                                        leaveSummaries = _jsonObject.getJSONArray("leaveDeatils");

                                        Log.d("leaveSummaries","Count = "+leaveSummaries.length());
                                        adapter.items.removeAll(adapter.items);
                                        adapter.notifyDataSetInvalidated();


                                        for (int i=0; i<leaveSummaries.length(); i++){

                                            JSONObject leaveSummary = (JSONObject) leaveSummaries.get(i);

                                            String leaveType = leaveSummary.getString("leaveType");
                                            String leaveBal = leaveSummary.getString("leaveBal");
                                            String leaveAvl = leaveSummary.getString("leaveAvl");

                                            String leaveTypeDisplayText = "--";

                                            boolean shouldShow = false;

                                            if (leaveType.equalsIgnoreCase("CFL")){
                                                leaveTypeDisplayText = "Carry Forward Leave";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("PL")){
                                                leaveTypeDisplayText = "Paid Leave";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("SL")){
                                                leaveTypeDisplayText = "Sick Leave";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("CP")){
                                                leaveTypeDisplayText = "Compassionate Leave";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("OP")){
                                                leaveTypeDisplayText = "Optional Holiday";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("PT")){
                                                leaveTypeDisplayText = "Paternity Leave";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("CO")){
                                                leaveTypeDisplayText = "Compensatory Leave";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("OD")){
                                                leaveTypeDisplayText = "On Duty";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("TOD")){
                                                leaveTypeDisplayText = "Training On Duty";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("WFH")){
                                                leaveTypeDisplayText = "Work From Home";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("FWK")){
                                                leaveTypeDisplayText = "Flexi Weekend";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("SAB")){
                                                leaveTypeDisplayText = "Sabbatical";
                                                shouldShow = true;
                                            }

                                            if(shouldShow){
                                                adapter.items.add("{'text1':'"+leaveTypeDisplayText+"','text2':'"+leaveAvl+"     -     "+leaveBal+"'}");
                                            }


                                        }


                                        adapter.notifyDataSetChanged();
                                        listView.invalidateViews();

                                        Log.d("leaveSummaries","Count2 = "+adapter.items.size());

                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Net.fastToast( getActivity(),"Unable to get required data for leave summary");
                    }

                }
            }
        });
    }
    }

这是从ActicityLAMS调用的FragLeaveSummary

public class ActivityLAMS extends AppCompatActivity implements View.OnClickListener{

    ListView listView;
    LeaveInfoRowAdapter adapter;

    Button btn1;
    Button btn2;
    Button btn3;

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

        listView = (ListView)findViewById(R.id.listView1);
        adapter = new LeaveInfoRowAdapter(this);
        adapter.items.add("{'text1':'Carry Forward Leave','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Paid Leave','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Sick Leave','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Compassionate leave','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Optional Holiday','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Paternity Leave','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Compensatory Off','text2':'0 - 0'}");
        adapter.items.add("{'text1':'On duty','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Training On Duty','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Work From Home','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Flexi Weekend','text2':'0 - 0'}");
        adapter.items.add("{'text1':'Sabbatical','text2':'0 - 0'}");
        listView.setAdapter(adapter);

        btn1 = (Button)findViewById(R.id.btn1);
        btn1.setOnClickListener(this);

        btn2 = (Button)findViewById(R.id.btn2);
        btn2.setOnClickListener(this);

        btn3 = (Button)findViewById(R.id.btn3);
        btn3.setOnClickListener(this);

    }
    @Override
    protected void onResume(){
        super.onResume();

        //(enable / disable) approve leave button w.r.t manager.

        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)btn2.getLayoutParams();
        lp.weight = 0;
        try {
            String isManager = (String)Net.getInstance().userLoginInfo.getString("isManager");
            if (isManager!=null && isManager.equalsIgnoreCase("yes")){
                lp.weight = 10;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
       // lp.weight = 10; /// TO test only
        btn2.setLayoutParams(lp);
        btn2.requestLayout();

        //fetch leave summary
        String email = Net.getInstance().getSavedUserInfoWithKey(this,Net.kKeyUserEmail);
        String token = Net.getInstance().getSavedUserInfoWithKey(this,Net.kKeyUserToken);

        String postBody = Net.getInstance().getJSONForFetchLeaveHistory(email,token);
        Net.getInstance().callServiceWithURLPart(this, Net.kURLPartEmpAuthentication, postBody, new Net.ICallServiceResult() {
            @Override
            public void serviceResult(String urlPart, boolean expected, String errorMsg) {
                if(!expected){
                    if(errorMsg!=null){
                        Net.fastToast(ActivityLAMS.this,"Unable to fetch leave summary.\n\n"+errorMsg);
                    }
                    else {
                        Net.fastToast(ActivityLAMS.this,"Unable to fetch leave summary.");
                    }
                }
                else {
                    try {
                        JSONObject jsonObject = new JSONObject(errorMsg);
                        //Change getString --> optString
                        String retCode = jsonObject.optString("retCode");
                        String status = jsonObject.getString("status");

                        int retCodeInt = Integer.parseInt(retCode);

                        Log.i("ActivityLAMS","status===" +status);
                       // Net.fastToast(ActivityLAMS.this,status);

                        if (retCodeInt==0){ //Fetch Leave Summary success

                            //Store user-email and token
                            Log.d("fetchLeaveSummary",errorMsg);

                            final JSONObject _jsonObject = jsonObject;

                            ActivityLAMS.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {

                                    JSONArray leaveSummaries;
                                    try {
                                        leaveSummaries = (JSONArray) _jsonObject.getJSONArray("leaveDeatils");
                                        Log.d("leaveSummaries","Count = "+leaveSummaries.length());
ActivityLAMS.this.adapter.items.removeAll(ActivityLAMS.this.adapter.items);
                                        ActivityLAMS.this.adapter.notifyDataSetInvalidated();
                                        for (int i=0; i<leaveSummaries.length(); i++){

                                            JSONObject leaveSummary = (JSONObject) leaveSummaries.get(i);

                                            String leaveType = leaveSummary.getString("leaveType");
                                            String leaveBal = leaveSummary.getString("leaveBal");
                                            String leaveAvl = leaveSummary.getString("leaveAvl");

                                            String leaveTypeDisplayText = "--";

                                            boolean shouldShow = false;

                                            if (leaveType.equalsIgnoreCase("CFL")){
                                                leaveTypeDisplayText = "Carry Forward Leave";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("PL")){
                                                leaveTypeDisplayText = "Paid Leave";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("SL")){
                                                leaveTypeDisplayText = "Sick Leave";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("CP")){
                                                leaveTypeDisplayText = "Compassionate Leave";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("OP")){
                                                leaveTypeDisplayText = "Optional Holiday";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("PT")){
                                                leaveTypeDisplayText = "Paternity Leave";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("CO")){
                                                leaveTypeDisplayText = "Compensatory Leave";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("OD")){
                                                leaveTypeDisplayText = "On Duty";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("TOD")){
                                                leaveTypeDisplayText = "Training On Duty";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("WFH")){
                                                leaveTypeDisplayText = "Work From Home";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("FWK")){
                                                leaveTypeDisplayText = "Flexi Weekend";
                                                shouldShow = true;
                                            }
                                            if (leaveType.equalsIgnoreCase("SAB")){
                                                leaveTypeDisplayText = "Sabbatical";
                                                shouldShow = true;
                                            }

                                            if(shouldShow){
                                                ActivityLAMS.this.adapter.items.add("{'text1':'"+leaveTypeDisplayText+"','text2':'"+leaveAvl+"     -     "+leaveBal+"'}");
                                            }
                                        }
ActivityLAMS.this.adapter.notifyDataSetChanged();
                                        ActivityLAMS.this.listView.invalidateViews();

                                        Log.d("leaveSummaries","Count2 = "+ActivityLAMS.this.adapter.items.size());

                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Net.fastToast(ActivityLAMS.this,"Unable to get required data for leave summary");
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        if(v==btn1){
            Intent intent = new Intent(this,ActivityLAMSApplyLeave.class);
            startActivity(intent);
        }
        if(v==btn2){
            /*Intent intent = new Intent(this,ActivityLAMSApproveLeave.class);
            startActivity(intent);*/
        }
        if(v==btn3){
            Intent intent = new Intent(this,ActivityViewAppliedLeaves.class);
            startActivity(intent);
        }
    }
}

有时候它会给我这样的错误,我通过放optString而不是getString来解决这个错误,但现在不行了:

04-11 11:08:09.802 28573-28573/com.allianz.azemployee W/System.err: org.json.JSONException: End of input at character 0 of 
04-11 11:08:09.802 28573-28573/com.allianz.azemployee W/System.err:     at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
04-11 11:08:09.802 28573-28573/com.allianz.azemployee W/System.err:     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
04-11 11:08:09.803 28573-28573/com.allianz.azemployee W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:156)
04-11 11:08:09.803 28573-28573/com.allianz.azemployee W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
04-11 11:08:09.803 28573-28573/com.allianz.azemployee W/System.err:     at com.allianz.azemployee.FragLeaveSummary$1.serviceResult(FragLeaveSummary.java:156)
04-11 11:08:09.803 28573-28573/com.allianz.azemployee W/System.err:     at com.allianz.azemployee.Net$1$1.run(Net.java:420)
04-11 11:08:09.803 28573-28573/com.allianz.azemployee W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
04-11 11:08:09.803 28573-28573/com.allianz.azemployee W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
04-11 11:08:09.803 28573-28573/com.allianz.azemployee W/System.err:     at android.os.Looper.loop(Looper.java:148)
04-11 11:08:09.803 28573-28573/com.allianz.azemployee W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
04-11 11:08:09.803 28573-28573/com.allianz.azemployee W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-11 11:08:09.803 28573-28573/com.allianz.azemployee W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
04-11 11:08:09.803 28573-28573/com.allianz.azemployee W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

任何帮助都将不胜感激。

0 个答案:

没有答案