0

嗨,我在 Android 中有一项服务,可以处理下面指定的 HTTP 方法 POST。现在,我需要调用 Intent

替换资源段()

方法。它有一个处理程序,需要将近 90 秒才能完成任务。在这段时间内,控制退出处理程序块。但我希望我的程序在 POST 处理程序中继续。简而言之,我希望我的服务在 POST 处理程序中暂停一段时间,直到我的 Intent(带有处理程序)完成其执行并且我需要延迟发送 HTTP Post 的响应。有人可以指导我如何执行此操作吗?

    if(method.equals("POST"))
        {   
        conn.receiveRequestEntity((HttpEntityEnclosingRequest)request);             
        HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();          
        String content_type = ""+entity.getContentType();
        JSONReceived = EntityUtils.toString(entity);
        if(content_type.contains("json"))
        {       
        Log.d(TAG,"Content received is: "+JSONReceived);
        bufferedWriter = new BufferedWriter(new FileWriter(new File(getFilesDir()+File.separator+constants.UPDATED_SCRIPT_FILE)));
        bufferedWriter.write(JSONReceived);
        bufferedWriter.close();            
        try {
            parseJSON(JSONReceived);
            replaceResourceSegment(); //Call to an intent with startActivityForResult()
            continueExecution(); //Continue the execution from here                                             
        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG,"IOException line 157");
        }

发回响应的代码:

        HttpResponse postResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
        postResponse.setEntity(new StringEntity("Got it"));
        conn.sendResponseHeader(postResponse);
        conn.sendResponseEntity(postResponse);
4

1 回答 1

0

我设法通过使用默认值为 false 的布尔变量来解决问题。它将定期检查并将控件保存在 POST 方法的处理程序中。

android.os.SystemClock.sleep(30000); //Sleeps for 30 seconds and invoke busy waiting in a thread
Thread syncThread = new Thread(new LoopCheck());
syncThread.start();
synchronized(syncThread)
{
Log.d(TAG,"Inside synchronized blockk");
try
{
syncThread.wait();
}catch(InterruptedException ie){
ie.printStackTrace();
}
}

线程类定义如下:

class LoopCheck extends Thread{
public LoopCheck(){
}
public void run(){
while(true)
{
try {
Thread.sleep(10000);                    
if(write)
{                           
write = false;
synchronized(syncThread)
{
syncThread.notify();
}
break;
}                           
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}       
}
于 2013-01-09T08:54:52.287 回答