async vazifasida if ixtiyoriy qo'ng'iroq qilish kerakmi?

Iltimos, javob berish kodi 204 bo'lsa qanday qilib qo'ng'iroq qilish kerak bo'lsa, serverni qayta chaqirishga yordam bering?

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
{
    LiveStationModel liveStationModel = null;
    protected LiveStationModel doInBackground(String... params) {
        IOException e;
        MalformedURLException e2;
        List 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);
                }
0
Bu javob sizga yordam berishi mumkin ...! stackoverflow.com/a/6374135/7316675
qo'shib qo'ydi muallif nTri, manba
Bu javob sizga yordam berishi mumkin ...! stackoverflow.com/a/6374135/7316675
qo'shib qo'ydi muallif nTri, manba

10 javoblar

onPostExecute usulini tekshirishingiz mumkin:

@Override
protected void onPostExecute(Void aVoid) {
     super.onPostExecute(aVoid);
     //check your if condition here
}
0
qo'shib qo'ydi

onPostExecute usulini tekshirishingiz mumkin:

@Override
protected void onPostExecute(Void aVoid) {
     super.onPostExecute(aVoid);
     //check your if condition here
}
0
qo'shib qo'ydi

Bu javobning 204 yoki yo'qligini tekshirib ko'radi, lekin boshqa ulanishni sinab ko'rishni istasangiz, disconnect joriy tanangizni tanlang va boshqa url bilan boshqa ulanishni amalga oshiring; chunki siz URL manzilingizni takrorlasangiz, siz yana bir bor (NO_CONTENT) bir xil natijalarni qo'lga kiritasiz.

public class JSONTask extends AsyncTask
{
    LiveStationModel liveStationModel = null;
    protected LiveStationModel doInBackground(String... params) {
        IOException e;
        MalformedURLException e2;
        List 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
            }

agar siz kontentni olmaguningizcha takrorlashni xohlasangiz quyidagi kabi pastadir foydalaning:

    try {

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

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

            ....

        }
0
qo'shib qo'ydi

Bu javobning 204 yoki yo'qligini tekshirib ko'radi, lekin boshqa ulanishni sinab ko'rishni istasangiz, disconnect joriy tanangizni tanlang va boshqa url bilan boshqa ulanishni amalga oshiring; chunki siz URL manzilingizni takrorlasangiz, siz yana bir bor (NO_CONTENT) bir xil natijalarni qo'lga kiritasiz.

public class JSONTask extends AsyncTask
{
    LiveStationModel liveStationModel = null;
    protected LiveStationModel doInBackground(String... params) {
        IOException e;
        MalformedURLException e2;
        List 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
            }

agar siz kontentni olmaguningizcha takrorlashni xohlasangiz quyidagi kabi pastadir foydalaning:

    try {

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

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

            ....

        }
0
qo'shib qo'ydi

AsyncTask ning onPostExecute usulida if shartini ishlating.

0
qo'shib qo'ydi

AsyncTask ning onPostExecute usulida if shartini ishlating.

0
qo'shib qo'ydi

Agar siz yangi JSONTask() ni chaqirmasangiz, onPostExecute usulida siz kutilgan natijaga erishasiz yoki yo'qligini tekshirasiz.

kabi

@Override
protected void onPostExecute(Void aVoid) {
     super.onPostExecute(aVoid);
   new JSONTask().execute(command);
}
0
qo'shib qo'ydi

Agar siz yangi JSONTask() ni chaqirmasangiz, onPostExecute usulida siz kutilgan natijaga erishasiz yoki yo'qligini tekshirasiz.

kabi

@Override
protected void onPostExecute(Void aVoid) {
     super.onPostExecute(aVoid);
   new JSONTask().execute(command);
}
0
qo'shib qo'ydi
   public class JSONTask extends AsyncTask
    {
    int resp;

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

Endi, bu kabi onPostExecute-ni bekor qilinsa,

@Override
protected void onPostExecute(Void aVoid) {
     super.onPostExecute(aVoid);
     if(resp == 204)
{
     new JSONTask().execute(command);
}
else
{
//your code here
}
}
0
qo'shib qo'ydi
   public class JSONTask extends AsyncTask
    {
    int resp;

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

Endi, bu kabi onPostExecute-ni bekor qilinsa,

@Override
protected void onPostExecute(Void aVoid) {
     super.onPostExecute(aVoid);
     if(resp == 204)
{
     new JSONTask().execute(command);
}
else
{
//your code here
}
}
0
qo'shib qo'ydi