与db的连接(Parse)

时间:2014-05-15 09:41:02

标签: java android parse-platform

目前我遇到Parse数据库连接问题(我认为),我想要做的是从数据库中检索信息并将其放在页面上。到目前为止,我被告知是我不需要编辑xml页面上的信息,只需要编辑Java页面。

public class Clothing extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_clothing);//database connection
    Parse.initialize <removed this due that the password of the db is>

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Products");
    query.getInBackground("5nVfgKvqkM", new GetCallback<ParseObject>() {
        public void done(ParseObject object, ParseException e) {
            if (e == null) {
                Toast.makeText(getBaseContext(), "succes", 5000);
             // object will be your game score
            } else {
                Toast.makeText(getBaseContext(), e.toString(), 5000);
             // something went wrong
            }
        }
    });
}
}

错误日志:

05-15 10:50:12.123: E/AndroidRuntime(19371): FATAL EXCEPTION: main
05-15 10:50:12.123: E/AndroidRuntime(19371): Process: nl.creationinc.swopp2, PID: 19371
05-15 10:50:12.123: E/AndroidRuntime(19371): java.lang.RuntimeException: Unable to start activity ComponentInfo    {nl.creationinc.swopp2/nl.creationinc.swopp2.Clothing}: android.os.NetworkOnMainThreadException
05-15 10:50:12.123: E/AndroidRuntime(19371):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at android.app.ActivityThread.access$800(ActivityThread.java:156)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at android.os.Handler.dispatchMessage(Handler.java:102)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at android.os.Looper.loop(Looper.java:157)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at android.app.ActivityThread.main(ActivityThread.java:5872)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at java.lang.reflect.Method.invokeNative(Native Method)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at java.lang.reflect.Method.invoke(Method.java:515)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at dalvik.system.NativeStart.main(Native Method)
05-15 10:50:12.123: E/AndroidRuntime(19371): Caused by: android.os.NetworkOnMainThreadException
05-15 10:50:12.123: E/AndroidRuntime(19371):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1236)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at com.android.org.conscrypt.OpenSSLSocketImpl.close(OpenSSLSocketImpl.java:1018)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at org.apache.http.impl.SocketHttpClientConnection.close(SocketHttpClientConnection.java:230)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at org.apache.http.impl.conn.DefaultClientConnection.close(DefaultClientConnection.java:161)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at org.apache.http.impl.conn.tsccm.AbstractConnPool.closeConnection(AbstractConnPool.java:332)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at org.apache.http.impl.conn.tsccm.AbstractConnPool.shutdown(AbstractConnPool.java:301)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.shutdown(ConnPoolByRoute.java:670)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.shutdown    (ThreadSafeClientConnManager.java:256)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at com.parse.ParseRequest.initialize(ParseRequest.java:118)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at com.parse.Parse.initialize(Parse.java:109)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at nl.creationinc.swopp2.Clothing.onCreate(Clothing.java:22)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at android.app.Activity.performCreate(Activity.java:5312)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
05-15 10:50:12.123: E/AndroidRuntime(19371):  ... 11 more

2 个答案:

答案 0 :(得分:0)

你得到的例外是:

android.os.NetworkOnMainThreadException

堆栈跟踪显示原因:

05-15 10:50:12.123: E/AndroidRuntime(19371):  at com.parse.ParseRequest.initialize(ParseRequest.java:118)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at com.parse.Parse.initialize(Parse.java:109)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at nl.creationinc.swopp2.Clothing.onCreate(Clothing.java:22)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at android.app.Activity.performCreate(Activity.java:5312)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
05-15 10:50:12.123: E/AndroidRuntime(19371):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)

Parse.initialize通过SSL执行请求,主线程中不允许这样做,因此您获得了异常。您不应该在UI线程上执行任何Parse操作,而不仅仅是get操作。

答案 1 :(得分:0)

这里是您应该如何获取数据的快照:

ParseQuery<ParseObject> query = ParseQuery.getQuery("Diary_Entry");
        query.whereEqualTo("Pwd", _aa);
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {
                    System.out.println("size of list is  " + objects.size());

                    for (int i = 0; i < objects.size(); i++) {
                        try {

                            _date.add(objects.get(i).fetchIfNeeded()
                                    .getString("Date"));
                                                        myadapter.notifyDataSetChanged();
                        } catch (ParseException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                    }
                } else {
                    System.out.println("bug");
                }
            }


});