[英]Waiting for JSON response to finish before updating fields
Currently in my application I execute a JSONDownloader, then tell my application to wait for a set period of time before updating fields with the retrieved data. 当前,在我的应用程序中,我执行JSONDownloader,然后告诉我的应用程序等待一段时间,然后使用检索到的数据更新字段。 How do I go about making the application wait for the JSONDownloader to finish its' task before I update the fields?
在更新字段之前,如何使应用程序等待JSONDownloader完成其任务?
The JSONDowloader class is written (abridged) as follows and is run as an asynchronous activity. JSONwloader类的编写(删节)如下,并作为异步活动运行。 This class is called when needed and sets global variables which, after a set amount of time after being called are updated.
此类在需要时被调用,并设置全局变量,该全局变量在被调用后经过一定时间后将被更新。 I'd like the fields to be updated as soon as the class has finished running.
我希望在课程结束后立即更新字段。
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){
}
}
The JSONDownloader is called within whatever method it is needed and then immediately after this is called; 可以在所需的任何方法中调用JSONDownloader,然后在调用之后立即进行; currently a set time is used before updating fields as thus:
当前,在更新字段之前会使用设置的时间,因此:
public void waitthencall()
{
long t0,t1;
t0=System.currentTimeMillis();
do{
t1=System.currentTimeMillis();
}
while (t1-t0<2000);
setfields();
}
By using asynchronous task! 通过使用异步任务!
http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html
May help! 可能有帮助!
杜德(Dude),一旦doInBackGround完成处理之后,就会调用onPostExecute方法,它将向您保证doInBackGround已完成执行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.