2

私はAndroidスタジオにはかなり新しいです。私はVolleyを使ってGet Requestを構築しようとしています。サーバーの応答はJsonObjectです。私はブレークポイントでコードをテストしましたが、なぜ私はonResponseにジャンプしないのか、なぜ動作しないのだろうと思います。Volley Get Request:onResponseは決して呼ばれません

public Account GetJsonObject(String urlExtension, String name, Context context) { 
    String baseURL = "myurl.com/api"; 
    baseURL += urlExtension + "/" + name; 
    // url will look like this: myurl.com/api/user/"username" 

    final Account user = new Account("",""); 
    //Account(name, email) 
    RequestQueue requestQueue; 
    requestQueue = Volley.newRequestQueue(context); 

    JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.GET, baseURL,null, 
      new Response.Listener<JSONObject>() { 

       // Takes the response from the JSON request 
       @Override 
       public void onResponse(JSONObject response) { 
        try { 
         JSONObject obj = response.getJSONObject("userObject"); 

         String username = obj.getString("Username"); 
         String email = obj.getString("Email"); 
         user.setUsername(username); 
         user.setEmail(email); 

        } 
        catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
       } 
      }); 


    requestQueue.add(jsonObject); 
    return user; 
    } 
+1

以下のように

// remove Account return type and use void public void GetJsonObject(String urlExtension, String name, Context context) { .... .... // other stuffs .... JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.GET, baseURL,null, new Response.Listener<JSONObject>() { // Takes the response from the JSON request @Override public void onResponse(JSONObject response) { processResponse(response); // call to method } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("ERROR", "Error occurred ", error); } }); requestQueue.add(jsonObject); } 

は今、別の方法を作成します。そこにブレークポイントで試したことは確かですか? – GVillani82

答えて

1

は@ GVillavani82としてごonErrorResponse()メソッド本体が空のコメント:

はここで入手する方法の私のコードです。この

new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 
     Log.e("ERROR", "Error occurred ", error); 
    } 
} 

のようなエラーがあなたがAndroidManifest.xmlファイルに設定され、以下の権限を持っているとAPIのURLが適切であることを確認してくださいログインするようにしてください。

<uses-permission android:name="android.permission.INTERNET"/> 

およびJsonObjectRequestクラスは、非同期ネットワークコールクラスを返します。コードを以下のように変更します。あなたのエラーコールバックが空である

private void processResponse(JSONObject response) { 
    try { 
     final Account user = new Account("",""); 
     JSONObject obj = response.getJSONObject("userObject"); 

     String username = obj.getString("Username"); 
     String email = obj.getString("Email"); 
     user.setUsername(username); 
     user.setEmail(email); 

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

ああ、ありがとう、助けを借りて@ GVillavani82ありがとう:D –

+0

私の答えがあなたの問題を解決するのに役立ったなら、私の答えは私の答えに左のマークをチェックして受け入れるようにしてください。 – Shashanth

+0

残念ながら、今は試してみましたが、うまくいきません。ErrorListenerでジャンプします。適切なURLを使用しています –

関連する問題