2017-03-10 9 views
0

応答コードが204の場合は、サーバーを再度呼び出す必要がありますか?非同期タスクでif文を呼び出す方法は?

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); 
       } 
+0

この答えはあなたを助けるかもしれません...! http://stackoverflow.com/a/6374135/7316675 – nTri

答えて

0

あなたはそれのようなonPostExecute方法かどうかを確認することができます

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

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

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

はAsyncTaskのonPostExecute方法であれば条件を使用して、このようonPostExecuteオーバーライドします。

0

onPostExecuteメソッドで、新しいJSONTask()を再度呼び出すのではなく、期待どおりの結果を得ているかどうかを確認してください。応答が204であるかどうか

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

よう

は、これはチェックしますが、あなたは別の接続を試してみたい場合は、disconnect現在の1、その後、別のURLで別の接続を行います。あなたのURLを繰り返すと同じ結果が何度も何度も繰り返されるからです(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 
      } 

あなたはコンテンツ はこのようにループを使用して取得するまで繰り返したい場合:

try { 

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

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

      .... 

     } 
関連する問題