我正在尝试将ibmcloud连接到我的应用程序。但是无论何时 connectToCloud()
它抛出以下异常。
我知道这个问题在这里问了很多次,但我不能解决这个我是新的安卓系统,我不能解决这个问题。请告诉我怎么解决这个问题。
D/com.iwavesys.iwave_trakr.IBMCloud.IBMConnect: .connectDevice() entered
I/System.out:*************CLIENT_ID*********d:z5g5sd:OBDII_Device:AndroidDEV01
connectionURI tcp://z5g5sd.messaging.internetofthings.ibmcloud.com:1883
I/System.out: IMEI: 123568689090005
D/com.iwavesys.iwave_trakr.IBMCloud.IBMConnect: .isMqttConnected() entered
I/System.out:*************CLIENT_ID*********d:z5g5sd:OBDII_Device:AndroidDEV01
I/System.out: Inside try block ............
Client value : org.eclipse.paho.android.service.MqttAndroidClient@54a8c99
D/com.iwavesys.iwave_trakr.IBMCloud.IBMConnect: .isMqttConnected() - returning false
I/System.out: client is : org.eclipse.paho.android.service.MqttAndroidClient@eeaab5e
D/com.iwavesys.iwave_trakr.IBMCloud.IBMConnect: Connecting to server : tcp://z5g5sd.messaging.internetofthings.ibmcloud.com:1883
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
W/System.err: at android.content.ComponentName.<init>(ComponentName.java:117)
at android.content.Intent.setClassName(Intent.java:9573)
W/System.err: at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:407)
at com.iwavesys.iwave_trakr.IBMCloud.IBMConnect.connectToCloud(IBMConnect.java:248)
at com.iwavesys.iwave_trakr.MainActivity$8.run(MainActivity.java:658)
at java.lang.Thread.run(Thread.java:764)
下面是代码中问题所在的部分:
mainactivity.java:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, ConnectivityReceiver.ConnectivityReceiverListener
{
............
............
static Context g_context;
@SuppressLint("StaticFieldLeak")
public static Context context;
public static MainActivity getInstance() {
if (instance == null) {
instance = new MainActivity();
}
return instance;
}
public static Context getContext() {
return context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...............
...............
g_context = context;
context = getApplicationContext();
...........
...........
}
public void new_session() {
// Wait till connection
//(!(con_stat.equals("Connected"))) {
System.out.println("new_Session Function.....");
System.out.println("Context : " +context);
//c_ibm.SubscribeToCloud(this);
proceed_to_connect();
}
public void proceed_to_connect() {
MainActivity.getInstance().wascalled = true;
new Thread(new Runnable() {
@Override
public void run() {
try {
Looper.prepare();
System.out.println("In proceed to Connect Function");
System.out.println("Context Value : " + context);
c_ibm = new IBMConnect(context, MainActivity.getInstance());
c_ibm.connectToCloud(context);
int status = 1;
statusConnected(status);
} catch (Exception e) {
e.printStackTrace();
Log.e(LOG_TAG, "Exception occurred when generating new private key and certificate.", e);
}
}
}).start();
}
ibmconnect.java文件:
public class IBMConnect {
private static IBMConnect instance;
private static MqttAndroidClient client;
private Context context;
static MainActivity m_act;
private String organization = "xyz";
private String deviceType = "xyz_Device";
private String deviceID = "xyz001";
private String authorizationToken = "xyz123";
private MqttCallback callbacks = null;
private IMqttActionListener listener = null;
protected MqttAsyncClient mqttAsyncClient = null;
protected volatile boolean disconnectRequested = false;
protected MqttClient mqttClient = null;
private ConnectionListener connectionListener;
public IBMConnect(Context context, MainActivity act) {
this.context = context;
this.client = null;
m_act = act;
}
public static IBMConnect getInstance(Context context) {
Log.d(TAG, ".getInstance() entered");
if (instance == null) {
instance = new IBMConnect(context, m_act);
}
return instance;
}
/**Connect to the Waston Internet of Things Platform
* @param context
*/
public void connectToCloud (Context context ) throws MqttException, KeyManagementException, NoSuchAlgorithmException {
//public IMqttToken connectToCloud (IoTCallbacks callbacks, IoTActionListener listener, SocketFactory factory) throws MqttException {
Log.d(TAG, ".connectDevice() entered");
String clientID = "d:" + this.getOrganization() + ":" + this.getDeviceType() + ":" + this.getDeviceID();
System.out.println("*************CLIENT_ID*********" + clientID);
String connectionURI;
SocketFactory factory = null;
if (factory == null || this.getOrganization().equals("quickstart")) {
connectionURI = "tcp://" + this.getOrganization() + IOT_ORGANIZATION_TCP;
System.out.println("connectionURI " + connectionURI);
} else {
connectionURI = "ssl://" + this.getOrganization() + IOT_ORGANIZATION_SSL;
System.out.println("connectionURI " + connectionURI);
}
imei = get_imei();
previmei = imei;
if (!isMqttConnected()) {
if (client != null) {
client.unregisterResources();
client = null;
}
client = new MqttAndroidClient(context, connectionURI, clientID);
System.out.println("client is : " + client);
client.setCallback(callbacks);
String username = IOT_DEVICE_USERNAME;
char[] password = this.getAuthorizationToken().toCharArray();
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true);
options.setUserName(username);
options.setPassword(password);
options.setConnectionTimeout(30);
options.setAutomaticReconnect(true);
options.setKeepAliveInterval(10);
if (factory != null && !this.getOrganization().equals("quickstart")) {
options.setSocketFactory(factory);
}
Log.d(TAG, "Connecting to server : " + connectionURI);
try {
**client.connect(options, context, listener);**//Getting error in this line
return;
//return client.connect();
} catch (MqttException e) {
Log.e (TAG, "Exception caught while attempting to connect to server ", e.getCause());
throw e;
}
readstatus();
}
我得到一个错误 client.connect(options, context, listener);
这条线。我在哪里失踪??任何人,请帮我解决这个错误。
谢谢您。。。
暂无答案!
目前还没有任何答案,快来回答吧!