2015-11-29 163 views
1

我在製作多個Volley StringRequests。Android Volley請求不響應

requestfriendlist方法提取與「Naruto」相關的配置文件列表。該方法爲每個配置文件獲取上一個方法結果中獲取的圖像。但是,我得到的響應(imagelink)並不是按順序排列的,因爲方法並不正確。

對於實施例 -

請求[1,2,3,4]

響應[2,1,4,3]或任何其他順序。

請幫我解決這個問題。

附加代碼片段

protected void onCreate (Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    listView = (ListView) findViewById(R.id.list); 
    adapter = new CustomImageAdapter(this, imageList); 
    listView.setAdapter(adapter); 

    requestfriendlist("Naruto"); 
} 
private void requestfriendlist (String profilename) 
{ 
    String uri = String.format(Config.URL_REQUEST_FRIENDS + "?current_user=%1$s", profilename); 
    Log.d(TAG + "uri", uri); 
    StringRequest strReq = new StringRequest(
      Request.Method.GET, 
      uri, 
      new Response.Listener<String>() 
      { 
       @Override 
       public void onResponse (String response) 
       { 
        try 
        { 
         JSONObject responseObj = new JSONObject(response); 
         Log.d(TAG + "friends", response); 
         JSONArray jResult = responseObj.getJSONArray("req_users"); 
         Toast.makeText(MainActivity.this, jResult.toString(), Toast.LENGTH_SHORT).show(); 
         for(int i = 0; i < jResult.length(); i++) 
         { 
          JSONObject jresponse = jResult.getJSONObject(i); 
          String profile = jresponse.getString("userid"); 
          friendlist.add(profile); 
         } 
         for(int i = 0; i < friendlist.size(); i++) 
          requestimagelink(friendlist.get(i)); 
        } 
        catch(JSONException e) 
        { 
         Log.d(TAG, "error" + e.getMessage()); 
         Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
        } 
       } 
      }, 
      new Response.ErrorListener() 
      { 
       @Override 
       public void onErrorResponse (VolleyError error) 
       { 
        Log.e(TAG, "Error: " + error.getMessage()); 
        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show(); 
       } 
      } 
    ); 
    MyApplication.getInstance().addToRequestQueue(strReq); 
} 

private void requestimagelink (final String profilename) 
{ 
    String uri = String.format(Config.URL_REQUEST_IMAGE + "?userid=%1$s", profilename); 
    Toast.makeText(MainActivity.this, uri, Toast.LENGTH_SHORT).show(); 
    Log.d(TAG, "uri" + uri); 
    StringRequest strReq = new StringRequest(
      Request.Method.GET, 
      uri, 
      new Response.Listener<String>() 
      { 
       //response from the server 
       @Override 
       public void onResponse (String response) 
       { 
        try 
        { 
         JSONObject responseObj = new JSONObject(response); 
         ((TextView) findViewById(R.id.tvprofilejson)).setText(response); 
         Log.d(TAG, response); 
         JSONArray jResult = responseObj.getJSONArray("photos"); 
         for(int i = 0; i < jResult.length(); i++) 
         { 
          JSONObject jresponse = jResult.getJSONObject(i); 
          ImageClass img = new ImageClass(); 
          img.setThumbnailUrl(jresponse.getString("name")); 
          img.setTitle(profilename); 
          imageList.add(img); 
          //imagelinks.add(jresponse.getString("name")); 
          adapter.notifyDataSetChanged(); 
         } 
        } 
        catch(JSONException e) 
        { 
         Log.d(TAG, "error" + e.getMessage()); 
         Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
        } 
       } 
      }, 
      new Response.ErrorListener() 
      { 
       @Override 
       public void onErrorResponse (VolleyError error) 
       { 
        Log.e(TAG, "Error: " + error.getMessage()); 
        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show(); 
       } 
      } 
    ); 
    // Adding request to request queue 
    MyApplication.getInstance().addToRequestQueue(strReq); 
} 

回答

6

這裏是如何使用一個HashMap您的配置文件鏈接到自己的形象:

ArrayList<String> friendlist = new ArrayList<String>(); 

/** 
    Keeps the profiles' images. Later in your arrayAdapter, where you want show thumbnail image, you must 
    get corresponding ImageClass from of profile with profileName. If the result is null means that the image 
    is not loaded yet, otherwise you can use the ImageClass object to retreive profile's image. 
*/ 
HashMap<String, ImageClass> profile_img_Hash = new HashMap<String, ImageClass>(); 

