嗨,我在 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);