2014-11-05 44 views
0

我的Android應用程序中有一個WakefulBroadcastReceiver,它已成功接收到設備的傳入SMS。在此之後,我試圖做的是使用Google Volley庫向我的Web服務器提交請求。問題在於,Volley是異步完成這個Web請求的。因此,BroadcastReceiver中的「onReceive」方法代碼在異步Web請求完成之前完成。我明白,當使用BroadcastReceiver與異步請求時,這是一個問題,但我通過服務(而不是直接在BroadcastReceiver中)發出請求,所以我不明白爲什麼會發生這種情況。如何在Android中執行來自BroadcastReceiver的異步Web請求

我收到以下NullPointerException異常,這我假設是因爲廣播接收器的情況下完成的請求之前已經被銷燬:


11-04 19:13:43.465 32385-32441/com.niiche.pinch E/Volley﹕ [5542] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException 
java.lang.NullPointerException 
     at libcore.net.UriCodec.encode(UriCodec.java:132) 
     at java.net.URLEncoder.encode(URLEncoder.java:57) 
     at com.android.volley.Request.encodeParameters(Request.java:456) 
     at com.android.volley.Request.getBody(Request.java:442) 
     at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:236) 
     at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210) 
     at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106) 
     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96) 
     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110) 

下面是代碼WakefulBroadcastReceiver(SmsReceiver):

public void onReceive(Context context, Intent intent) { 
    //retrieve the SMS PDUs, from the intent extras 
    Bundle pdusBundle = intent.getExtras(); 
    Object[] pdus = (Object[])pdusBundle.get("pdus"); 

    //create sms message object from the PDU 
    SmsMessage message = SmsMessage.createFromPdu((byte[])pdus[0]); 

    Intent service = new Intent(context, SmsIntentService.class); 
    service.putExtra(SmsIntentService.EXTRA_SMS_MESSAGE, message.getMessageBody()); 

    //start the service, keeping the device awake while it is launching 
    startWakefulService(context, service); 
    setResultCode(Activity.RESULT_OK); 
}//End method 

下面是Int entService(SmsIntentService):

protected void onHandleIntent(Intent intent) { 
    if (intent != null) { 
     Bundle extras = intent.getExtras(); 

     if(!extras.isEmpty()){ // has the effect of unparcelling the bundle 
      String smsMessage = extras.getString(EXTRA_SMS_MESSAGE); 

      submitVolleyServerRequestAsync(smsMessage); 
     }//End if 
    }//End if 

    // Release the wake lock provided by the WakefulBroadcastReceiver 
    SmsReceiver.completeWakefulIntent(intent); 
}//End method 

任何可以在這裏提供的援助非常感謝。提前致謝!

+3

我不知道Volley,但是你需要從'IntentService'提供的後臺線程同步執行你的HTTP調用**。否則,服務將會完成,你的'WakeLock'將在你的HTTP調用完成之前被釋放。 – CommonsWare 2014-11-05 00:30:38

回答

0

感謝指針@Aun和@CommonsWare。我的問題實際上是我如何創建排球請求。 Volley顯然不處理輸入參數值(即HTTP Get/Post Params),它被作爲null傳遞。因此,我必須檢查每個參數是否爲null,然後將其添加到請求的參數映射中:

Map<String, String> mapPostArgs = new HashMap<String, String>(); 
if(name != null) { 
     mapPostArgs.put("name", name); 
    } 

GsonRequest<APIResponse<Integer>> request = new GsonRequest<APIResponse<Integer>>(
      uri, 
      new TypeToken<APIResponse<Integer>>(){}.getType(), 
      getRequestHeaders(), 
      mapPostArgs, 
      listener, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        handleError(error); 
       }//End method 
      }); 

非常感謝您的指導!

0

因爲您在Intent服務中執行異步請求,SmsReceiver.completeWakefulIntent(intent);在它完成之前被調用。

因此,您的webservice請求與排除的RequestFuture類同步。

RequestFuture<JSONObject> future = RequestFuture.newFuture(); 
JsonObjectRequest request = new JsonObjectRequest(URL, null, future, future); 
requestQueue.add(request); 

try { 
    JSONObject response = future.get(); // this will block 
} catch (InterruptedException e) { 
    // exception handling 
} catch (ExecutionException e) { 
    // exception handling 
} 
+0

謝謝恩。我嘗試過,但仍然得到了以前的NULLREFERENCEEXCEPTION,以及=> java.util.concurrent.ExecutionException:com.android.volley.VolleyError:java.lang.NullPointerException – CP17 2014-11-05 22:33:23

+0

你缺少http://在你的url傳遞給凌空? – Aun 2014-11-06 18:02:02

相關問題