2016-09-11 5 views
0

私はアンドロイドにボレーでURLに接続しています:org.json.JSONException:値... - ボレーですか?

private void connectToUrl(String url) { 
    JsonArrayRequest req = new JsonArrayRequest(url, 
      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 
        JSONObject jsonObj = null; 
        try { 
         jsonObj = new JSONObject(response.toString()); 
         JSONArray contacts = jsonObj.getJSONArray("d"); 
         for (int i = 0; i < contacts.length(); i++) { 
          try { 
           JSONObject object = response.getJSONObject(i); 
           Log.i("VALUEDSSDDD", object.getString("urlpic")); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      VolleyLog.d("Custom Log Error", "Error: " + error.getMessage()); 
      Log.i("ERRRROOORR",error.getMessage()+""); 
     } 
    }); 

    // Adding request to request queue 
    AppController.getInstance().addToRequestQueue(req, tag_json_arry); 
    req.setRetryPolicy(new DefaultRetryPolicy(
      30000, 
      DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
      DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
} 

をしかし、私はurlに接続したときに私にエラーを取得:

{ 
    "d": [{ 
     "apiCurrentUserID": 0, 
     "apiCurrentUserName": null, 
     "urlpic": "http://Image.jpg", 
     "Title": "Jim", 
     "FileMusicUrl": "http://video.mp4", 
     "StringDuration": "05':07''", 
     "ViewCount": 57, 
     "DownloadCount": 56, 
     "uploadDate": "/Date(1473550560)/", 
     "MusicID": 192727, 
     "UserID": 0, 
     "Summary": "Some text", 
     "OrderView": null, 
     "ShowDate": "/Date(1473550500)/", 
     "FileSize": "19.15 MB", 
     "RatePlus": null, 
     "RateDash": null, 
     "ChannelID": 0, 
     "ChannelName": null, 
     "CurrentChannelID": 0, 
     "CurrentChannelName": null, 
     "isFollowed": false, 
     "profileImgUserSend": null, 
     "isDisliked": false, 
     "isLiked": false, 
     "isLatered": false, 
     "isFavorited": false, 
     "apiCategories": null, 
     "apiTags": [{ 
      "tageID": "438232", 
      "titleTag": "book" 
     }, { 
      "tageID": "411557", 
      "titleTag": "sounds" 
     }, { 
      "tageID": "365984", 
      "titleTag": "map" 
     }] 
    }], 
    "RowwCount": 0 
} 

I:ここで

org.json.JSONException: Value ..... 

は私jsonですjsonよりurlになっています。

EDIT:

私は私のコードを編集します。

private void connectToUrl(String url) { 
    requestQueue = Volley.newRequestQueue(this); 
    JsonObjectRequest obreq = new JsonObjectRequest(Request.Method.DEPRECATED_GET_OR_POST, url, null, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        JSONObject jsonObj = null; 
        try { 
         jsonObj = new JSONObject(response.toString()); 
         JSONObject contacts = jsonObj.getJSONObject("d"); 
         try { 
          Log.i("VALUEDSSDDD", contacts.getString("urlpic")); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Log.e("Volley", "Error " + error.getMessage()); 
       } 
      } 
    ); 
    requestQueue.add(obreq); 
} 

しかし、私はエラーを取得:最後のコードで

null 

は私が結果を示したが、このコードドンで結果はありません。

+0

ポストの完全なエラーログ –

答えて

1

貼り付けたレスポンスは、JSONObjectではないJSONArrayです。あなたはJSON配列であるdの値を取得する必要があります。

+0

私はJsonObjectRequestから使用されるが、私はどんな結果を得ることはありません、ヌルをエラーが出ます。 –

+0

@ android_dev。どうもありがとう。 –

+0

@JoJoRoidハッピーに助けてください:) –

0

私の問題が解決:

private void connectToUrl(String url) { 
    JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,url,null, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 

        JSONObject jsonObj = null; 
        try { 
         jsonObj = new JSONObject(response.toString()); 
         JSONArray contacts = jsonObj.getJSONArray("d"); 
         for (int i = 0; i < contacts.length(); i++) { 
          try { 
           JSONObject object = contacts.getJSONObject(i); 

          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      VolleyLog.d("Custom Log Error", "Error: " + error.getMessage()); 
      Log.i("ERRRROOORR",error.getMessage()+""); 
     } 
    }); 

    // Adding request to request queue 
    AppController.getInstance().addToRequestQueue(req, tag_json_arry); 
    req.setRetryPolicy(new DefaultRetryPolicy(
      30000, 
      DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
      DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
} 
関連する問題