2017-03-10 3 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); 
    if(resp == 204) 
{ 
    new JSONTask().execute(command); 
} 
else 
{ 
//your code here 
} 
} 
0
public class JSONTask extends AsyncTask<String, String, LiveStationModel> 
    { 
    int resp; 

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

지금과 같이 onPostExecute 우선 사용 AsyncTask를의 onPostExecute 방법의 경우 조건.

0

,

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

onPostExecute 메서드에서 새 JSONTask()를 다시 호출하기보다는 예상 결과를 얻고 있는지 확인하십시오. execute (command);

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

처럼이 응답이 204인지 아닌지 확인하지만 것이다 당신이 다른 URL을 다른 연결을 한 후 다른 연결, disconnect 현재를 시도하려는 경우; 왜냐하면 당신이 당신의 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(); 

      .... 

     } 
관련 문제