0
在我的應用程序中,我可能建立藍牙連接並使用自己的服務發送字節,並向接收方發送一些字節。ServiceConnection - 服務拋出NullPointerException
現在我試圖連接到我自己的服務器發送文本並獲得答案失敗。對於任何原因,我得到了我的服務NullPointerException異常,如果我嘗試調用sendMessage()方法..
服務:
import android.annotation.TargetApi;
import android.app.Service;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
public class ClientService extends Service {
private String LOG_TAG = null;
private final IBinder mBinder = new LocalBinder();
private final String url_string = "";
private int receivedUcCommand = 0;
@Override
public void onCreate() {
super.onCreate();
}
public void sendToServer(final String text){
new Thread(new Runnable() {
@Override
public void run() {
if(internetAvailable()){
try {
String textParam = "text1" + URLEncoder.encode(text,"UTF-8");
URL scriptUrl = new URL(url_string);
HttpURLConnection connection = (HttpURLConnection) scriptUrl.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setFixedLengthStreamingMode(textParam.getBytes().length);
OutputStreamWriter contextWriter = new OutputStreamWriter(connection.getOutputStream());
contextWriter.write(textParam);
contextWriter.flush();
contextWriter.close();
InputStream answerInputStream = connection.getInputStream();
String answer = read(answerInputStream);
answerInputStream.close();
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
private String read(InputStream is){
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String currentLine;
try {
while((currentLine = br.readLine()) != null){
sb.append(currentLine+"\n");
}
}catch (IOException e){
e.printStackTrace();
}
return sb.toString().trim();
}
private boolean internetAvailable(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
return ni != null && ni.isConnectedOrConnecting();
}
@Override
public IBinder onBind(Intent intent) {
// Wont be called as service is not bound
Log.i(LOG_TAG, "In onBind");
return mBinder;
}
public class LocalBinder extends Binder {
ClientService getService() {
// Return this instance of LocalService so clients can call public methods
return ClientService.this;
}
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.i(LOG_TAG, "In onTaskRemoved");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(LOG_TAG, "In onDestroy");
}
}
在活動我這樣做:
public class loggedInActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
public static final String mBroadcastStringAction = "com.truiton.broadcast.string";
public static final String mBroadcastIntegerAction = "com.truiton.broadcast.integer";
public static final String mBroadcastArrayListAction = "com.truiton.broadcast.arraylist";
private TextView mTextView;
private IntentFilter mIntentFilter;
BluetoothService bService;
ClientService cService;
boolean btBound = false;
boolean clientBound = false;
// private View startButton;
Button testButton;
EditText et;
static final int CAM_REQUEST = 1;
//Button reconnectButton;
TextView recText;
recText = (TextView) findViewById(R.id.receivedText);
//recText.setText("KEIN ALARM");
et = (EditText) findViewById(R.id.editText);
testButton = (Button) findViewById(R.id.testButton);
// This way I start and bind the service for Bluetooth, to my activity
Intent btIntent = new Intent(this, BluetoothService.class);
startService(btIntent);
bindService(btIntent, btServiceConnection, Context.BIND_AUTO_CREATE);
//And this way I unsuccessfully trying to bind and start the ClientService for ServerConnection
Intent clientIntent = new Intent(this, ClientService.class);
startService(clientIntent);
bindService(clientIntent, clientServiceConnection, Context.BIND_AUTO_CREATE);
testButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//ATTENTION: TRYING TO SENDING TO SERVER HERE, IT THROWS ME A NULLPOINTER
cService.sendToServer(et.getText().toString());
}
});
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection btServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
BluetoothService.LocalBinder binder = (BluetoothService.LocalBinder) service;
bService = binder.getService();
btBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
btBound = false;
}
};
//I think it has to be to do something with that code here, but
this is the only place, where cService is defined...
private ServiceConnection clientServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
ClientService.LocalBinder binder = (ClientService.LocalBinder) service;
cService = binder.getService();
clientBound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
clientBound = false;
}
};