我无法从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;
}
}
}
我得到的错误是nullpointer异常。我不知道为什么它有空。它给出了错误:finally {try {client.close();.
除此之外,log cat没有显示太多东西。你能指导我吗?
答案 0 :(得分:0)
你在doInBackground方法中返回null。