Search code examples
androidbroadcastreceiverpush-notificationrest-client

Can a broadcast receiver class make a rest client call?


I have a broadcast receiver class for receiving the push notification which will be carrying an ID/key for a particular item. Then I need to make a call using rest service to retrieve that item and then display it. But neither it gives any error or process the steps. Once this is done then I need to open the particular activity corresponding to the particular item retrieved.

private void retrieveItem(String key) {   
    String feedURI = mContext.getString(R.string.feed_uri_for_push)+"/";
    feedURI += key;     
    RestClient.connect(feedURI, 10, new HTTPData());
}

private class HTTPData implements RestInterface<InputStream>{

@Override
public void onError(Exception e) {
   //give error
}

@Override
public void receive(InputStream instream) {
     String result = RestClient.convertStreamToString(instream);
     JSONArray jsonArray = null;
     try {
             jsonArray = new JSONObject(result).getJSONArray(result); //using array for now.               
    } catch(JSONException e) {
        e.printStackTrace();
    }

    if(jsonArray == null) {
      return;
    }

    // Create Item based on content
    for (int i = 0; i < jsonArray.length(); i++) {
    try {
        if(i > 1){
        break;
    }

            JSONObject preItem = jsonArray.getJSONObject(i);
            ConvertItem item = new NewsItem(rawItem);
            newItem =item;
            } catch (JSONException e) {
              e.printStackTrace();
        }   
     }      
     }
 }

Solution

  • Solved this by passing the key using intent to another class and making the rest call from there instead.