2

I am using okhttp3 as a networking library and Node-mongo as a back-end service.Sometimes when my app starts it shows timeout exception and when I close app and start it again then it fetches data from the server.There is no error in app but I want to know why timeout exception is showing.

Below is my code in which I am showing data in listview.

MainActivity.java

public class Activity2 extends AppCompatActivity {

ListView listView;
List<Data> places;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_2);

    listView  = findViewById(R.id.listView);
    places = new ArrayList<>();

    final ProgressDialog prg = new ProgressDialog(Activity2.this);
    prg.setMessage("Loading...");
    prg.show();

    Log.d("OnCreate","Oncreate started");

    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder().url("https://tiffino.herokuapp.com/test").build();

    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, final IOException e) {

            prg.dismiss();

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),""+e.getMessage(),Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public void onResponse(Call call, final Response response) throws IOException {

            prg.dismiss();

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    try {
                        JSONArray arr = new JSONArray(response.body().string());

                        for(int i = 0;i < arr.length();i++){

                            JSONObject obj = arr.getJSONObject(i);

                            String str1 = obj.getString("Name");

                            Data data = new Data(str1);

                            places.add(data);

                        }

                        PlacesAdapter adapter=  new PlacesAdapter(places,getApplicationContext());
                        listView.setAdapter(adapter);

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

    });

   }
} 

PlacesAdapter.java

public class PlacesAdapter extends ArrayAdapter<Data> {

private List<Data>  places;
private Context ctx;

public PlacesAdapter( List<Data> places, Context ctx) {
    super(ctx,R.layout.places_row,places);
    this.places = places;
    this.ctx = ctx;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    LayoutInflater inflater = LayoutInflater.from(ctx);

    View listView = inflater.inflate(R.layout.places_row,null,true);

    TextView txt = listView.findViewById(R.id.txt);

    Data data = places.get(position);

    txt.setText(data.getPlace());

    return listView;

  }
}

How can I solve this?

Ali Khaki
  • 1,184
  • 1
  • 13
  • 24
Digvijay
  • 2,887
  • 3
  • 36
  • 86

1 Answers1

3

Server timeout will happen if your server is slow or your server's default timeout will be very less.

in this case you can set time-out for your requests like below

client.setConnectTimeout(35, TimeUnit.SECONDS); //change timeout according to your server needs
client.setReadTimeout(35, TimeUnit.SECONDS);
client.setWriteTimeout(35, TimeUnit.SECONDS);

if you are using OkHttp3 then you can use the Builder to achieve this task like below.

client = new OkHttpClient.Builder()
        .connectTimeout(30, TimeUnit.SECONDS)
        .writeTimeout(30, TimeUnit.SECONDS)
        .readTimeout(30, TimeUnit.SECONDS)
        .build();
Jayanth
  • 5,954
  • 3
  • 21
  • 38
  • I have added ths after client object initiated but it showing error that cannot resolve method setConnectTimout(). – Digvijay Nov 02 '18 at 07:47
  • which version of okHttp are you using? – Jayanth Nov 02 '18 at 07:54
  • I am using 'com.squareup.okhttp3:okhttp:3.10.0' – Digvijay Nov 02 '18 at 07:57
  • Here okHttpClient.Builder() and Request .builder() are both different things?? – Digvijay Nov 02 '18 at 08:02
  • In request builder you can specify request parameter like method, header and request body, type etc.. and in client builder you set connection parameters like connection timeouts and read timeouts etc refer here: https://square.github.io/okhttp/3.x/okhttp/okhttp3/OkHttpClient.Builder.html https://square.github.io/okhttp/3.x/okhttp/okhttp3/Request.Builder.html – Jayanth Nov 02 '18 at 08:06
  • Thanks bro, it is working fine perfectly accepted as an answer. – Digvijay Nov 02 '18 at 08:14