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

Android (PhoneGap) - Send HTTP request from a button's click event in Push Notification

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

可能是我们执行HTTP请求时出现异常,因为它似乎不起作用。 (目前,我们无法查看异常本身,因为我不会在这里进入)

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

任何帮助将不胜感激。

这是我的 BroadcastReceiver class:

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();