2016-06-20 4 views
0

내 안드로이드 앱의 URL에서 데이터를 가져 오려고합니다. Get/Post 요청에 발리 라이브러리를 사용하고 있습니다. 내 코드는 다음은 URL의 컨텐츠를 얻을 수 있습니다 :JSON 구문 분석 중에 JSONexception을 throw 할 수 없습니다.

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( Request.Method.GET, R.string.login, null, 
        new Response.Listener<JSONObject>() { 
         @Override 
         public void onResponse(JSONObject response) { 
          try { 
            Intent intent = new Intent(LoginActivity.this, HomeActivity.class); 
            startActivity(intent); 
            finish(); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
           Log.e("ex","Exception Caught! "+e); 
          } 
         } 
        }, 
        new Response.ErrorListener() { 
         @Override 
         public void onErrorResponse(VolleyError error) { 

         } 
        }); 
      RequestQueue requestQueue = Volley.newRequestQueue(this); 
      requestQueue.add(jsonObjectRequest); 

하지만 JSONException에 오류가 다음 무엇입니까 :

예외 org.json.JSONexception이 try 블록 대응에 던져되지 않습니다

누군가 나를 도울 수 있습니까?

감사
판 카지

답변

3

org.json.JSONexception은 JSON 작업에 발생합니다. try 블록에 JSON 연산이 없으므로이 오류가 발생하지 않습니다.

에서 java.lang.Exception으로 변경하거나 try-catch 블록을 모두 제거하십시오. 당신이 JsonObjectRequest을 사용하기 때문에

try { 
    Intent intent = new Intent(LoginActivity.this, HomeActivity.class); 
    startActivity(intent); 
    finish(); 
} catch (Exception e) { 
    e.printStackTrace(); 
    Log.e("ex","Exception Caught! "+e); 
} 
+0

수정이 아니라 좀 더 나은 답을 포맷하는 것이 처리합니다. – basic

+0

그래, 내가 그랬어. 게시 한 후 내 서식을 보았습니다. :디 – Rohit5k2

1

, 당신은 수동으로 된 JSONObject로 문자열을 변환 할 위치를 일반적으로 StringRequest에서 볼 것 시도 - 캐치에 대한 필요가 없습니다.

JsonObjectRequest 오류를 JSON을 구문 분석이있는 경우에, 당신은 onErrorResponse

관련 문제