can't make synchronous request. waiting for a response from the server

by Nariman Ziyadinov   Last Updated October 19, 2019 10:11 AM - source

I call a method, where a variable is assigned a value.But the server response comes later.And my test variable is null.How to wait for a response from the server?

package com.example.memder;

import android.app.Instrumentation;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.github.kevinsawicki.http.HttpRequest;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import cz.msebera.android.httpclient.Header;

public class MyMemesFragment extends Fragment {
    public String tokenFromStorage;
    private String test;
    MyMemesModel myMemesModel;
    private Boolean returnCall = false;
    private String adressMemes = "https://memnderapi.pythonanywhere.com/memes/api/memesbyuser/?page=";
    public ArrayList<MyMemesModel> list = new ArrayList<>();
    View view;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        InitialDate();

        System.out.println(test);
        view =  inflater.inflate(R.layout.fragment_mymemes,container,false);

        /*RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.List);
        recyclerView.setHasFixedSize(true);
        DataAdapter dataAdapter = new DataAdapter();
        recyclerView.setAdapter(dataAdapter);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);*/




        return view;

    }

    public void InitialDate(){


        SharedPreferences pref = getActivity().getSharedPreferences("token",Context.MODE_PRIVATE);
        tokenFromStorage = pref.getString("token","Token not found");


        final AsyncHttpClient client = new AsyncHttpClient();
        client.addHeader("Authorization", "Token " + tokenFromStorage);


        client.get(adressMemes + 1, new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                System.out.println("______SUCCESS_____");
                JSONObject jsonResponse = null;

                try {
                    jsonResponse = new JSONObject(new String(responseBody));
                    JSONArray memesArr = jsonResponse.getJSONArray("results");
                    for (int i = 0; i < memesArr.length(); i++)
                    {
                        String date = memesArr.getJSONObject(i).getString("date");
                        String img = memesArr.getJSONObject(i).getString("img");
                        int dis = memesArr.getJSONObject(i).getInt("dislikes");
                        int likes = memesArr.getJSONObject(i).getInt("likes");
                        //myMemesModel.createList();
                        list.add(new MyMemesModel(date,img,dis,likes));
                        System.out.println(list.size());
                        //System.out.println(myMemesModel.listInfoImage().get(0));

                        test = "NOT NULL";

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                System.out.println("_____FAIL_____");
            }


        });
    }

}
Tags : applications


Related Questions




Install Google Phone dialer on other phones

Updated January 24, 2019 18:11 PM

Image and Video Files not showing up in Gallery

Updated April 08, 2015 18:04 PM