强制关闭使用套接字和AsyncTask的AsyncTask

2023-09-04 04:34:35 作者:不见初衷

您好栈的人......

请看看我的类code和我下面的LogCat中...

我得到一个强制关闭尝试连接时。如果有人可以帮助我弄清楚为什么它会是多少AP preciated。

基本上什么code正在做的是:

在以一个IP地址的目的。 连接到IP与端口32 然后发送命令,等待响应和发送另一个命令。 在2个命令我们SENT我应该得到的响应SNX_COM> 一旦连接建立,我希望连接保持打开状态,发送特定的命令按钮的点击。 Android中使用AsyncTask实现文件下载以及进度更新提示

请帮忙:)

 包com.smarte.smartipcontrol;

进口java.io.BufferedReader中;
进口java.io.BufferedWriter中;
进口java.io.IOException异常;
进口java.io.InputStreamReader中;
进口java.io.OutputStreamWriter中;
进口的java.io.PrintWriter;
进口的java.net.InetAddress;
进口的java.net.Socket;
进口的java.net.UnknownHostException;

进口android.app.Activity;
进口android.app.Dialog;
进口android.app.ProgressDialog;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.view.View;

公共类IPControl延伸活动{

私人Socket套接字;
私人字符串serverIpAddress =com.smarte.smartipcontrol.ACTU_IP;
私有静态最终诠释REDIRECTED_SERVERPORT = 32;
公共PrintWriter的输出;
公众的BufferedReader的;
公共字符串数据;
公共对象PD;


公共无效getModel(查看视图){
    尝试 {
        通过out.println([M \ r \ N);
        //System.out.print("root\r\n);
        而(in.ready()!);
        串textStatus = readBuffer();

    }赶上(IOException异常E){}
}


@覆盖
公共无效的onCreate(包savedInstanceState){
   super.onCreate(savedInstanceState);

   的setContentView(R.layout.act_ipcontrol);


   尝试{

   this.pd = ProgressDialog.show(这一点,加载。,请等待......,真,假);
    新AsyncAction()执行()。

   }赶上(例外五){
       e.printStackTrace();
   }

   }



私有类AsyncAction扩展的AsyncTask<字符串,太虚,字符串> {
   保护字符串doInBackground(字符串参数... args){
   尝试 {
 InetAddress类serverAddr = InetAddress.getByName(serverIpAddress);
 插座=新的Socket(serverAddr,REDIRECTED_SERVERPORT);
}赶上(UnknownHostException异常E1){
 e1.printStackTrace();
}赶上(IOException异常E1){
 e1.printStackTrace();
}
尝试 {
 OUT =的新PrintWriter(新的BufferedWriter(新OutputStreamWriter(socket.getOutputStream())),TRUE);在=新的BufferedReader(新的InputStreamReader(socket.getInputStream()));
 而(在。就绪()!);
 readBuffer();
 通过out.println(根\ r \ N);
 //System.out.print("root\r\n);
 而(在。就绪()!);
 readBuffer();
 通过out.println(根\ r \ N);
 //System.out.print("root\r\n);
 而(在。就绪()!);
 弦乐味精=;

而(在。就绪()){
 MSG = MSG +(炭)的.read();
}
}赶上(IOException异常E){}

       返回null; //返回要传递给onPostExecute什么()
   }

   保护无效onPostExecute(字符串结果){

   // resultis从doInbackground返回的数据

       IPControl.this.data =结果;




       如果(IPControl.this.pd!= NULL){
           ((对话)IPControl.this.pd).dismiss();
       }
    }


}

私人字符串readBuffer()抛出IOException异常{
    弦乐味精=;

    而(in.ready()){
        MSG = MSG +(炭)in.read();
    }
    //System.out.print(msg);
    如果(msg.indexOf(SNX_COM>中!)= -1)返回msg.substring(0,msg.indexOf(SNX_COM>中));
    否则返回味精;
}

}
 

logcat的.......

  12-03 15:39:56.346:E / AndroidRuntime(2697):致命异常:AsyncTask的#5
12-03 15:39:56.346:E / AndroidRuntime(2697):java.lang.RuntimeException的:一个错误而执行doInBackground发生()
12-03 15:39:56.346:E / AndroidRuntime(2697):在android.os.AsyncTask $ 3.done(AsyncTask.java:299)
12-03 15:39:56.346:E / AndroidRuntime(2697):在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
12-03 15:39:56.346:E / AndroidRuntime(2697):在java.util.concurrent.FutureTask.setException(FutureTask.java:219)
12-03 15:39:56.346:E / AndroidRuntime(2697):在java.util.concurrent.FutureTask.run(FutureTask.java:239)
12-03 15:39:56.346:E / AndroidRuntime(2697):在android.os.AsyncTask $ SerialExecutor $ 1.运行(AsyncTask.java:230)
12-03 15:39:56.346:E / AndroidRuntime(2697):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-03 15:39:56.346:E / AndroidRuntime(2697):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:573)
12-03 15:39:56.346:E / AndroidRuntime(2697):在java.lang.Thread.run(Thread.java:856)
12-03 15:39:56.346:E / AndroidRuntime(2697):由:显示java.lang.NullPointerException
12-03 15:39:56.346:E / AndroidRuntime(2697):在com.smarte.smartipcontrol.IPControl $ AsyncAction.doInBackground(IPControl.java:71)
12-03 15:39:56.346:E / AndroidRuntime(2697):在com.smarte.smartipcontrol.IPControl $ AsyncAction.doInBackground(IPControl.java:1)
12-03 15:39:56.346:E / AndroidRuntime(2697):在android.os.AsyncTask $ 2.call(AsyncTask.java:287)
12-03 15:39:56.346:E / AndroidRuntime(2697):在java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-03 15:39:56.346:E / AndroidRuntime(2697):4 ...更多
 

解决方案

我觉得你的问题是在这里:

 的InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
 插座=新的Socket(serverAddr,REDIRECTED_SERVERPORT);
 

我找不到,你的 serverIpAddress 分配,这样你会得到NullPointerException异常。

Hello people of stack....

Please see my class code and my LogCat below...

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:

Taking an IP Address from an intent. Connecting to the IP with port 32 Then send a command, wait for response and the send another command. After the 2 commands our sent I should get a response of "SNX_COM>" 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.......

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.