Domanda

I am developing an application. In which i used the push notification functionality which is working good.

Now i want to call a PHP webservice from

onMessage(Context context, Intent data){
}

and parse the response in background in the same function and store the parsed data in SQLite database.

I tried to just call the web service, but getting an error. My code is as follows :

@Override
protected void onMessage(Context context, Intent data) {
    String message = data.getStringExtra("message");
    if(message != null){
        ServiceHandler serviceHandler = new ServiceHandler();
        String jsonStr = serviceHandler.makeHttpRequest("http://hostname:1010/test_mf.php", "POST");
        Log.d("Response",jsonStr);
        //generateNotification(getApplicationContext(), message);
    } else {
        Log.d("Message","null");
    }
}

The error log is as follows :

03-13 18:13:26.235: V/GCMBroadcastReceiver(19288): onReceive: com.google.android.c2dm.intent.RECEIVE
03-13 18:13:26.235: V/GCMBroadcastReceiver(19288): GCM IntentService class: com.example.appname.GCMIntentService
03-13 18:13:26.240: V/GCMBaseIntentService(19288): Acquiring wakelock
03-13 18:13:26.265: V/GCMBaseIntentService(19288): Intent service name: GCMIntentService-54442218753-1
03-13 18:13:26.285: V/GCMBaseIntentService(19288): Releasing wakelock
03-13 18:13:26.290: W/dalvikvm(19288): threadid=11: thread exiting with uncaught exception (group=0x4001e578)
03-13 18:13:26.300: E/AndroidRuntime(19288): FATAL EXCEPTION: IntentService[GCMIntentService-54442218753-1]
03-13 18:13:26.300: E/AndroidRuntime(19288): java.lang.NullPointerException
03-13 18:13:26.300: E/AndroidRuntime(19288):    at org.apache.http.client.utils.URLEncodedUtils.format(URLEncodedUtils.java:160)
03-13 18:13:26.300: E/AndroidRuntime(19288):    at org.apache.http.client.entity.UrlEncodedFormEntity.<init>(UrlEncodedFormEntity.java:71)
03-13 18:13:26.300: E/AndroidRuntime(19288):    at com.oi.mobileforgalaxy.webclasses.ServiceHandler.makeHttpRequest(ServiceHandler.java:53)
03-13 18:13:26.300: E/AndroidRuntime(19288):    at com.oi.mobileforgalaxy.activity.GCMIntentService.onMessage(GCMIntentService.java:71)
03-13 18:13:26.300: E/AndroidRuntime(19288):    at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
03-13 18:13:26.300: E/AndroidRuntime(19288):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
03-13 18:13:26.300: E/AndroidRuntime(19288):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 18:13:26.300: E/AndroidRuntime(19288):    at android.os.Looper.loop(Looper.java:123)
03-13 18:13:26.300: E/AndroidRuntime(19288):    at android.os.HandlerThread.run(HandlerThread.java:60)
È stato utile?

Soluzione

The error is in your serviceHandler.makeHttpRequest. Try this code :

public class ServiceHandler {
    // Global Declaration.
    static InputStream inputStream = null;
    static JSONObject jsonObject = null;
    static String json = "";
    public final static int GET = 1;
    public final static int POST = 2;

    public ServiceHandler() {
    }

    public String makeHttpRequest(String url, String method, List<NameValuePair> params) {
    try {
        if(method == "POST"){
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            json = EntityUtils.toString(httpEntity);;
        }else if(method == "GET"){
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            json = EntityUtils.toString(httpEntity);
        }           
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return json;
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top