2015-11-29 113 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