我無法從Android向Java桌面服務器獲取價值。我只得到一個輸出。我已經使循環運行了5次。它運行一次並在android上輸出一次,然後崩潰。它應該給android 5次輸出。在每個輸出之後,它應該將價值傳遞給java桌面服務器。錯誤:空指針異常
我在做什麼和我想要的是什麼:我正在獲得信號強度。我想要5個樣本。所以我運行代碼5次。只要我一次獲得值,我就可以將它傳遞給Java Desktop Server代碼。我已經寫在java中作爲server.java,它運行得很好。
客戶端代碼:
public class MainActivity extends Activity {
protected static final long TIME_DELAY = 1000;
TextView mTextView;
private WifiManager wifiManager;
Handler handler=new Handler();
int count =0; String data ="";
private String messsage;
private static final IntentFilter FILTER = new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text_id);
wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
wifiManager.startScan();
}
@Override
public void onStart() {
super.onStart();
// Register the scan receiver
registerReceiver(scanReceiver, FILTER);
}
@Override
public void onStop() {
super.onStop();
// Unregister the receiver
unregisterReceiver(scanReceiver);
}
public void onClickRefresh(View v) {
count=0;
wifiManager.startScan();
//Log.d(TAG, "onClickRefresh");
}
BroadcastReceiver scanReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Log.d(TAG, "scanReceiver received!");
// Refreshes the chart once we have scan results
// Request a scan again
if(count<5){
final StringBuilder sb = new StringBuilder();
List<ScanResult> wifiScanList = wifiManager.getScanResults();
for (ScanResult result : wifiScanList) {
if (result.SSID.equals("Khosla ka Ghosla")) {
sb.append(""+result.level);
}
if (result.SSID.equals("panny")) {
sb.append(""+result.level);
}
if (result.SSID.equals("ferbora")) {
sb.append(""+result.level);
}
// mTextView.setText("getting called " +count + sb);
messsage=sb.toString();
mTextView.setText(messsage);
} count++;
new Asynctask1().execute(messsage);
wifiManager.startScan();
}
}
};
public class Asynctask1 extends AsyncTask<String, Void, Void> {
private PrintWriter printwriter;
private static final String IP_ADDRESS = "192.168.0.8";
private static final int DEST_PORT = 4444;
private EditText mTextField;
protected Void doInBackground(String... messages) {
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;
}
}
}
我得到的錯誤是空指針異常。我不知道爲什麼它是空的。它給出了錯誤:finally {try {client.close();.
除此之外,日誌貓不會顯示太多東西。你能指導我嗎?
@NiksTyagi是的,我可以連接服務器。如果我只寫Asynctask,那麼我可以打印到服務器。但是,當我運行代碼5次並且保持循環時不行。所以服務器很好。不知道,爲什麼這個錯誤即將到來。 –