2016-06-20 7 views
0

私のアンドロイドアプリのURLからデータを取得しようとしています。私はGet/Postリクエストにvolleyライブラリを使用しています。以下は、URLのコンテンツを取得するために私のコードです:JSONの解析中にJSONexceptionをスローすることができません

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操作がないため、このエラーは発生しません。

org.json.JSONexceptionjava.lang.Exceptionのいずれかを変更するか、try-catchブロックをすべて削除してください。

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

そうですね。投稿後に書式を見た。 :D – Rohit5k2

1

あなたがJsonObjectRequestを使用しているので、あなたが手動でJSONObjectに文字列を変換しますStringRequestで一般的に見ることのtry-catchする必要はありません。

JSON-解析エラーがJsonObjectRequestである場合は、onErrorResponse

関連する問題