获取字符串

时间:2017-06-04 14:09:24

标签: java android json jsonexception

我正在调用异步任务,该任务返回一个json对象,该对象具有结果,消息为字符串,结果为cost。

虽然它显示了结果,但是响应对象中的消息字符串显示json异常,因为没有结果值。

的AsyncTask

public class SearchPostsAsyncTask extends AsyncTask<String, Void, JSONObject> {
    String api;
    JSONObject jsonParams;
    Context mContext;
    private SearchPostsCallBack searchPostsCallBack;
    private ProgressDialog loadingDialog;
    private Snackbar snackbar;
    private LinearLayout parentLayout;
    private ArrayList<PostDelivery> list;
    private JSONArray listsArray;
    private JSONObject jsonObject;

    public SearchPostsAsyncTask(Context context, LinearLayout linearLayout,SearchPostsCallBack searchPostsCallBack) {

        this.mContext = context;
        this.searchPostsCallBack = searchPostsCallBack;
        this.parentLayout = linearLayout;

    }

    public interface SearchPostsCallBack {
        void doPostExecute(ArrayList<PostDelivery> list);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        loadingDialog = new ProgressDialog(mContext);

        if (!isOnline()) {

            snackbar = Snackbar.make(parentLayout, R.string.check_network, Snackbar.LENGTH_LONG);
            snackbar.show();
        } else {
            loadingDialog.show(mContext, null,mContext.getString(R.string.wait));
        }

    }

    @Override
    protected JSONObject doInBackground(String... params) {
        try {
            api = mContext.getResources().getString(R.string.url) + "requestlist";

            jsonParams = new JSONObject();
            jsonParams.put("st_lati", params[0]);
            jsonParams.put("st_longi", params[1]);
            jsonParams.put("ed_lati", params[2]);
            jsonParams.put("ed_longi", params[3]);
            jsonParams.put("pt_date", params[4]);


            ServerRequest request = new ServerRequest(api, jsonParams);
            return request.sendPostRequest(params[5]);

        } catch (JSONException je) {
            Log.e("exception",je.toString());
            return Excpetion2JSON.getJSON(je);
        } catch (Exception ue) {
            return Excpetion2JSON.getJSON(ue);
        }
    } 

    @Override
    protected void onPostExecute(JSONObject response) {
        super.onPostExecute(response);
        if (loadingDialog.isShowing())
            loadingDialog.dismiss();
        try {
            list = new ArrayList<>();


                    String result = response.getString("result");
                    String message = response.getString("message");
                    if (result.equals("1")) {


                        listsArray = response.getJSONArray("cost");

                        for (int j = 0; j < listsArray.length(); j++) {

                            jsonObject = listsArray.getJSONObject(j);


                    PostDelivery postDelivery = new PostDelivery();

                    postDelivery.setmPt_id(jsonObject.getString("pt_id"));
                    postDelivery.setmPt_name(jsonObject.getString("pt_name"));
                    postDelivery.setmPtDetail(jsonObject.getString("pt_detail"));
                    postDelivery.setmPtStartLoc(jsonObject.getString("pt_start_loc"));
                    postDelivery.setmPtEndLoc(jsonObject.getString("pt_end_loc"));
                    postDelivery.setmPtDate(jsonObject.getString("pt_date"));


                    list.add(postDelivery);

                    searchPostsCallBack.doPostExecute(list);

                }
            }

                snackbar = Snackbar.make(parentLayout, "sorry", Snackbar.LENGTH_LONG);

                snackbar.show();


    }catch (JSONException je) {
            je.printStackTrace();
            //  Toast.makeText(getApplicationContext(), je.getMessage(), Toast.LENGTH_LONG).show();
        }
    } 

}

例外与回应:

D/ServerResponse: {"result":1,"message":"Success","cost":[{"pt_id":"5","ur_id":"2","pt_name":"mobile","pt_detail":"samsung mobile ","pt_size":"0","pt_weight":"150","pt_start_loc":"nashik"}]}
06-04 19:26:06.284 7129-7129/com.carryapp W/System.err: org.json.JSONException: No value for result

这里出了什么问题?请帮忙。谢谢..

2 个答案:

答案 0 :(得分:0)

在您的ServerResponse {"result":1}中,&#34;结果&#34;是int,并且您使用String result = response.getString("result"),您应该使用getInt

答案 1 :(得分:0)

  

{"result":1, “消息”: “成功”, “成本”:[{ “pt_id”: “5”, “ur_id”: “2”, “pt_name”: “移动”,“pt_detail “:“三星   移动“,”pt_size“:”0“,”pt_weight“:”150“,”pt_start_loc“:”nashik“}]}

表单附加的response,似乎result的值为int,您尝试使用以下值获取此值:

String result = response.getString("result"); // WRONG

尝试使用:

int result = response.getInt("result");