Retrofit: Retrofit not call in android pie

Created on 16 Oct 2018  Â·  9Comments  Â·  Source: square/retrofit

What kind of issue is this?

  • [ ] Bug report. If you’ve found a bug, spend the time to write a failing test. Bugs with tests
    get fixed. Here’s an example:

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

    ApiInterface api = retrofit.create(ApiInterface.class);

    Call<ResultModel> call = api.getFileSource(position);
    call.enqueue(new Callback<ResultModel>() {
        @Override
        public void onResponse(Call<ResultModel> call, Response<ResultModel> response) {

            examples = new ArrayList<>();
            examples = response.body().getFileSourceList();

            DownloadFileTask downloadFileTask = new DownloadFileTask(examples);
            downloadFileTask.execute();

            if (pd.isShowing()) {
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    public void run() {
                        pd.dismiss();
                    }
                }, 5000);
            }
        }

        @Override
        public void onFailure(Call<ResultModel> call, Throwable t) {
            if (pd.isShowing()) {
                pd.dismiss();
            }
        }
    });`

Most helpful comment

Just add this line in AndroidManifest.xml

android:usesCleartextTraffic="true"

All 9 comments

Not enough information to reproduce. We're using Pie. Can you isolate into a standalone test case?

Please help me out

ApiInterface.java

`public interface ApiInterface {

String BASE_URL = "http://techhunt00.tk/AndroidExamples/";

@FormUrlEncoded
@POST("example.php")
Call<ResultModel> getExamples(
        @Field("ex_type") String ex_type
);

}`

ExamplesModel.java

`package techhunt.com.androidexamples.Model;

import com.google.gson.annotations.SerializedName;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;

/**

  • Created by User on 21-03-2018.
    */

