0

Please help me it should call again the server if the response code is 204 how to call?

String command = ("http://api.railwayapi.com/live/train/" + m_train_Number + "/doj/" + m_year + m_month + m_day + "/apikey/tc9sc898/");
new JSONTask().execute(command);

public class JSONTask extends AsyncTask<String, String, LiveStationModel>
{
    LiveStationModel liveStationModel = null;
    protected LiveStationModel doInBackground(String... params) {
        IOException e;
        MalformedURLException e2;
        List<LiveStationModel> myList = null;
        Throwable th;
        JSONException e3;
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        try {
            connection = (HttpURLConnection) new URL(params[0]).openConnection();
            connection.connect();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            try {
                StringBuffer buffer = new StringBuffer();
                String str = "";
                while (true) {
                    str = bufferedReader.readLine();
                    if (str == null) {
                        break;
                    }
                    buffer.append(str);
                }
PravinS
  • 2,640
  • 3
  • 21
  • 25

5 Answers5

0

your can check that it onPostExecute method like:

@Override
protected void onPostExecute(Void aVoid) {
     super.onPostExecute(aVoid);
     //check your if condition here
}
AwaisMajeed
  • 2,254
  • 2
  • 10
  • 25
0
   public class JSONTask extends AsyncTask<String, String, LiveStationModel>
    {
    int resp;

                -------
                connection.connect();
                resp = connection.getResponseCode();
                --------
                    }

Now override onPostExecute like this,

@Override
protected void onPostExecute(Void aVoid) {
     super.onPostExecute(aVoid);
     if(resp == 204)
{
     new JSONTask().execute(command);
}
else
{
//your code here
}
}
MUKUND U
  • 44
  • 6
0

Use the if condition in onPostExecute method of AsyncTask.

R.R.M
  • 780
  • 4
  • 10
0

in your onPostExecute method check you are getting expected result or not, if not than again call your new JSONTask().execute(command);

like

@Override
protected void onPostExecute(Void aVoid) {
     super.onPostExecute(aVoid);
   new JSONTask().execute(command);
}
Lovekush Vishwakarma
  • 3,035
  • 23
  • 25
0

This will check if the response is 204 or not but if you want to try another connection, disconnect current one and then make another connection with different url; because if you repeat your URL, you get the same results again and again(NO_CONTENT).

public class JSONTask extends AsyncTask<String, String, LiveStationModel>
{
    LiveStationModel liveStationModel = null;
    protected LiveStationModel doInBackground(String... params) {
        IOException e;
        MalformedURLException e2;
        List<LiveStationModel> myList = null;
        Throwable th;
        JSONException e3;
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        try {
            connection = (HttpURLConnection) new URL(params[0]).openConnection();
            connection.connect();

            if(connection.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT)
            {
                //do what you gonna do
            }

if you want to repeat until you get the content use a loop like this:

    try {

        int code = connection.getResponseCode();
        while(code == 204)
        {

            connection = (HttpURLConnection) new URL(params[?]).openConnection();   // Different Parameter here
            connection.connect();

            ....

        }
Mehran Zamani
  • 831
  • 9
  • 31