简体   繁体   English

使用套接字和AsyncTask强制关闭

[英]Force close using sockets and AsyncTask

Hello people of stack.... 您好堆叠的人。

Please see my class code and my LogCat below... 请在下面查看我的课程代码和LogCat ...

I am getting a force close when trying to connect. 尝试连接时,我将近距离受力。 If someone could help me to figure out why it would be much appreciated. 如果有人可以帮助我找出原因,将不胜感激。

Basically what the code is doing is: 基本上,代码在做什么:

  1. Taking an IP Address from an intent. 从意图中获取IP地址。
  2. Connecting to the IP with port 32 通过端口32连接到IP
  3. Then send a command, wait for response and the send another command. 然后发送命令,等待响应,然后发送另一个命令。
  4. After the 2 commands our sent I should get a response of "SNX_COM>" 在发送了2条命令后,我应该得到“ SNX_COM>”的响应
  5. Once the connection is established, I want the connection to stay open to send specific commands on button click. 建立连接后,我希望连接保持打开状态以在单击按钮时发送特定命令。

Please help :) 请帮忙 :)

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.app.Dialog;
import android.app.ProgressDialog;
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{   

   this.pd = ProgressDialog.show(this, "Loading..", "Please Wait...", true, false);
    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) {

   //resultis the data returned from doInbackground

       IPControl.this.data = result;




       if (IPControl.this.pd != null) {
           ((Dialog) IPControl.this.pd).dismiss();
       }
    }


}

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;
}

}

Logcat....... Logcat .......

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

I think your problem is here: 我认为您的问题在这里:

 InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
 socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);

I can't find where your serverIpAddress is assigned, thus you get NullPointerException. 我找不到分配serverIpAddress位置,因此得到NullPointerException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM