I am having an issue handling HTTP URL Connections that do not return data. The code I am using is below... It is basically a messaging client, and this method gets any messages sent to a fake user (bot) and then I am able to manipulate the messages, look for key words, and respond from the bot.
public void getMessages(String bot)
{
xml = "" +
"xmlMessage=<message type=\"comet.get.message.updates\" "
+ "id=\"" + bot + "\" "
+ "password=\"" + password + "\" />";
// Replace spaces (partial url encode).
xml = xml.replace(" ", "%20");
String serverResponse = "";
try
{
// Build URL
url = new URL(botUrl);
request = (HttpURLConnection)url.openConnection();
// Set the Request Method.
request.setRequestMethod("POST");
request.setDoInput(t开发者_高级运维rue);
request.setDoOutput(true);
request.setUseCaches(false);
request.setAllowUserInteraction(false);
request.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");
out = request.getOutputStream();
writer = new OutputStreamWriter(out, "UTF-8");
writer.write(xml);
writer.close();
String temp = "";
buff = new BufferedReader ( new InputStreamReader ( request.getInputStream() ) );
while ( (temp = buff.readLine()) != null )
{
serverResponse = serverResponse + temp;
}
// XML RESPONSE EXAMPLE
// xml = "- <message type=\"comet.message.updates\" id=\"chalkboard.status@fdq.att.com\" count=\"2\">z" +
// "- <contact id=\"jy5740\" />" +
// "<statement text=\"test\" from=\"jy5740\" />" +
// "<statement text=\"testing 123\" from=\"jy5740\" />" +
// "</contact>" +
// "</message>";
}
catch (MalformedURLException ex)
{
System.out.println("Bad URL: " + ex);
}
catch (IOException ex)
{
System.out.println("Connection error: " + ex);
}
// do stuff with the serverResponse string
The method works perfectly if there are messages that have not been received at the time of the method call. The problem is when there have not been any messages since the last check. The method just stays in the while loop until a message is sent to the bot locking my app. How do I determine if there was no response from the server?
The point of comet is "long polling" - that you make a request and it won't complete until there's a real response, or until it times out. In other words, if there are no messages I'd expect the call to readLine
to block for a long time.
If you need to make a request which won't take a long time to time-out, you'll either need to specify a time-out somewhere (possibly at the HTTP level, possibly within the XML content) or use a different call to start with - there may well be a different kind of message used for non-hanging requests.
No idea, but here is my code for HTTP Post:
HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(UPLOAD_URL); MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); try { reqEntity.addPart("param1", new StringBody("yes")); reqEntity.addPart("param2", new StringBody("no")); httppost.setEntity(reqEntity); LOG.debug("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); String urlImageShack = null; if (resEntity != null) { // XML returned by Imageshack String page = EntityUtils.toString(resEntity); LOG.debug("It return: " + page); }
精彩评论