【问题标题】:Android (PhoneGap) - Send HTTP request from a button's click event in Push NotificationAndroid (PhoneGap) - 从推送通知中按钮的单击事件发送 HTTP 请求
【发布时间】: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


    【解决方案1】:

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

    这是我使用的代码:

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

    【讨论】:

      猜你喜欢
      • 2017-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多