先执行服务器调用再执行下一行代码
execute server call first then next line of code
我想先执行 Toast with text "tex2" 然后执行 Toast with text "text1" 但是
在我的代码中,当我执行下面的代码(我的意思是具有相同结构的不同代码)时,它以相反的顺序打印。
(假设我想等待响应然后执行下一步)
class A
{
public int onStartCommand(Intent intent, int flags, int startId)
{
getdatafromnet();
Toast.makeText(getApplicationContext(), "text1", Toast.LENGTH_LONG).show();
//..................code
}
void getdatafromnet()
{
//volley server call
stringRequest=new StringRequest(Request.Method.GET, url2,
new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
Toast.makeText(getApplicationContext(), "text2",Toast.LENGTH_LONG).show();
}
});
//....follwing volley parameters and calls
}
}
如果您希望在服务器调用后执行某些操作returns。最好将您的代码放在 onResponse 方法中或从 onResponse 方法中调用函数。
由于调用网络是异步的,您无法得到正确的结果。
要处理它,您可以使用 android.os.Handler.
例如;
class A
{
Handler m_handler = new Handler() {
@Override
public void handleMessage(Message inputMessage) {
switch (inputMessage.what) {
case 1:
getdatafromnet();
break;
case 2:
Toast.makeText(getApplicationContext(), "text1", Toast.LENGTH_LONG).show();
//..................code
break;
default:
super.handleMessage(inputMessage);
}
}
};
public int onStartCommand(Intent intent, int flags, int startId)
{
m_handler.sendEmptyMessage(1);
}
void getdatafromnet()
{
//volley server call
stringRequest=new StringRequest(Request.Method.GET, url2,
new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
Toast.makeText(getApplicationContext(), "text2",Toast.LENGTH_LONG).show();
m_handler.sendEmptyMessage(2);
}//..........follwing volley parameters and calls
});
}
我想先执行 Toast with text "tex2" 然后执行 Toast with text "text1" 但是
在我的代码中,当我执行下面的代码(我的意思是具有相同结构的不同代码)时,它以相反的顺序打印。
(假设我想等待响应然后执行下一步)
class A
{
public int onStartCommand(Intent intent, int flags, int startId)
{
getdatafromnet();
Toast.makeText(getApplicationContext(), "text1", Toast.LENGTH_LONG).show();
//..................code
}
void getdatafromnet()
{
//volley server call
stringRequest=new StringRequest(Request.Method.GET, url2,
new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
Toast.makeText(getApplicationContext(), "text2",Toast.LENGTH_LONG).show();
}
});
//....follwing volley parameters and calls
}
}
如果您希望在服务器调用后执行某些操作returns。最好将您的代码放在 onResponse 方法中或从 onResponse 方法中调用函数。
由于调用网络是异步的,您无法得到正确的结果。 要处理它,您可以使用 android.os.Handler.
例如;
class A
{
Handler m_handler = new Handler() {
@Override
public void handleMessage(Message inputMessage) {
switch (inputMessage.what) {
case 1:
getdatafromnet();
break;
case 2:
Toast.makeText(getApplicationContext(), "text1", Toast.LENGTH_LONG).show();
//..................code
break;
default:
super.handleMessage(inputMessage);
}
}
};
public int onStartCommand(Intent intent, int flags, int startId)
{
m_handler.sendEmptyMessage(1);
}
void getdatafromnet()
{
//volley server call
stringRequest=new StringRequest(Request.Method.GET, url2,
new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
Toast.makeText(getApplicationContext(), "text2",Toast.LENGTH_LONG).show();
m_handler.sendEmptyMessage(2);
}//..........follwing volley parameters and calls
});
}