在Android上实现Socket on Service

时间:2013-05-29 19:17:45

标签: android sockets service

我正在通过套接字与我的服务器进行通信,但我遇到了android 4.0,4.1和4.2的一些问题。 但是使用Android 2.3.3,相同的代码运行良好。

我的代码: http://paste2.org/98FEtt6f

在我的客户端套接字下面:

package com.example.socketservice;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class SocketService extends Service implements Runnable {

    Thread connectionWriting;
    Thread connectionListening;

    Socket nsocket; //Network Socket
    BufferedReader nis; //Network Input Stream
    DataOutputStream nos; //Network Output Stream

    String rx;
    String tx = null;

    private static final String SOCKETSERVER = "xxxx";
    private static final int SOCKETPORT = 9999;

    @Override
    public void onCreate() {
        Log.i("DEBUG", "onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        SocketAddress sockaddr = null;
        try {
            sockaddr = new InetSocketAddress(SOCKETSERVER, SOCKETPORT);
            nsocket = new Socket();
        } catch (Exception e) {
            Log.d("DEBUG", e.getMessage());
        }

        try {
            nsocket.connect(sockaddr, 0);
            if (nsocket.isConnected()) {
             Log.d("DEBUG", "conectado");
             nis = new BufferedReader(new InputStreamReader(nsocket.getInputStream()));          
                connectionListening = new Thread(this, "ConnectionListening");
                connectionListening.start();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        try {
            nis.close();
            nos.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            nsocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Log.i("DEBUG", "onDestroy");
    }

    private synchronized void connectionListening() {
        try {
            while ((rx = nis.readLine()) != null) {
                Log.d("DEBUG", "Recebido: " + rx);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void run() {
        for (;;) {
            Log.d("DEBUG", "Lendo");
            connectionListening();
        }
    }
}

错误:

05-29 16:12:28.151: E/AndroidRuntime(19620): FATAL EXCEPTION: main
05-29 16:12:28.151: E/AndroidRuntime(19620): java.lang.RuntimeException: Unable to start service com.example.socketservice.SocketService@4127a928 with Intent { act=SocketService }: java.lang.NullPointerException
05-29 16:12:28.151: E/AndroidRuntime(19620):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2390)
05-29 16:12:28.151: E/AndroidRuntime(19620):    at android.app.ActivityThread.access$1900(ActivityThread.java:128)
05-29 16:12:28.151: E/AndroidRuntime(19620):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
05-29 16:12:28.151: E/AndroidRuntime(19620):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 16:12:28.151: E/AndroidRuntime(19620):    at android.os.Looper.loop(Looper.java:137)
05-29 16:12:28.151: E/AndroidRuntime(19620):    at android.app.ActivityThread.main(ActivityThread.java:4514)
05-29 16:12:28.151: E/AndroidRuntime(19620):    at java.lang.reflect.Method.invokeNative(Native Method)
05-29 16:12:28.151: E/AndroidRuntime(19620):    at java.lang.reflect.Method.invoke(Method.java:511)
05-29 16:12:28.151: E/AndroidRuntime(19620):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-29 16:12:28.151: E/AndroidRuntime(19620):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-29 16:12:28.151: E/AndroidRuntime(19620):    at dalvik.system.NativeStart.main(Native Method)
05-29 16:12:28.151: E/AndroidRuntime(19620): Caused by: java.lang.NullPointerException
05-29 16:12:28.151: E/AndroidRuntime(19620):    at com.example.socketservice.SocketService.onStartCommand(SocketService.java:49)
05-29 16:12:28.151: E/AndroidRuntime(19620):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2373)
05-29 16:12:28.151: E/AndroidRuntime(19620):    ... 10 more

0 个答案:

没有答案