-3

可能重复:
使用套接字和 AsyncTask 强制关闭

嗨,堆栈的人们....

请帮帮我,我正在失去理智。

请看代码和logcat.....

基本上代码正在做的是:

  1. 从意图中获取 IP 地址。
  2. 使用 32 端口连接到 IP
  3. 然后发送一个命令,等待响应并发送另一个命令。
  4. 在我们发送 2 个命令后,我应该得到“SNX_COM>”的响应
  5. 建立连接后,我希望连接保持打开状态,以便在单击按钮时发送特定命令。

^^这基本上是我的代码应该实现的^^

Java 类

package com.smarte.smartipcontrol;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;

public class IPControl extends Activity {

private Socket socket;
private String serverIpAddress = "com.smarte.smartipcontrol.ACTU_IP";
private static final int REDIRECTED_SERVERPORT = 32;
public PrintWriter out;
public BufferedReader in;
public String data;
public Object pd;



public void getModel(View view) {
    try {
        out.println("[m\r\n");
        //System.out.print("root\r\n");
        while(!in.ready());
        String textStatus = readBuffer();

    } catch(IOException e) {}
}


@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   setContentView(R.layout.act_ipcontrol);


   try{   

    new AsyncAction().execute();

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

   }



private class AsyncAction extends AsyncTask<String, Void, String> {
   protected String doInBackground(String... args) { 
   try {
 InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
 socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
} catch (UnknownHostException e1) {
 e1.printStackTrace();
} catch (IOException e1) {
 e1.printStackTrace();
}
try {
 out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
 while (! in .ready());
 readBuffer();
 out.println("root\r\n");
 //System.out.print("root\r\n");
 while (! in .ready());
 readBuffer();
 out.println("root\r\n");
 //System.out.print("root\r\n");
 while (! in .ready());
 String msg = "";

while ( in .ready()) {
 msg = msg + (char) in .read();
}
} catch (IOException e) {}

       return null;//returns what you want to pass to the onPostExecute()
   }

   protected void onPostExecute(String result) {

   //results the data returned from doInbackground

       IPControl.this.data = result;



       }
   }


private String readBuffer() throws IOException {
    String msg = "";

    while(in.ready()) {
        msg = msg + (char)in.read();
    }
    //System.out.print(msg);
    if(msg.indexOf("SNX_COM> ") != -1) return msg.substring(0, msg.indexOf("SNX_COM> "));
    else return msg;
}

}

日志猫....

12-03 21:56:43.670: E/AndroidRuntime(1231): FATAL EXCEPTION: AsyncTask #1
12-03 21:56:43.670: E/AndroidRuntime(1231): java.lang.RuntimeException: An error occured while executing doInBackground()
12-03 21:56:43.670: E/AndroidRuntime(1231):     at  android.os.AsyncTask$3.done(AsyncTask.java:299)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at java.lang.Thread.run(Thread.java:856)
12-03 21:56:43.670: E/AndroidRuntime(1231): Caused by: java.lang.NullPointerException
12-03 21:56:43.670: E/AndroidRuntime(1231):     at com.smarte.smartipcontrol.IPControl$AsyncAction.doInBackground(IPControl.java:71)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at com.smarte.smartipcontrol.IPControl$AsyncAction.doInBackground(IPControl.java:1)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-03 21:56:43.670: E/AndroidRuntime(1231):     ... 4 more
4

1 回答 1

1

当您吞下异常时会发生这种情况......您在其中一行有一个 NPE(不能说是哪一个,因为您将两者都粘贴在同一行 71):

out = new PrintWriter(new BufferedWriter(
    new OutputStreamWriter(socket.getOutputStream())), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

socketIPControl(就像inand out)的一个实例变量,这意味着如果由于某种原因无法建立连接,程序只会打印堆栈跟踪并继续:

try {
    InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
    socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
} catch (UnknownHostException e) {
    e.printStackTrace();
}
// next of the code

如果您仔细检查您的控制台(或者可能是 Eclipse 控制台),您会发现究竟发生了什么,因此您可以做些什么来解决实际问题。

但是,您确实应该将所有代码放在第一个try块中,因为如果无法建立连接,继续进行没有任何意义。

此外,socketinout应该是实例变量——我们不能提供进一步的建议,因为你没有提供上下文——但是,由于这些在 Activity 的其余代码中没有重用,你可以自由地将它们设为局部变量。close()最后,完成后对所有事情都是一个好习惯(这是不使用实例变量的另一个原因)。

于 2012-12-03T22:49:37.513 回答