Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrofit not call in android pie #2925

Closed
1 task
coderspacedev opened this issue Oct 16, 2018 · 9 comments
Closed
1 task

Retrofit not call in android pie #2925

coderspacedev opened this issue Oct 16, 2018 · 9 comments

Comments

@coderspacedev
Copy link

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();
            }
        }
    });`
@swankjesse
Copy link
Member

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

@coderspacedev
Copy link
Author

coderspacedev commented Oct 18, 2018

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.

@maheshsystimanx
Copy link

i m also face this problem.any solution?

@JakeWharton
Copy link
Member

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.

@coderspacedev
Copy link
Author

Just add this line in AndroidManifest.xml

android:usesCleartextTraffic="true"

@maheshsystimanx
Copy link

maheshsystimanx commented Nov 22, 2018

android:usesCleartextTraffic="true"

thank you .This code is working

@maheshsystimanx
Copy link

maheshsystimanx commented Nov 22, 2018 via email

@TechNov
Copy link

TechNov commented Nov 9, 2019

Thank's

@gokul-kumar96
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants