繁体   English   中英

Retrofit 无法从 firebase 数据库中获取数据

[英]Retrofit can't get data from firebase database

我使用改造来访问 firebase 数据库,但它只显示“使用 jsonReader.stLenient(true)...”

这是我的 mainactivity.java


public class MainActivity extends AppCompatActivity {
    private TextView textViewResult;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textViewResult=findViewById(R.id.text_view_result);


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://XXXXXX.firebaseio.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        API jsonPlaceHolderApi = retrofit.create(API.class);

        Call<List<user>> call = jsonPlaceHolderApi.getPosts();

        call.enqueue(new Callback<List<user>>() {
            @Override
            public void onResponse(Call<List<user>> call, Response<List<user>> response) {

                if (!response.isSuccessful()) {
                    textViewResult.setText("Code: " + response.code());
                    return;
                }

                List<user> posts = response.body();

                for (user post : posts) {
                    String content = "";
                    content += "name: " + post.getName() + "\n";
                    content += "email: " + post.getEmail() + "\n";
                    content += "userphone: " + post.getPhone() + "\n";


                    textViewResult.append(content);
                }
            }

            @Override
            public void onFailure(Call<List<user>> call, Throwable t) {
                textViewResult.setText(t.getMessage());
            }


        });



    }

}


这是 user.java


public class user {

    private String email;
    private String name;
    private String phone;


    public user(String email, String name, String phone) {
        this.email = email;
        this.name = name;
        this.phone = phone;
    }


    public String getEmail() {
        return email;
    }

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }
}



这是 api.java


package com.example.myapplication;

import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;

public interface API {
    @GET(".")
    Call<List<user>> getPosts();
}

这是 Firebase 数据结构


[ {
  "email" : "xxxxxx@gmail.com",
  "name" : "rrrrr",
  "userphone" : "458793258"
}, {
  "email" : "jjjjjj@gmail.com",
  "name" : "yyyyyy",
  "userphone" : "658976589"
}, {
  "email" : "ooooooo@gmail.com",
  "name" : "llllllll",
  "userphone" : "90890890"
} ]

在这里我使用这些但是每当我尝试运行以显示错误消息时。 每次。 另外,我想知道在 baseurl 中使用 firebase 数据库 URL 时应该在 Get(??) 内部传递什么。

确保你有:

compile 'com.google.code.gson:gson:2.6.1'

在您可以使用此代码之后

Gson gson = new GsonBuilder()
        .setLenient()
        .create();

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM