【发布时间】:2020-01-20 08:05:49
【问题描述】:
我使用改造来访问 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(??) 中传递什么。
【问题讨论】:
标签: java android-studio firebase-realtime-database retrofit2