我有扩展异步任务的类和另一个扩展线程的类。
客户端.java
public class Client extends AsyncTask<String, Void, Void>
{
Socket socket;
private SocketClient socketClient;
DataOutputStream out;
@Override
protected Void doInBackground(String... params)
{
// TODO Auto-generated method stub
Log.d("home","Client");
connect();
return null;
}
protected void connect()
{
// TODO Auto-generated method stub
Log.d("home","Connect");
if(socket!=null)
{
disconnect();
return;
}
try
{
String host = "111.111.1.111";
InetAddress in=InetAddress.getByName(host);
socket = new Socket(in,21);
socketClient=SocketClient.handle(this,socket);
Log.d("home","Connection established");
}
catch(Exception e)
{
e.printStackTrace();
Log.d("home","Not Connection established");
}
}
void disconnect()
{
// TODO Auto-generated method stub
try
{
socketClient.setDesonnected(true);
socket.close();
}
catch (Exception e)
{
System.err.println("Error closing client : "+e);
}
socket=null;
}
protected void sendmessage(byte[] commandchannel1)
{
// TODO Auto-generated method stub
byte[] commandchannel=commandchannel1;
try
{
if(out==null)
{
out=new DataOutputStream(socket.getOutputStream());
}
// out.writeInt(commandchannel.length);
out.write(commandchannel);
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
disconnect();
}
}
public void setreceived(String got)
{
// TODO Auto-generated method stub
System.out.println(got);
}
}
SocketClient.java
public class SocketClient extends Thread
{
private boolean desonnected=false;
private static SocketClient socketClient=null;
private Socket socket=null;
private static Client parent;
private DataInputStream in;
customadapter customadapter;
public synchronized void setDesonnected(boolean cr)
{
desonnected=cr;
}
private SocketClient(Client parent, Socket s)
{
super("Client");
this.parent = parent;
socket=s;
setDesonnected(false);
start();
System.out.println(this.parent);
}
public static SocketClient handle(Client client, Socket socket)
{
// TODO Auto-generated method stub
if(socketClient==null)
socketClient=new SocketClient(parent, socket);
else {
if(socketClient.socket!=null) {
try {
socketClient.socket.close();
} catch (Exception e) {
}
}
socketClient.socket=null;
socketClient=new SocketClient(parent,socket);
}
return socketClient;
}
public void run() {
InputStream is = null;
try {
is = socket.getInputStream();
System.out.println(is);
in = new DataInputStream(is);
} catch(IOException e) {
try {
socket.close();
} catch(IOException e2) {
System.err.println("Socket not closed :"+e2);
}
System.out.println("Could not open socket : "+e.getMessage());
parent.disconnect();
return;
}
while(!desonnected) {
try {
String got = readInputStream(in); //in.readLine();
if(got==null) {
//parent.error("Connection closed by client");
parent.disconnect();
break;
}
**parent.setreceived(got);**
} catch(IOException e)
{
if(!desonnected)
{
System.out.println(e.getMessage());
parent.disconnect();
}
break;
}
}//end of while
try {
is.close();
in.close();
//socket.close();
} catch (Exception err) {}
socket=null;
}//end of run
private static String readInputStream(DataInputStream _in) throws IOException
{
StringBuilder sb=new StringBuilder();
String data = "";
int s = _in.read();
if(s==-1)
return null;
data += ""+String.format("%X", s)+ " ";
int len = _in.available();
System.out.println("Len got : "+len);
if(len > 0)
{
byte[] byteData = new byte[len];
_in.read(byteData);
for(byte b:byteData)
{
System.out.println(byteData);
sb.append(String.format("%02X",b));
sb.append(" ");
}
}
data += sb.toString();
System.out.println(data);
return data;
}
}
当 setreceived(got)
被调用时,我收到异常,因为 Attempt to invoke virtual method on null pointer exception。我将如何克服这个异常。
我从另一个 Activity 调用异步任务作为
client=new Client();
client.execute();
我想最终将接收到的值返回给一个 Activity 。我怎样才能做到这一点。 请帮助。提前致谢
最佳答案
您必须更改handle
方法。至于初始化SocketClient
,你已经传递了参数parent
,它还没有被初始化。所以传递 'client' 而不是它。
public static SocketClient handle(Client client, Socket socket)
{
// Verify that the socket is open
if(socketClient==null)
socketClient=new SocketClient(client, socket);
else {
if(socketClient.socket!=null) {
try {
socketClient.socket.close();
} catch (Exception e) {
}
}
socketClient.socket=null;
socketClient=new SocketClient(client,socket);
}
return socketClient;
}
关于java - 将数据从Android中的另一个类发送到异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34260590/