2016-04-29 19 views
0

私はネットワーク要求をするために私のデバイスを起動しようとしています。私が1〜2分にアラームを設定したとき。それは完全に動作します。しかし、私は約10分に設定し、私のディスプレイをオフにすると、アラームが発生しますが、ボレーはタイトルに示されているように "NoConnectionError"をスローします。volley NoConnectionError:java.net.UnknownHostException

私はそれについて研究しました、私はWakeFullBroadCastReceiverを使用して受信機を実装しました。その後、私は同じ問題を抱えていました。私はさらに調査し、私のWiFiにロックを追加しました。それでも、私は同じエラーが発生しています。

ご了承ください。

受信機:

@Override 
public void onReceive (Context context, Intent intent) { 
    Log.i("Receiver", "Called here"); 
    Intent serIntent1 = new Intent(context, FetchTodayWordService.class); 
    startWakefulService(context, serIntent1); 
} 

サービスアラームを設定する

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.i("SERVICE", "started"); 
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
    lock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "LockTag"); 
    lock.acquire(); 
    MakeRequest(intent); 
    return START_STICKY; 
} 

@Override 
protected void onHandleIntent (Intent intent) { 
    Log.i("OUTPUT", "Service called"); 
    MakeRequest(intent); 
} 
public void MakeRequest(final Intent intent) 
{ 
    if (intent == null) return; 
    JsonObjectRequest jsonObjectRequest = 
      new JsonObjectRequest 
        (Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 
         @Override 
         public void onResponse (JSONObject response) { 
          lock.release(); 
          wordDay = ParseWordDay.ParseJSON(response); 
          if(wordDay != null) 
          { 
           new StoreData().DoWork(getApplicationContext(), wordDay, intent); 

          } 

         } 
        }, 
          new Response.ErrorListener() { 
           @Override 
           public void onErrorResponse (VolleyError error) { 
            TodayWordReceiver.completeWakefulIntent(intent); 
            lock.release(); 
            Log.i("OUTPUT", String.valueOf(error)); 
           } 
          }); 

    MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest); 
} 

private void SetAlarm() { 

    int alarmType = AlarmManager.ELAPSED_REALTIME; 
    final int TIME = 1000 * 60 * 5; 
    Intent intent = new Intent(this, TodayWordReceiver.class); 

    /*boolean alarmUp = (PendingIntent.getBroadcast(getApplicationContext(), 
      1, intent, PendingIntent.FLAG_NO_CREATE) != null); 
    if (!alarmUp) 
    {*/ 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     alarmManager.setRepeating(alarmType, SystemClock.elapsedRealtime() + TIME, 
       TIME, pendingIntent); 
     Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show(); 
    /*}else Toast.makeText(MainActivity.this, "ALarm already set", Toast.LENGTH_SHORT).show();*/ 
} 

エラー出力:

04-29 23:48:23.125 15702-15702/com.example.clinton.light I/Receiver: Called here 04-29 23:48:23.145 15702-15702/com.example.clinton.light I/SERVICE: started 04-29 23:48:23.175 15702-15702/com.example.clinton.light I/OUTPUT: com.android.volley.NoConnectionError: java.net.UnknownHostException: Unable to resolve host "api.wordnik.com": No address associated with hostname

本当にありがとうございます。私はあなたの答えをマークすることができないかもしれない、私は十分なポイントがありません。

+1

端末のWi-Fiアドバンス設定を確認し、「スリープ中にWi-Fiをオンにする」が「常に」に設定されているかどうかを確認してください – freddieptf

+0

「常に」に設定されています。 –

答えて

0

UnknowHostExceptionは、いずれかのプロキシがダウンしているとき、ネットワーク接続がダウンしているとき、またはDNSの問題のときの非常に一般的なエラーです。プロセスが実行中であることをセッションで確認し、セッションタイムアウトの問題が発生する可能性があることを確認します。

+0

こんにちは、プロセスが実行されているセッションを確認するにはどうすればよいですか?あなたは少し精緻化できますか?これらのことでまだアマチュア... –

+0

私はブラウザにURLを置き、約20回リフレッシュしましたが、どれも私にネットワークの問題を与えませんでした –

+0

申し訳ありませんが私はURLを提供することができません、あなたはAPIキーが必要になりますそれを働かせる。秘密です:) –

1

画面がオンのとき(デバイスが起きているとき)にのみネットワーク操作が成功したことに気付きました。だから私は

int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP; の代わりにアラームタイプを置き換え:

int alarmType = AlarmManager.ELAPSED_REALTIME; 

そして、それはそれを修正しました。 wifiにロックをかけたり、スリープしているスレッドを使用する必要はありません。

今後の参考のため、完全なAlarmCodeを以下に示します。質問に表示されているWi-Fiロックを使用しないでください。

private void SetAlarm() { 

    int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP; 
    final int TIME = 1000 * 60 * 5; 
    Intent intent = new Intent(this, TodayWordReceiver.class); 

    /*boolean alarmUp = (PendingIntent.getBroadcast(getApplicationContext(), 
      1, intent, PendingIntent.FLAG_NO_CREATE) != null); 
    if (!alarmUp) 
    {*/ 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     alarmManager.setRepeating(alarmType, SystemClock.elapsedRealtime() + TIME, 
       TIME, pendingIntent); 
     Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show(); 
    /*}else Toast.makeText(MainActivity.this, "ALarm already set", Toast.LENGTH_SHORT).show();*/ 
} 

それだけです!

関連する問題