public class ExamplesModel implements Serializable {

@SerializedName("ex_id")
private int ex_id;

@SerializedName("ex_name")
private String ex_name;

@SerializedName("yt_url")
private String yt_url;

@SerializedName("ex_type")
private String ex_type;

private ArrayList<FileSourceModel> fileSourceModels;

public ArrayList<FileSourceModel> getFileSourceModels() {
    return fileSourceModels;
}

public void setFileSourceModels(ArrayList<FileSourceModel> fileSourceModels) {
    this.fileSourceModels = fileSourceModels;
}

public ExamplesModel(int ex_id, String ex_name, String yt_url, String ex_type) {
    this.ex_id = ex_id;
    this.ex_name = ex_name;
    this.yt_url = yt_url;
    this.ex_type = ex_type;
}

public ExamplesModel(String ex_type) {
    this.ex_type = ex_type;
}

public int getEx_id() {
    return ex_id;
}

public void setEx_id(int ex_id) {
    this.ex_id = ex_id;
}

public String getEx_name() {
    return ex_name;
}

public void setEx_name(String ex_name) {
    this.ex_name = ex_name;
}

public String getYt_url() {
    return yt_url;
}

public void setYt_url(String yt_url) {
    this.yt_url = yt_url;
}

public String getEx_type() {
    return ex_type;
}

public void setEx_type(String ex_type) {
    this.ex_type = ex_type;
}

}`

callMethod

`public void getExamples() {

    if (networkLayout.getVisibility() != View.GONE)
        networkLayout.setVisibility(View.GONE);
    progressDialog = new ProgressDialog(getActivity(), R.style.ProgressDialogTheme);
    ProgressBar spinner = new android.widget.ProgressBar(getActivity(), null,android.R.attr.progressBarStyle);
    spinner.getIndeterminateDrawable().setColorFilter(Color.WHITE, android.graphics.PorterDuff.Mode.SRC_IN);
    String message = "KEEP CALM AND STAY TUNED";
    SpannableString spannableString =  new SpannableString(message);
    CalligraphyTypefaceSpan typefaceSpan = new CalligraphyTypefaceSpan(TypefaceUtils.load(getContext().getAssets(), "fonts/GoogleSans-Bold.ttf"));
    spannableString.setSpan(typefaceSpan, 0, message.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    progressDialog.setContentView(spinner);
    progressDialog.setMessage(spannableString);
    progressDialog.setCancelable(false);
    progressDialog.show();

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

    ApiInterface api = retrofit.create(ApiInterface.class);

    Call<ResultModel> call = api.getExamples("basic");
    call.enqueue(new Callback<ResultModel>() {
        @Override
        public void onResponse(Call<ResultModel> call, Response<ResultModel> response) {

            examples = new ArrayList<>();
            examples = response.body().getExamplesArrayList();
            if (prefManager.isFirstTimeBasic()) {
                PreferenceManager.getInstance().putInt("basic_size", examples.size() - 1);
            } else {
                if (PreferenceManager.getInstance().getInt("basic_size") < examples.size()) {
                    newExamplesArray.clear();
                    for (int i = PreferenceManager.getInstance().getInt("basic_size"); i < examples.size(); i++) {
                        newExamplesArray.add(examples.get(i).getEx_name());
                    }

                    if (newExamplesArray.size() > 0) {
                        notifyNewExamplesDialog(getActivity(), newExamplesArray, examples.size());
                    }
                }
            }
            prefManager.setFirstTimeBasic(false);
            basic = new String[examples.size()];
            for (int i = 0; i < examples.size(); i++) {
                basic[i] = examples.get(i).getEx_name();
                ex_idList.add(examples.get(i).getEx_id());
                ex_titleList.add(examples.get(i).getEx_name());
            }

            progressDialog.dismiss();
            if (getActivity() != null) {
                if (basic != null) {
                    adapter = new ListAdapter(getActivity(), basic);
                    lv.setAdapter(adapter);
                }
            } else {

                String errorCode;
                switch (response.code()) {
                    case 400:
                        errorCode = "404! not found";
                        break;
                    case 500:
                        errorCode = "500! server broken";
                        break;
                    default:
                        errorCode = "Unknown error";
                        break;
                }

                showNetWorkDialog("No Result", "Please Try Again \n" + errorCode);
            }
        }

        @Override
        public void onFailure(Call<ResultModel> call, Throwable t) {
            progressDialog.dismiss();
            boolean isConnected = ConnectivityReceiver.isConnected();
            if (!isConnected) {
                showNetWorkDialog("Ooops!", "Network failure, please try again.");
            } else {
                Snackbar.make(rootView, "Something Wrong", Snackbar.LENGTH_SHORT);
            }
        }
    });
}`

Thank you.

i m also face this problem.any solution?

Android 9.0 disables cleartext traffic by default: https://developer.android.com/about/versions/pie/android-9.0-changes-28#framework-security-changes. Use HTTPS or explicitly enable unsafe HTTP calls.

Just add this line in AndroidManifest.xml

android:usesCleartextTraffic="true"

android:usesCleartextTraffic="true"

thank you .This code is working

Hi            android:usesCleartextTraffic="true". This code is Working. Thanks & Regards, Mahesh, Android Developer, SystimaNX IT Solutions Pvt Ltd, M : +91-8754048942, Confidentiality Notice: This email (including any attachments) is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorised review, use, disclosure, dissemination, forwarding, printing or copying of this email or any action taken in reliance on this e-mail is strictly prohibited and unlawful. If you are not the intended recipient, please notify the sender by reply email and destroy all copies of the original message. Thanks for your cooperation. ---- On Thu, 22 Nov 2018 09:42:31 +0530 TechHunt Developers notifications@github.com wrote ---- Just add this line in AndroidManifest.xml android:usesCleartextTraffic="true" — You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

Thank's

Hii @JakeWharton
Server Timeout Issue :
In Android when I am using the Retofit Api. Server response is getting delay for more than five minutes due to Server slow down.On that time how can I show the message to the user.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bhagyasri picture bhagyasri  Â·  3Comments

zheangrybear picture zheangrybear  Â·  3Comments

kkunsue picture kkunsue  Â·  3Comments

colintheshots picture colintheshots  Â·  3Comments

ramonmoraes8080 picture ramonmoraes8080  Â·  3Comments