The situation is this: I have to send a message via http, but, whenever the http connection results unavailable (i.e. during a voice call), the message should be switched via sms. http sending alone is ok, sms sending alone is ok. but when I try during a voice call it seems that the http connection gets blocked until the call is terminated, preventing me to switch to sms.
In my first version I wait for the listener methods (listener.handleError and listener.messageSent) to be called and notify a waiting thread:HTML Code:public void run(){ while(fRunning){ synchronized(this){ //wait for the message try{ wait(); } catch(final InterruptedException ie){ } if(szMessage == null) continue; HttpConnection conn = null; OutputStream os = null; try{ conn = (HttpConnection)Connector.open(szURL, Connector.READ_WRITE, true); conn.setRequestProperty("User-Agent", "Profile/" + System.getProperty("microedition.profiles") + " Configuration/" + System.getProperty("microedition.configuration")); conn.setRequestProperty("Connection", "close"); if(szMessageType.equals(HttpConnection.POST)){ //compose a POST message (setup state) conn.setRequestMethod(HttpConnection.POST); if(szMIMETypeContent != null) conn.setRequestProperty("Content-Type", szMIMETypeContent); conn.setRequestProperty("Connection", "close"); if(szMIMETypeResponse != null) conn.setRequestProperty("Accept", szMIMETypeResponse); conn.setRequestProperty("Content-Length", "" + szMessage.length()); //(transition to connected state) os = conn.openOutputStream(); os.write(szMessage.getBytes()); try{ os.close(); } catch(final Exception e){ } os = null; } else{ fMessageEnqueued = false; throw new Exception("Message type not allowed to be sent"); } final StringBuffer responseMsg = new StringBuffer(); final int nResponseCode = readResponseCode(conn, responseMsg); if(nResponseCode == HttpConnection.HTTP_OK){ fMessageEnqueued = false; fMessageSent = true; if(listener != null){ listener.messageSent(conn, Integer.toString(nResponseCode)); listener.messageReceived(conn, responseMsg.toString()); } break; } else{ fMessageEnqueued = false; throw new Exception("Unexpected return code: " + Integer.toString(nResponseCode) + (responseMsg.length() > 0? " - " + responseMsg.toString(): "")); } } catch(final SecurityException se){ fMessageEnqueued = false; fDisallowFurtherSending = true; if(listener != null) listener.handleError(conn, se, "Unwanted HTTP connectivity"); } catch(final ConnectionNotFoundException cnfe){ fMessageEnqueued = false; if(listener != null) listener.handleError(conn, cnfe, "Unsupported protocol, or no connectivity"); } catch(final ClassCastException cce){ fMessageEnqueued = false; if(listener != null) listener.handleError(conn, cce, "'" + szURL + "' is not a valid HTTP URL"); } catch(final Exception e){ fMessageEnqueued = false; if(listener != null) listener.handleError(conn, e, "URL connection error"); } //(transition to closed state) if(conn != null){ try{ conn.close(); } catch(final Exception e){ } conn = null; } szMessage = null; } } } public void sendPostMessage(final String szMessage, final String szMIMETypeContent, final String szMIMETypeResponse){ synchronized(this){ fMessageEnqueued = true; fMessageSent = false; fDisallowFurtherSending = false; this.szMessage = szMessage; this.szMIMETypeContent = szMIMETypeContent; this.szMIMETypeResponse = szMIMETypeResponse; szMessageType = HttpConnection.POST; notify(); } }
then, I tried with a loop:HTML Code:httpManager.sendPostMessage("blah", HTTPManager.MIMETYPE_APP_FORM_ENCODED, HTTPManager.MIMETYPE_ANY); //wait for response synchronized(responseMutex){ try{ responseMutex.wait(); } catch(final InterruptedException ie){ } } public void messageSent(final Connection conn, final Object message){ synchronized(responseMutex){ responseMutex.notify(); } }
but nothing seems to work... anyone has any hints for me?HTML Code:httpManager.sendPostMessage("blah", HTTPManager.MIMETYPE_APP_FORM_ENCODED, HTTPManager.MIMETYPE_ANY); final long lEndTime = System.currentTimeMillis() + 6000; while(httpManager.isMessageEnqueued() && System.currentTimeMillis() < lEndTime){ try{ Thread.sleep(100); } catch(final InterruptedException ie){ } } final boolean fMessageSent = (!httpManager.isMessageEnqueued() && httpManager.isMessageSent());

Reply With Quote

