2017-10-12 59 views
0

我是新來的android和我正在開發一個應用程序,從bitrex獲取當前速率並將其轉換爲印度盧比,但問題是我想刷新每秒鐘的速度,但我的處理程序只運行一次,第一次後,並沒有改變其值,即使在利率苯甲地那銨API爲什麼我的處理程序只運行一次刷新數據

在改變這就是我的活動

public class Test extends AppCompatActivity { 
double last; 
double inr; 
double x; 


TextView tv, tt, ui,uer; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_test); 





    uer=(TextView)findViewById(R.id.usern); 
    ui = (TextView) findViewById(R.id.uid); 
    tv = (TextView) findViewById(R.id.name); 
    tt = (TextView) findViewById(R.id.email); 


    final RequestQueue queue = Volley.newRequestQueue(this); 
    final RequestQueue queuee = Volley.newRequestQueue(this); 

    String url = "https://bittrex.com/api/v1.1/public/getticker?market=usdt-btc"; 
    String urll="http://api.fixer.io/latest?base=USD"; 

    final StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 

        JSONObject json = null; 
        try { 
         json = new JSONObject(response); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
        try { 
         JSONObject result = json.getJSONObject("result"); 

         last = result.getDouble("Last"); 
         tv.setText(String.valueOf(last)); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
    }); 


    final StringRequest stringRequestmoney = new StringRequest(Request.Method.GET, urll, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 


        JSONObject jjson = null; 
        try { 
         jjson = new JSONObject(response); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
        try { 
         JSONObject money = jjson.getJSONObject("rates"); 
         inr = money.getDouble("INR"); 
         tt.setText(String.valueOf(inr)); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
    }); 


//  queue.add(stringRequest); 
//  queuee.add(stringRequestmoney); 
    final Handler handler = new Handler(); 

    handler.postDelayed(new Runnable() { 
          public void run() { 
           //do something 
           queue.add(stringRequest); 
           queuee.add(stringRequestmoney); 
           x = (last * inr);  
           ui.setText(""+x); 
           handler.postDelayed(this, 5000); 

          } 
         }, 5000); 



} 
+0

你怎麼知道處理程序再也沒有調用過? – nhoxbypass

+0

因爲在API中的速率變化,但沒有在我的Android應用程序中改變@GeniusQ –

回答

0

我不知道,如果這是你的問題的根源,但嘗試找出你處理中的任何錯誤

handler.postDelayed(new Runnable() { 
        public void run() { 
           try{ 
            //do something 
            queue.add(stringRequest); 
            queuee.add(stringRequestmoney); 
            x = (last * inr);  
            ui.setText(""+x); 
            handler.postDelayed(this, 5000); 
          } 
         finally { 
         // 100% guarantee that this always happens, even if 
         // your update method throws an exception 
         handler.postDelayed(this, 5000); 
           } 
           } 
          }, 5000); 
+0

無法在代碼中發現任何問題。我在處理程序中寫錯了什麼? @Boukharist –

相關問題