2016-08-05 24 views
0

我想從兩個不同的表在MYSQL中的數據,所以我用「StringRequest」方法從MYSQL檢索數據,在下面的代碼中,我可以從一個表中獲取數據,在Android的ListView中查看它,但我如何更改我的代碼,以便我也從另一個表中獲取數據。如何傳遞兩個字符串中的「StringRequest」在android

這裏是我的代碼:

String url ="http://alwaysready.16mb.com/OnlineJobSort.php;"; 
String url_lock="http://alwaysready.16mb.com/LocalSort.php?"; 

    StringRequest stringRequest = new StringRequest(URL, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      showJSON(response); 
     } 
    }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(Jobs.this, error.getMessage().toString(), Toast.LENGTH_LONG).show(); 
       } 
      }); 

    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 

從上面的代碼我可以能夠得到從「URL」,但我需要從兩個「URL」 &「url_lock」獲得的數據。

回答

1

好吧,很簡單。您只需要使用不同的URL參數製作2個StringRequests

String url = "http://alwaysready.16mb.com/OnlineJobSort.php;"; 
String url_lock = "http://alwaysready.16mb.com/LocalSort.php?"; 

StringRequest stringRequest1 = new StringRequest(url, new Response.Listener<String>() { 
    @Override 
    public void onResponse(String response) { 
     showJSON(response); 
    } 
}, 
     new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Toast.makeText(Jobs.this, error.getMessage().toString(), Toast.LENGTH_LONG).show(); 
      } 
     }); 

StringRequest stringRequest2 = new StringRequest(url_lock, new Response.Listener<String>() { 
    @Override 
    public void onResponse(String response) { 
     showJSON(response); 
    } 
}, 
     new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Toast.makeText(Jobs.this, error.getMessage().toString(), Toast.LENGTH_LONG).show(); 
      } 
     }); 

RequestQueue requestQueue = Volley.newRequestQueue(this); 
requestQueue.add(stringRequest1); 
requestQueue.add(stringRequest2); 
相關問題