Here's an example method that uses my PostHttp class (see below) to send a Sprint Notification:
[code:1:90f1837771] String sendSprintNotification(String from, String to, String body) {
PostHttp ph = new PostHttp();
ph.setParameter("post", "Send");
ph.setParameter("recipientsAsString", to);
ph.setParameter("callback", from);
ph.setParameter("Submit", "Submit+Query");
ph.setParameter("body", body);
return(ph.submit());
}[/code:1:90f1837771]
Here is my PostHttp class:
[code:1:90f1837771]/*
* @(#)PostHttp.java 7-Apr-2003 by William Frantz
* No Copyright. All Rights Freely Given Away.
* Give credit where credit is due.
*
* THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES
* ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR
* SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
* LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*/
import javax.microedition.io.*;
import java.io.*;
public class PostHttp {
static String protocol;
static String host;
static String path;
static String document;
static String postData;
public PostHttp() {
// These are the default values.
// If none of the "set" methods are called, these values are used.
// Note this class could be used to submit data to any POST form.
protocol = "http://";
host = "messaging.sprintpcs.com";
path ="/sml/";
document = "guestCompose.do";
postData = "";
}
public void setProtocol(String p) {
protocol = p;
}
public void setHost(String h) {
host = h;
}
public void setPath(String p) {
path = p;
}
public void setDocument(String d) {
document = d;
}
public void setParameter(String parameter, String value) {
// There could be a problem here with special characters like '&'
if (postData.length() > 0) {
postData = postData + "&";
}
postData = postData + parameter + "=" + value;
}
HttpConnection setupConnection(String uri) throws IOException {
HttpConnection connection = null;
connection = (HttpConnection)Connector.open(uri,
Connector.READ_WRITE);
connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Host", host);
return(connection);
}
void sendPostData(HttpConnection connection,
String postData) throws IOException {
OutputStream outputStream = null;
outputStream = connection.openOutputStream();
outputStream.write(postData.getBytes());
outputStream.close();
}
String readResult(HttpConnection connection) throws IOException {
InputStreamReader inputStream;
String result = "";
final int arbitraryBufferSize = 100;
final int endOfData = -1;
char[] characterBuffer = new char[arbitraryBufferSize];
int bytesRead;
inputStream = new InputStreamReader(connection.openInputStream());
bytesRead = inputStream.read(characterBuffer);
while (bytesRead != endOfData) {
result = result + new String(characterBuffer);
bytesRead = inputStream.read(characterBuffer);
}
inputStream.close();
return(result);
}
public String submit() {
HttpConnection connection;
String result;
try {
connection = setupConnection(protocol+host+path+document);
sendPostData(connection, postData);
result = readResult(connection);
connection.close();
} catch (IOException ioe) {
result = "IOException!";
}
return(result);
}
}[/code:1:90f1837771]