我正在制作分类市场安卓应用程序。因此,我必须在我的主页上显示人们完成的广告或帖子,一切正常,我正在邮递员上获得所有帖子,但没有在我的回收视图中。
public class SearchFragment extends Fragment {
private static final String TAG = "SearchFragment";
private static final String BASE_URL = "http://35.188.133.114//elasticsearch/posts/post/";
private static final int NUM_GRID_COLUMNS = 2;
private static final int GRID_ITEM_MARGIN = 5;
//widgets
private ImageView mFilters;
private EditText mSearchText;
private FrameLayout mFrameLayout;
//vars
private String mElasticSearchPassword;
private String mPrefCity;
private String mPrefStateProv;
//private String mPrefCountry;
private String mPrefCollege;
private ArrayList<Post> mPosts;
private RecyclerView mRecyclerView;
private PostListAdapter mAdapter;
private ListView listView;
private String currentuser;
private String user;
private ImageView filter;
ArrayList<Card> list = new ArrayList<>();
FloatingActionButton fab;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search, container, false);
mFilters = (ImageView) view.findViewById(R.id.ic_search);
mSearchText = (EditText) view.findViewById(R.id.input_search);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
mFrameLayout = (FrameLayout) view.findViewById(R.id.container);
filter = (ImageView) view.findViewById(R.id.filter);
currentuser = FirebaseAuth.getInstance().getUid();
filter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),FiltersActivity.class);
startActivity(intent);
}
});
getElasticSearchPassword();
welcome();
init();
return view;
}
private void setupPostsList(){
RecyclerViewMargin itemDecorator = new RecyclerViewMargin(GRID_ITEM_MARGIN, NUM_GRID_COLUMNS);
mRecyclerView.addItemDecoration(itemDecorator);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), NUM_GRID_COLUMNS);
mRecyclerView.setLayoutManager(gridLayoutManager);
mAdapter = new PostListAdapter(getActivity(), mPosts);
mRecyclerView.setAdapter(mAdapter);
// CustomListAdapter adapter = new CustomListAdapter(getActivity(), R.layout.card_layout_main, list);
}
private void welcome() {
if (!currentuser.equals("")) {
mPosts = new ArrayList<Post>();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ElasticSearchAPI searchAPI = retrofit.create(ElasticSearchAPI.class);
HashMap<String, String> headerMap = new HashMap<String, String>();
headerMap.put("Authorization", Credentials.basic("user", mElasticSearchPassword));
// String searchString = "*";
/* if(!mSearchText.equals("")){
searchString = searchString + mSearchText.getText().toString() + "*";
}*/
/* if(!mPrefCity.equals("")){
searchString = searchString + " city:" + mPrefCity;
}
if(!mPrefStateProv.equals("")){
searchString = searchString + " state_province:" + mPrefStateProv;
}*/
/* if(!mPrefCollege.equals("")){
searchString = searchString + " college:" + mPrefCollege;
}*/
/*if(!currentuser.equals("")){
searchString = searchString + " user_id:" + currentuser;
}*/
Call<HitsObject> call = searchAPI.search(headerMap, "AND", "*");
call.enqueue(new Callback<HitsObject>() {
@Override
public void onResponse(Call<HitsObject> call, Response<HitsObject> response) {
HitsList hitsList = new HitsList();
String jsonResponse = "";
try {
Log.d(TAG, "onResponse: server response: " + response.toString());
if (response.isSuccessful()) {
hitsList = response.body().getHits();
} else {
jsonResponse = response.errorBody().string();
}
Log.d(TAG, "onResponse: hits: " + hitsList);
for (int i = 0; i < hitsList.getPostIndex().size(); i++) {
Log.d(TAG, "onResponse: data: " + hitsList.getPostIndex().get(i).getPost().toString());
mPosts.add(hitsList.getPostIndex().get(i).getPost());
}
Log.d(TAG, "onResponse: size: " + mPosts.size());
//setup the list of posts
setupPostsList();
} catch (NullPointerException e) {
Log.e(TAG, "onResponse: NullPointerException: " + e.getMessage());
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, "onResponse: IndexOutOfBoundsException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "onResponse: IOException: " + e.getMessage());
}
}
@Override
public void onFailure(Call<HitsObject> call, Throwable t) {
Log.e(TAG, "onFailure: " + t.getMessage());
Toast.makeText(getActivity(), "search failed", Toast.LENGTH_SHORT).show();
}
});
}
// return false;
}
private void init(){
mFilters.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPosts = new ArrayList<Post>();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ElasticSearchAPI searchAPI = retrofit.create(ElasticSearchAPI.class);
HashMap<String, String> headerMap = new HashMap<String, String>();
headerMap.put("Authorization", Credentials.basic("user", mElasticSearchPassword));
String searchString = "";
if(!mSearchText.equals("")){
searchString = searchString + mSearchText.getText().toString() + "*";
}
/* if(!mPrefCity.equals("")){
searchString = searchString + " city:" + mPrefCity;
}
if(!mPrefStateProv.equals("")){
searchString = searchString + " state_province:" + mPrefStateProv;
}*/
if(!mPrefCollege.equals("")){
searchString = searchString + " college:" + mPrefCollege;
}
/*if(!currentuser.equals("")){
searchString = searchString + " user_id:" + currentuser;
}*/
Call<HitsObject> call = searchAPI.search(headerMap, "AND", searchString);
call.enqueue(new Callback<HitsObject>() {
@Override
public void onResponse(Call<HitsObject> call, Response<HitsObject> response) {
HitsList hitsList = new HitsList();
String jsonResponse = "";
try{
Log.d(TAG, "onResponse: server response: " + response.toString());
if(response.isSuccessful()){
hitsList = response.body().getHits();
}else{
jsonResponse = response.errorBody().string();
}
Log.d(TAG, "onResponse: hits: " + hitsList);
for(int i = 0; i < hitsList.getPostIndex().size(); i++){
Log.d(TAG, "onResponse: data: " + hitsList.getPostIndex().get(i).getPost().toString());
mPosts.add(hitsList.getPostIndex().get(i).getPost());
}
Log.d(TAG, "onResponse: size: " + mPosts.size());
//setup the list of posts
setupPostsList();
}catch (NullPointerException e){
Log.e(TAG, "onResponse: NullPointerException: " + e.getMessage() );
}
catch (IndexOutOfBoundsException e){
Log.e(TAG, "onResponse: IndexOutOfBoundsException: " + e.getMessage() );
}
catch (IOException e){
Log.e(TAG, "onResponse: IOException: " + e.getMessage() );
}
}
@Override
public void onFailure(Call<HitsObject> call, Throwable t) {
Log.e(TAG, "onFailure: " + t.getMessage() );
Toast.makeText(getActivity(), "search failed", Toast.LENGTH_SHORT).show();
}
});
}
// return false;
});
}
welcome(( 方法没有被调用,因为我看不到任何帖子,但init((方法正在工作并在单击搜索按钮时显示所有帖子。并且两种方法都使用相同的代码,这是我所做的查询的唯一区别,不知道为什么welcome((方法没有显示任何帖子。
实际上,你的方法是从onCreate()
调用的,但你的条件(!currentuser.equals("")
返回假。
查看currentuser = FirebaseAuth.getInstance().getUid();
的值
仅当您对用户进行身份验证时,您才会将其显示为不为空,否则它将为空或空。
这是 Firbase 问题的问题,请检查 Firebase 实例是否已在您的应用程序类中正确配置和初始化。然后,您应该无法获取当前用户=="。
如果用户令牌/身份验证无效且用户信息无效,Firebase 将返回401/410
错误代码。