protected void onCreate (Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    listView = (ListView) findViewById(R.id.list); 
    adapter = new CustomImageAdapter(this, imageList); 
    listView.setAdapter(adapter); 

    requestfriendlist("Naruto"); 
} 
private void requestfriendlist (String profilename) 
{ 
    String uri = String.format(Config.URL_REQUEST_FRIENDS + "?current_user=%1$s", profilename); 
    Log.d(TAG + "uri", uri); 
    StringRequest strReq = new StringRequest(
      Request.Method.GET, 
      uri, 
      new Response.Listener<String>() 
      { 
       @Override 
       public void onResponse (String response) 
       { 
        try 
        { 
         JSONObject responseObj = new JSONObject(response); 
         Log.d(TAG + "friends", response); 
         JSONArray jResult = responseObj.getJSONArray("req_users"); 
         Toast.makeText(MainActivity.this, jResult.toString(), Toast.LENGTH_SHORT).show(); 
         for(int i = 0; i < jResult.length(); i++) 
         { 
          JSONObject jresponse = jResult.getJSONObject(i); 
          String profile = jresponse.getString("userid"); 
          friendlist.add(profile); 
         } 
         for(int i = 0; i < friendlist.size(); i++) 
          requestimagelink(friendlist.get(i)); 

        } 
        catch(JSONException e) 
        { 
         Log.d(TAG, "error" + e.getMessage()); 
         Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
        } 
       } 
      }, 
      new Response.ErrorListener() 
      { 
       @Override 
       public void onErrorResponse (VolleyError error) 
       { 
        Log.e(TAG, "Error: " + error.getMessage()); 
        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show(); 
       } 
      } 
    ); 
    MyApplication.getInstance().addToRequestQueue(strReq); 
} 

private void requestimagelink (final String profilename) 
{ 

    //This snippet will prevent re-downloading 
    if(profile_img_Hash.get(profilename) != null) { 

     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       adapter.notifyDataSetChanged(); //Optional. Not neccessary..! 
      } 
     }); 
     return; //Because this profile's image is loaded. 
    } 


    String uri = String.format(Config.URL_REQUEST_IMAGE + "?userid=%1$s", profilename); 
    Toast.makeText(MainActivity.this, uri, Toast.LENGTH_SHORT).show(); 
    Log.d(TAG, "uri" + uri); 
    StringRequest strReq = new StringRequest(
      Request.Method.GET, 
      uri, 
      new Response.Listener<String>() 
      { 
       //response from the server 
       @Override 
       public void onResponse (String response) 
       { 
        try 
        { 
         JSONObject responseObj = new JSONObject(response); 
         ((TextView) findViewById(R.id.tvprofilejson)).setText(response); 
         Log.d(TAG, response); 
         JSONArray jResult = responseObj.getJSONArray("photos"); 
         for(int i = 0; i < jResult.length(); i++) 
         { 
          JSONObject jresponse = jResult.getJSONObject(i); 
          ImageClass img = new ImageClass(); 
          img.setThumbnailUrl(jresponse.getString("name")); 
          img.setTitle(profilename); 
          imageList.add(img); 
          //imagelinks.add(jresponse.getString("name")); 

          //Before notifying the adapter we have to put the img into our hash map. 
          profile_img_Hash.put(profilename, img); 

          //Remember, in you getView(..) method of your adapter, you have to get image from 
          // profile_img_Hash by profileName as key. If the returned result was null do nothing 
          // If the returned value was not null you can use the ImageClass to provide profile's 
          // image. :) 
          adapter.notifyDataSetChanged(); 
         } 
        } 
        catch(JSONException e) 
        { 
         Log.d(TAG, "error" + e.getMessage()); 
         Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
        } 


       } 
      }, 
      new Response.ErrorListener() 
      { 
       @Override 
       public void onErrorResponse (VolleyError error) 
       { 
        Log.e(TAG, "Error: " + error.getMessage()); 
        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show(); 
       } 

      } 
    ); 
    // Adding request to request queue 
    MyApplication.getInstance().addToRequestQueue(strReq); 
} 

請記住,在你arrayAdapter的getView(...)方法你必須從profile_img_Hash加載配置文件的圖像,配置文件名稱如下所示:

ImageClass img = profile_img_Hash.get(profilename); 
if(img != null){ 
    //row's imageView.setBitmap(img.getBitmap()); 
} 

希望這有助於。

+0

感謝您的回答。但這不是我所期待的。我需要一種方法來保持速度,但也要保持順序。你能否解釋如何將圖像鏈接到好友列表中的項目並維護訂單? (通過使用地圖????)預先感謝。 –

+0

我編輯了答案中的代碼。也許這不是整個解決方案,但它可以給你一些關於如何將你的個人資料與他們的圖像鏈接的想法。 – Afshin