com.firebase.client.FirebaseException:无法跳转到键入JSON

时间:2016-07-19 11:45:02

标签: android json firebase firebase-realtime-database

我这里有代码..这是

main_menu.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);
        getData();
    }

private void getData(){
        String url = config.DATA_URL+a_username;
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        Log.d(TAG,"MERGING URL AND NAME SUCCESS");
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                showJSON(response);
                Log.d(TAG,"SHOW JSON SUCCESS");
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        requestQueue.add(stringRequest);
}
private void showJSON(String response){

        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray result = jsonObject.getJSONArray(config.JSON_ARRAY);
            JSONObject userid = result.getJSONObject(0);
            id = userid.getString(config.KEY_ID).toString().trim();
            Toast.makeText(getApplicationContext(),id,Toast.LENGTH_LONG).show();
            Log.d(TAG,"GETTING ID SUCCESS");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

@Override
    protected void  onStart(){
        super.onStart();
        real_id = id.toString();
        mref = new Firebase("https://connection-4f6d8.firebaseio.com/"+real_id);
        mref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String text = dataSnapshot.getValue(String.class);
                if(text.equals("UNLOCKED")){
                    sendDataonWIfi("0");
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
    }

Execption:

FATAL EXCEPTION: main Process: com.example.jaypeetan.mftis, PID: 32112com.firebase.client.FirebaseException: Failed to bounce to type
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185)
at com.example.jaypeetan.mftis.main_menu$6.onDataChange(main_menu.java:290)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:56)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)                     at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token
at [Source: java.io.StringReader@22c79a40; line: 1, column: 1]
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:575)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:46)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)                                                                                     ... 13 more

我确实认为错误与JSON有关 我需要的是我的数据库中的用户ID,这是我正确但但问题是当我尝试连接我的firebase它给我这个错误..我试图改变变量 real_id 值为数字并且工作正常但是当它依赖于JSON时它会给我一个错误..请帮助我:D

这是我的firebase的影子 enter image description here

2 个答案:

答案 0 :(得分:0)

请按照new updated Firebase documentation进行操作。

将.JSON文件导入android项目,然后尝试获取此类数据

ValueEventListener postListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // Get Post object and use the values to update the UI
        Post post = dataSnapshot.getValue(Post.class);
        // ...
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // Getting Post failed, log a message
        Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
        // ...
    }
};
mPostReference.child(real_id).addValueEventListener(postListener);

答案 1 :(得分:0)

您将String个数字混合存储在同一个列表中。然后在你的代码中你总是要求一个字符串:

// THIS FAILS WHEN THE SNAPSHOT VALUE IS A NUMBER
String text = dataSnapshot.getValue(String.class);

这不会起作用,因为数字不是字符串(即使它可以转换为一个字符串)。

如果您坚持混合类型,则提取值的代码必须能够处理混合类型。例如:

public void onDataChange(DataSnapshot dataSnapshot) {
    String text = String.valueOf(dataSnapshot.getValue());

通过使用没有类型的dataSnapshot.getValue(),我们将获得存储在快照中的任何类型。然后我们将其转换为String.valueOf()的字符串。

我建议在列表中坚持使用单一统一类型。它会使您的代码更容易出错,更容易理解。