我有一个与 Web 服务交互的 android 应用程序。我从服务下载图像,作为 base64 字符串。这是过程:
::应用程序每 10 秒轮询一次 wifi
::Within wifi > 发送请求以获取任何新图像
::服务器获取请求,处理图像,更新数据库(都相当快)
::Server 将 image 转换为 base64 并返回字符串
::App保存图片
够简单吧?我遇到的问题是应用程序短暂进入 wifi 范围,然后在它得到响应之前退出。这意味着图像被认为是下载到数据库中,而实际上不是(这个系统中的大问题)。
有没有确保响应通过的好方法?我最好的选择是制作第二种网络方法,它更新数据库,只有在下载图像后才从应用程序调用?
我正在使用 SOAP(ksoap2 导入)进行这些调用。
是我放下的方式好,还是有更好的方式?
CallSoap.java:
public String Call(String variable1)
{
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
PropertyInfo pi=new PropertyInfo();
pi.setName("variable1");
pi.setValue(variable1);
pi.setType(String.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
Object response=null;
try
{
httpTransport.call(SOAP_ACTION, envelope);
response = envelope.getResponse();
}
catch (Exception exception)
{
response=exception.toString();
}
return response.toString();
}
调用者.java
public class Caller extends Thread
{
public CallSoap cs;
public String variable1= "variable1";
public void run()
{
try
{
cs=new CallSoap();
String resp=cs.Call(variable1);
MySOAPCallActivity.rslt=resp;
}catch(Exception ex)
{
MySOAPCallActivity.rslt=ex.toString();
}
}
}
主类:
try{
rslt="START";
Caller c=new Caller();
c.variable1=variable1;
c.join();
c.start();
while(rslt=="START")
{
try
{
Thread.sleep(10);
}
catch(Exception ex)
{
}
}
}
catch(Exception ex){
}