2017-07-25 8 views
0

5秒ごとにjsonデータをwebservicesからリクエストしたいとします。私はそれをユーザーに見せたくないので、私はこのサービスをサービスでやっています。コードにエラーはありませんが、期待どおりログに出力が表示されません。ここでAndroid:サービスから5秒ごとにhttpリクエストを送信する方法

コードは次のとおりです。makeServiceCallため

protected void onHandleIntent(Intent intent) { 

    final String driverId = intent.getStringExtra("DriverId"); 

    new Handler().postDelayed(new Runnable() { 
     public void run() { 
      HttpHandler sh = new HttpHandler(); 

      // making request to url and getting respose 
      String jsonStr = sh.makeServiceCall(url + "?cno=" + driverId + "&lat=0&lon=79"); 

      Log.e("ServicesClass", "Response from url: " + jsonStr); 

      if (jsonStr != null) { 
       String temp = jsonStr; 
       String finals = temp.replace("<string xmlns=\"http://tempuri.org/\">", ""); 
       Log.e(TAG, "Response from url at Service Class: " + jsonStr); 

      } else { 
       Log.e(TAG, "Response from url at Service Class: " + jsonStr); 
      } 
     } 
    }, 5000); 
} 

コード:

public String makeServiceCall(String reqUrl){ 

     String response = null; 

     try { 

      URL url = new URL(reqUrl); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setRequestMethod("GET"); 

      //read the response 
      InputStream in = new BufferedInputStream(conn.getInputStream()); 
      response = convertStreamToString(in); 
     }catch (MalformedURLException e){ 
      Log.e(TAG, "MalformedException: " +e.getMessage()); 
     }catch (ProtocolException e){ 
      Log.e(TAG, "Protocal Exception: " +e.getMessage()); 
     }catch (IOException e){ 
      Log.e(TAG, "IOException: " +e.getMessage()); 
     }catch (Exception e){ 
      Log.e(TAG, "Exception: " +e.getMessage()); 
     } 
     return response; 
    } 

    private String convertStreamToString(InputStream is){ 

     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line; 
     try { 
      while ((line = reader.readLine()) != null){ 

       sb.append(line).append('\n'); 
      } 
     }catch (IOException e){ 
      e.printStackTrace(); 
     }finally { 
      try { 
       is.close(); 
      }catch (IOException e){ 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 

誰もが誤りである可能コードから把握するのに役立つことはできますか?

+1

あなたはそのための意思サービスを利用することができますし、私が編集したポストをチェックし、intentServiceを使用しますが、同じ問題が存在すること – ManishNegi

+0

ためasyncTaskを必要とされることはありません。 –

+0

問題点 – ManishNegi

答えて

0

アラームマネージャを使用すると繰り返しアラームが設定されます。

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    Intent myIntent = new Intent(this, RepeatingService.class); 
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 5000, 5000, pendingIntent); 

ジョブが終了すると自動的に停止するため、RepeatingServiceはIntentServiceを使用します。

class RepeatingService extends IntentService{ 

override fun onHandleIntent(intent: Intent?) { 
    // do your stuff like async or anyother task. 
} 

} 
+0

編集した投稿を確認してください、intentServiceを使用しましたが、同じ問題があります。 –

関連する問題