0

こんにちは私は以下に指定されているようにHTTPメソッドPOSTを処理するAndroidのサービスを持っています。今、私はインテントを呼び出す必要があります

replaceResourceSegment()

方法。タスクを完了するのに約90秒かかるハンドラーがあります。その時間内に、制御はハンドラーブロックを終了します。しかし、POSTのハンドラー内でプログラムを続行したい。つまり、インテント(ハンドラー付き)が実行を完了し、HTTP Postの応答の送信を遅らせる必要があるまで、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 に答える