【问题标题】:Try is working properly but catch is given errorTry 工作正常,但 catch 出现错误
【发布时间】:2015-11-06 13:03:57
【问题描述】:

嘿,伙计们,我在一个应用程序中工作,当我发送 OTP 以验证有错误时,我正在通过 OTP 验证我的手机号码,移动是验证但 catch 块给出错误,Activity 不是转到下一个 Acivity 而不是日志显示

 private void verifyOtp(final String otp){
    final StringRequest strReq = new StringRequest(Request.Method.POST,
            config.Config.URL_VERIFY_OTP, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d(TAG, response.toString());
            try {
                JSONObject responseObj = new JSONObject(response);
                // Parsing json object response
                // response will be a json object
                boolean status =responseObj.getBoolean("status");
                if (status==true) {
                    // parsing the user profile information
                    JSONObject profileObj = responseObj.getJSONObject(response);
                    String mobile = profileObj.getString("mobile");
                    PrefManager pref = new PrefManager(getApplicationContext());
                    pref.createLogin(mobile);
                    Intent intent = new Intent(HttpService.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    Toast.makeText(getApplicationContext(), "HTTPIF"+status, Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "HTTPELSE"+status, Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {  //------this is working and give toast----//
                System.out.print("jsonError :=>"+e);
                Toast.makeText(getApplicationContext(),
                        "Error is WWW: " + e.getMessage(),
                        Toast.LENGTH_LONG).show();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "HTTPError: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }) {
        MyApplication userinfo = (MyApplication)getApplicationContext();
        final  String user = userinfo.getuser();      // Global Variable get Value uID
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("akey","xxxxxxxxxx");
            params.put("mobileverify", otp);
            params.put("uid",user);
            Log.e(TAG, "Posting params: " + params.toString());
            return params;
        }
    };
    MyApplication.getInstance().addToRequestQueue(strReq);
}

【问题讨论】:

    标签: android try-catch android-volley


    【解决方案1】:
    String mobile = profileObj.getString("mobile");
    

    你可能没有得到任何字符串,而是一个空值。

    【讨论】:

      【解决方案2】:

      您将收到JSONException。这可能是由多种原因造成的,您可以在这里找到它们:http://developer.android.com/reference/org/json/JSONException.html

      原因是,您使用的是StringRequest。这意味着,response 是一个普通的 String 而不是 JSON 响应。只需打印出reponse 并亲自查看它不是有效的JSON。您需要改用JSONRequest,或按原样处理String 响应,而不是json 解析。

      更新: 您的问题可能是由于以下两行之一,因为它们都可以抛出 JSONException: getJSONObject() , getString()

      JSONObject profileObj = responseObj.getJSONObject(response);
      String mobile = profileObj.getString("mobile");
      

      您说您的 JSON 响应是 {"msg":"verified","status":true}。那么您如何尝试获取 profileObjmobile 的值?如果找不到该键的值,它将抛出JSONException。为避免这种情况,您需要检查键的值是否存在。在获取密钥之前,使用has(String key) 确认密钥的映射是否存在。

      【讨论】:

      • 这是我的回复 json {"msg":"verified","status":true} 它是有效的,但它给出了错误
      • 发布JSONExceptionSystem.out.print("jsonError :=&gt;"+e);
      • 这是错误Error Is...org.json.JSONException: No value for {"msg":"verified","status":true}
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      相关资源
      最近更新 更多