gpt4 book ai didi

java - Android (PhoneGap) - 从推送通知中按钮的点击事件发送 HTTP 请求

转载 作者:可可西里 更新时间:2023-11-01 16:40:08 25 4
gpt4 key购买 nike

我在我的 Android PhoneGap 应用推送通知窗口中实现了一个操作按钮,它向我的服务器执行 HTTP 请求以处理服务器操作。

可能我们在执行HTTP请求的时候出现了异常,因为它好像不起作用。 (目前,我们无法查看异常本身,原因我不会在这里深入)

我们通过在不同的线程中调用 HTTP 请求来实现,因为我们在 Stackoverflow 中看到了无法在主线程中调用 HTTP 请求的答案。

我们将不胜感激。

这是我的 BroadcastReceiver 类:

public class ExpressPayRequest extends BroadcastReceiver{

@Override
public void onReceive(Context Context, Intent arg1) {
Context.startService(new Intent(Context, PostRequestService.class));
}
}

服务看起来像这样:

public class PostRequestService extends Service{

@Override
public void onCreate() {
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

try{

URL url = new URL("http://someGETRequest");
HttpURLConnection client = (HttpURLConnection)url.openConnection();

int responseCode = client.getResponseCode();

}
catch(IOException e){
Log.e("LOGEntry", "error log: " + e.getMessage(),e);
}

return START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}

最佳答案

我自己想通了
如果有人感兴趣,解决方案是创建一个新线程而不是启动一个新服务

这是我使用的代码:

Thread thread = new Thread(){
public void run(){
try{
URL url = new URL("http://test");
HttpURLConnection client = (HttpURLConnection)url.openConnection();
client.connect();
int responseCode = client.getResponseCode();

} catch (IOException e) {
e.printStackTrace();

}

}
};

thread.start();

关于java - Android (PhoneGap) - 从推送通知中按钮的点击事件发送 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51479654/

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