-1

我正在开发一个应用程序。在其中我使用了运行良好的推送通知功能。

现在我想从

onMessage(Context context, Intent data){
}

并在同一函数中解析后台响应,并将解析后的数据存储在 SQLite 数据库中。

我试图只调用 Web 服务,但出现错误。我的代码如下:

@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");
    }
}

错误日志如下:

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)
4

1 回答 1

1

错误在您的serviceHandler.makeHttpRequest 中。试试这个代码:

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;
    }
}
于 2014-03-18T12:23:11.900 回答