--- android/SmsSend/src/dk/thoerup/smssend/SmsSend.java 2010/05/25 19:58:18 750 +++ android/SmsSend/src/dk/thoerup/smssend/SmsSend.java 2010/05/26 07:44:39 751 @@ -1,7 +1,13 @@ package dk.thoerup.smssend; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; +import java.net.URLDecoder; import java.net.URLEncoder; import java.util.ArrayList; @@ -258,29 +264,51 @@ } //"http://sms.coolsmsc.dk:8080/?username=%s&password=%s&to=%s&from=SMS+HTTP+GW&message=hello+world" + + String url = "http://sms.coolsmsc.dk:8080/"; + StringBuilder sb = new StringBuilder(); - sb.append("http://sms.coolsmsc.dk:8080/"); - sb.append("?username=").append(user); + sb.append("username=").append(user); sb.append("&password=").append(pass); sb.append("&to=").append( getTo() ); sb.append("&from=").append( encode(getFrom()) ); sb.append("&message=").append( encode(getMessage()) ); + sb.append("&resulttype=urlencoded"); + sb.append("&lang=en"); Log.v("SmsSend", sb.toString()); + + boolean success = false; + String msg = ""; try { - URL u = new URL( sb.toString() ); - URLConnection conn = u.openConnection(); - conn.getInputStream(); + byte bytes[] = postContent(url, sb.toString(), 2500 ); + String res = new String(bytes); + String parts[] = res.split("&"); + + for (String part : parts) { + String pair[] = part.split("="); + if (pair[0].equals("status")) { + if (pair[1].equals("success")) { + success = true; + } else { + success = false; + } + } + + if (pair[0].equals("result")) { + msg = URLDecoder.decode(pair[1]); + } + } } catch (Exception e) { Log.e("SmsSend", "sendCoolSms", e); - return false; + success = false; } - return true; + return success; } boolean sendSmsDaemon() { @@ -299,6 +327,64 @@ return true; } + + + public static byte[] getContent(String uri, int timeout) throws IOException { + byte buffer[] = new byte[256]; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes + + URL url = new URL(uri); + URLConnection connection = url.openConnection(); + HttpURLConnection c; + connection.setConnectTimeout(timeout); + InputStream is = connection.getInputStream(); + + try { + int count; + while ( (count = is.read(buffer)) != -1) { + baos.write(buffer, 0, count); + } + } finally { + try { + is.close(); + baos.close(); + } catch (Exception e) {} //never mind if close throws an exception + } + + return baos.toByteArray(); + } + + public static byte[] postContent(String uri, String data, int timeout) throws IOException { + byte buffer[] = new byte[256]; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes + + URL url = new URL(uri); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setDoOutput(true); + connection.setRequestMethod("POST"); + connection.setConnectTimeout(timeout); + + OutputStream os = connection.getOutputStream(); + os.write( data.getBytes() ); + + InputStream is = connection.getInputStream(); + + try { + int count; + while ( (count = is.read(buffer)) != -1) { + baos.write(buffer, 0, count); + } + } finally { + try { + is.close(); + baos.close(); + } catch (Exception e) {} //never mind if close throws an exception + } + + return baos.toByteArray(); + } OnItemSelectedListener methodSelected = new OnItemSelectedListener() { public void onItemSelected(AdapterView parent, View view, int pos, long id) { @@ -380,4 +466,4 @@ } -} \ No newline at end of file +}