【发布时间】:2023-03-04 03:31:01
【问题描述】:
我在我的 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;
}
}
【问题讨论】:
标签: java android http phonegap