현재 내 애플리케이션에서 JSONDownloader를 실행 한 다음 검색된 데이터로 필드를 업데이트하기 전에 일정 기간 대기하도록 애플리케이션에 알립니다. 필드를 업데이트하기 전에 응용 프로그램에서 JSONDownloader가 '작업을 마칠 때까지 기다리는 방법을 알려면 어떻게해야합니까?필드를 업데이트하기 전에 JSON 응답 대기 완료
JSONDowloader 클래스는 다음과 같이 작성 (요약)되며 비동기 활동으로 실행됩니다. 이 클래스는 필요할 때 호출되며, 호출 된 후에 설정된 시간이 지난 후에 전역 변수를 설정합니다. 수업이 끝나 자마자 필드를 업데이트하고 싶습니다.
public class JSONDownloader extends AsyncTask<Object, Object, Object>{
@Override
protected Object doInBackground(Object... params) {
if(JSONstate == false){
try {
final URL url = new URL("https://irrelevant");
final URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
urlConnection.connect();
final InputStream inputStream = urlConnection.getInputStream();
final StringBuilder sb = new StringBuilder();
while (inputStream.available() > 0) {
sb.append((char) inputStream.read());
}
System.out.println("up to setting string");
String result = sb.toString();
JSONObject jsonOrg = new JSONObject(result);
String ok = "ok";
Response = jsonOrg.getString("response");
System.out.println(Response);
if(Response.equals(ok)){
Settingsresponse = true;
orgName = jsonOrg.getString("orgName");
System.out.println("orgName" + orgName);
accessPointName = jsonOrg.getString("accessPointName");
System.out.println("accessPointName" + accessPointName);
lat = jsonOrg.getString("latitude");
System.out.println("lat" + lat);
longi = jsonOrg.getString("longitude");
System.out.println("longi" + longi);
floor = jsonOrg.getString("floor");
System.out.println("floor" + floor);
orgId = jsonOrg.getString("orgId");
System.out.println("orgId" + orgId);
}
else{
System.out.println("Data sent was erroneous");
Settingsresponse = false;
}
} catch (Exception e) {
System.err.print(e);
}
}
return null;
}
protected void onPostExecute(Void result){
}
}
JSONDownloader는 필요한 모든 메서드 내에서 호출 된 다음이 메서드가 호출 된 직후 호출됩니다. 현재 시간 설정은 따라서 필드를 업데이트하기 전에 사용됩니다
public void waitthencall()
{
long t0,t1;
t0=System.currentTimeMillis();
do{
t1=System.currentTimeMillis();
}
while (t1-t0<2000);
setfields();
}
먼저 완료 당신을 확신합니다, 전화, 그것은 클래스의 이름에서 분명하지만 그건 도서관이 아니라 우리가 알아야 할 맞춤형 수업이라고 생각합니다. 둘째, JSONDownloader 작업을 UI 스레드가 아닌 AsyncTask로 실행한다고 가정하므로 스레드가 완료되었는지 확인하고 모든 작업이 완료되면 작업을 수행 할 수 있습니다. 우리가 도울 수 있도록 더 많은 코드를 보여주십시오. – infinity
수정 사항 : JSONDownloader 클래스가 표시됩니다. – Margaret
나머지는'protected void onPostExecute (void 결과) { }'내부에서 처리하십시오. – Nizam