Screen "loading" on Activity

Asked

Viewed 438 times

0

I would like to know or understand how to put a loading on the screen. My app consults on a webservice, and sometimes depending on the connection it takes to bring the information. And the app turns all white. To the end user it seems that stuck. So I would like to put a loading screen.

HTTP request code

    private static final String BASE_URL = "https://LINK DO MEU Json";
private static Retrofit mRetrofit;
private static ApiNOMEDOPROJETORequestInterceptor mInterceptor;
private static Gson mGson;
private static OkHttpClient mClient;

public static Retrofit getInstance() {
    if (mRetrofit == null) {
        //create Gson
        mGson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();

        //create interceptor
        mInterceptor = new ApiNOMEDOPROJETORequestInterceptor();


        //create httpClient with interceptor
        mClient = new OkHttpClient.Builder().addInterceptor(mInterceptor).build();

        //create retrofit
        mRetrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(mGson))
                .client(mClient)
                .build();
    }
    return mRetrofit;
}

}

  • Edit the question and enter the code that queries the webservice.

  • From what I understand, this whole white that the application gets during the procedure is because there is not really information on the screen. Right? If that is not the case, but because the screen is stuck, the solution starts by placing the request to the web service in a separate thread. On the subject of your loading screen, particularly, I would not create a screen for this, but use a Progressbar, displaying when the request starts and hiding when it is finished. Please show us your code as @ramaral requested so we can show you an example.

  • @Youngerauad think what I need is this. I will edit my question and put the request code.

  • You have to capture the data of the webservice in the background, otherwise the screen is stuck waiting to finish the request.

1 answer

1


Good morning Artur, you probably use the async class to make this connection, try using the code below. When you call the class it shows a Dialog for the user, I hope I’ve helped.

public abstract class AsyncData extends AsyncTask<String, String, String> {

    protected final Context mContext;
    protected final String mMsqInfo;
    protected ProgressDialog mDialog;

public AsyncData(String msgInfo, Context context) {
    mContext = context;
    mMsqInfo = msgInfo;
}

@Override
protected void onPreExecute() {
    if (mIsShowDialog){
        mDialog = new ProgressDialog(mContext);
        mDialog.setTitle(R.string.app_label_wait);
        mDialog.setMessage(mMsqInfo);
        mDialog.setCancelable(true);
        mDialog.show();
    }
}



@Override
protected String doInBackground(String... params) {
    // seu código...
}

@Override
protected void onPostExecute(String results) {
    super.onPostExecute(results);
    if ((mDialog != null) && ( mDialog.isShowing()) {

        mDialog.dismiss();
        mDialog = null;
    }
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.