gpt4 book ai didi

java - Volley NoConnectionError : java.net.UnknownHostException

转载 作者:行者123 更新时间:2023-12-01 09:59:18 27 4
gpt4 key购买 nike

我正在尝试唤醒我的设备以发出网络请求。当我将闹钟设置为一两分钟时。它工作完美。但是当我将其设置为大约 10 分钟并关闭显示器时,警报就会响起,但 volley 会抛出“NoConnectionError”,如标题所示。

我对此进行了研究,我已经使用 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

任何帮助将不胜感激。我可能无法标记您的答案,我没有足够的分数。

最佳答案

我意识到只有当屏幕打开时(当设备唤醒时)网络操作才成功。所以我将警报类型替换为

int AlarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;而不是:

int alarmType = AlarmManager.ELAPSED_REALTIME;

这解决了它。无需对 wifi 进行任何锁定或使用任何 hibernate 线程。

完整的 AlarmCode 如下所示,以供将来引用:请不要使用问题中显示的 WiFi 锁。

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();*/
}

就是这样!

关于java - Volley NoConnectionError : java.net.UnknownHostException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36945115/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com