问题描述
I tried to display data(string) from android device to java desktop server application. I was successful in that. I tried to find wifi signal strength from wifi access point to android device. I did that too. Now, I need to integrate this both thing.
First, is the client code where I find the signal strength and pass it to server. First, I run the server. It runs and waits. As soon as I run the client the signal strength from 1st router is displayed and then the app crashes. I have given the error log.
It crashes because of Async method in the client. This Async task code works well when I try to send string from android mobile to java desktop server. But, here it gives me an error. I think, I have made some simple error.
Client.java
package com.example.temp;
public class MainActivity extends Activity {
private Socket client;
private PrintWriter printwriter;
private EditText textField;
private Button button;
private String messsage;
StringBuilder sb = new StringBuilder();
public final static String extra = "com.example.temp.MESSAGE";
protected static final long TIME_DELAY = 5000;
TextView mTextView;
Handler handler=new Handler();
int count =0; String data ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text_id);
handler.post(updateTextRunnable);
messsage=mTextView.getText().toString();
new Asynctask().execute(messsage);
}
Runnable updateTextRunnable = new Runnable() {
public void run() {
if (count < 5) {
WifiManager mainWifiObj;
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
class WifiScanReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
}
}
WifiScanReceiver wifiReciever = new WifiScanReceiver();
registerReceiver(wifiReciever, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
for (ScanResult result : wifiScanList) {
if (result.SSID.equals("Dal-WPA2")) {
sb.append(result.level);
}
if (result.SSID.equals("eduroam")) {
sb.append(result.level);
}
if (result.SSID.equals("Dal")) {
sb.append(result.level);
}
}
count++; mTextView.setText("getting called " +count + sb);
} else {
}
//----------------code here to send values to java server---
handler.postDelayed(this, TIME_DELAY);
}
};
class Asynctask extends AsyncTask<String, Void, Void> {
private static final String IP_ADDRESS = "134.190.162.165";
private static final int DEST_PORT = 4444;
private EditText mTextField;
protected Void doInBackground(String... messages) {
// if (messages.length != 1) { return null; }
String message = messages[0];
Socket client = null;
try {
client = new Socket(IP_ADDRESS, DEST_PORT); // connect to server
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Write to server.
try {
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(messsage); // write the message to output stream
printwriter.flush();
printwriter.close();
}
catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
}
The Error from the log cat I found is : It is in the Async task here.
>FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
android.os.AsyncTask$3.done(AsyncTask.java:278)
java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
java.util.concurrent.FutureTask.setException(FutureTask.java:124)
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
java.util.concurrent.FutureTask.run(FutureTask.java:137)
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
com.example.temp.MainActivity$Asynctask.doInBackground(MainActivity.java:114)
com.example.temp.MainActivity$Asynctask.doInBackground(MainActivity.java:1)
android.os.AsyncTask$2.call(AsyncTask.java:264)
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
Activity com.example.temp.MainActivity has leaked IntentReceiver com.example.temp.MainActivity $1$1WifiScanReceiver@41756998 that was originally registered here. Are you missing a call to unregisterReceiver()?
02-17 15:13:52.771: E/ActivityThread(25738): android.app.IntentReceiverLeaked: Activity com.example.temp.MainActivity has leaked IntentReceiver com.example.temp.MainActivity$1$1WifiScanReceiver@41756998 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:763)
android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:567)
android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1167)
android.app.ContextImpl.registerReceiver(ContextImpl.java:1154)
android.app.ContextImpl.registerReceiver(ContextImpl.java:1148)
android.content.ContextWrapper.registerReceiver(ContextWrapper.java:348)
com.example.temp.MainActivity$1.run(MainActivity.java:58)
android.os.Handler.handleCallback(Handler.java:605)
android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4517)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)
The server just waits for the response and displays the data. With simple string it displays the data But here, the client crashes so, it is not possible. So, I assume that client code is perfect.
There is no compile time error only run time. In the run(), I call Async method and then do handler.postdelayed, so that code runs again after few second. But it runs correctly till Asynctask class is called In the Async task, it gives me error.
I am new. It would be great if someone can point out my mistake.
I tried but I am not getting what I should do. I think my logic is correct as individual parts are running. But dont know why async task is not running here.
Thank you in advance.
Edited : The print writer and other sockets closed.
Still getting the same error.
I have just used class Asynctask and not public class Asycntask, hope there wont be any problem in this.
I just understand from the log file that error is in asynctask background , but exactly the error is not understandable also.
I dont know where I am getting wrong.
Thanks for the help.
fdvkfdnvkkfdnb fdkvj nfdvnjfdknv fdvnfdvnkdfnv fdjkvnfdkjvnjkfdn
fdjvfdjkv fdvndfkjvn fdlvnkdfvn fdkvnfdnvk fdvknfdkvn
fdvklmfd fdkv flkk fdkvkfl fdkkklvklfdm flvmalsvmaslvv
dfvkfdlkv˚Flkfdv fdlkvmfdklvm fdvkfdklv dfklvlfdknvkfdnv
dfvknfdklvn dfkvfdkvn fdkvnfdkvn dfkkvnfdknv dfjkvnfdkjnv
fdvkfdnvkkfdnb fdkvj nfdvnjfdknv fdvnfdvnkdfnv fdjkvnfdkjvnjkfdnfdjvfdjkv fdvndfkjvn fdlvnkdfvn fdkvnfdnvk fdvknfdkvnfdvklmfd fdkv flkk fdkvkfl fdkkklvklfdm flvmalsvmaslvvdfvkfdlkv f lkfdv fdlkvmfdklvm fdvkfdklv dfklvlfdknvkfdnvdfvknfdklvn dfkvfdkvn fdkvnfdkvn dfkkvnfdknv dfjkvnfdkjnv
推荐答案
我的猜测是,这个问题的出现是因为你没有正确关闭客户端
和 PrintWriter的
。 (您需要关闭所有流套接字以及插座本身。)在重复执行中,的AsyncTask
试图打开一个新的套接字,并最终操作系统用完资源这样做,因为此故障关闭的左右。到新的Socket(...)呼叫
抛出一个异常(你可以捕获它),但是客户端
遗体空
。既然你不例外返回后,你再得到一个 NullPointerException异常
。我的猜测是,这个 NullPointerException异常
是何等的负责应用程序崩溃。 (这确实只是一个猜测。在您发布错误的信息是相当稀疏)
My guess is that the problem arises because you are not properly closing client
and printwriter
. (You need to close all streams for the socket as well as the socket itself.) On repeated executions, the AsyncTask
tries to open a new socket and eventually the OS runs out of resources for doing so because of this failure to close. The call to new Socket(...)
throws an exception (which you catch) but then client
remains null
. Since you don't return after the exception, you then get a NullPointerException
. My guess is that this NullPointerException
is what's responsible for the app crash. (This really is just a guess; the info in the error you posted is quite sparse.)
试试这个code来代替:
Try this code instead:
public class Asynctask extends AsyncTask<String, Void, Void> {
private EditText mTextField;
protected Void doInBackground(String... messages) {
String message = messages[0];
Socket client = null;
try {
client = new Socket(IP_ADDRESS, DEST_PORT);
} catch (IOException e) {
e.printStackTrace();
return null;
}
try {
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(messsage);
}
catch (IOException e) {
e.printStackTrace();
} finally {
if (printwriter != null) {
try {
printwriter.close();
} catch (IOException e) {
e.printStackTrace();
}
printwriter = null;
}
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
这篇关于机器人的AsyncTask方法不显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!