我嘗試了Android Studio中的Cloudboost Java Sdk並遇到了希望有人可以幫助我的障礙。我認爲我已經正確設置了該項目,但Logcat告訴我其他方面。我已經包含了所有相關文件的摘錄供您查看。感謝Cloudboost Java Android Studio項目設置
首先,這裏是logcat的錯誤:
致命異常:主要過程:main.dozer74.cloudboosttest,PID:18357 java.lang.IllegalStateException:在機器人的onClick :的Android無法執行方法。 (android.view.java:5198) at android.view.View $ PerformClick.run(View.java: 21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java .lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit。 java:616) 引發:java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java: 288) at android.view.View.performClick(View.java:5198) at android.view.View $ PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit .ja VA:616) 產生的原因:android.os.NetworkOnMainThreadException 在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273) 在java.net.InetAddress.lookupHostByName(InetAddress.java:431) 是java .net.InetAddress.getAllByNameImpl(InetAddress.java:252) at java.net.InetAddress.getAllByName(InetAddress.java:215) at com.android.okhttp.internal.Network $ 1.resolveInetAddresses(Network.java:29)在com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188) (在com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelec tor.java:157) 在com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100) 在com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357) at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340) at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330) at com.android。okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433) at com.android.okhttp.internal.huc。 HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114) 在com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245) 在com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection。的java:218) 在com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java) 在io.cloudboost.util.CBParser.callJson(CBParser.java:69) 在io.cloudboost.CloudQuery.find(CloudQuery.java:1408) 在main.dozer74.cloudboosttest.MainActivity.getTroopsFromDatabase(MainActivity.java:27) 在java.lang.reflect.Method.invoke(本機方法) 在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 在android.view.View.performClick(View.java:5198) 在android.view.View $ PerformClick.run(查看。的java:21147) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(嚕per.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller .RUN(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
應用程序是非常簡單;只是執行查詢按鈕
按鈕的XML代碼
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Troops From Database"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:onClick="getTroopsFromDatabase"/>
的按鈕
Java代碼:
public void getTroopsFromDatabase(View view) {
//Log.d("dozer74", "Get troops from database");
CloudQuery query = new CloudQuery("Messages");
try {
query.find(new CloudObjectArrayCallback(){
@Override
public void done(CloudObject[] x, CloudException t) {
if(x != null){
Log.d("dozer74", "====>>>> x is not equal to null");
} else {
Log.d("dozer74", "====>>>> x is equal to null");
}
if(t != null){
Log.d("dozer74", "====>>>> t is not equal to null");
} else {
Log.d("dozer74", "====>>>> t is equal to null");
}
}
});
} catch (CloudException e) {
e.printStackTrace();
}
}
這裏是我的清單與引用一個叫做應用程序類在我的Cloudboost初始化發生
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="main.dozer74.cloudboosttest">
<application
android:name=".Application"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
最後,這裏是來自Application.class的代碼,應用程序ID和主鍵「X」編出來的,除了代碼顯而易見的原因加入到我的build.gradle在那裏我得到了SDK到項目中。
Application.class
public class Application extends android.app.Application {
@Override
public void onCreate() {
super.onCreate();
//Init your Application
CloudApp.init("xxxxxxxxxxxx", "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
}
}
的build.gradle片斷
repositories {
maven {
url 'https://docs.cloudboost.io/java.html'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'io.cloudboost:JavaSDK:1.0.3'
}