1 |
package dk.thoerup.smssend; |
package dk.thoerup.smssend; |
2 |
|
|
3 |
|
import java.io.ByteArrayOutputStream; |
4 |
|
import java.io.IOException; |
5 |
|
import java.io.InputStream; |
6 |
|
import java.io.OutputStream; |
7 |
|
import java.net.HttpURLConnection; |
8 |
import java.net.URL; |
import java.net.URL; |
9 |
import java.net.URLConnection; |
import java.net.URLConnection; |
10 |
|
import java.net.URLDecoder; |
11 |
import java.net.URLEncoder; |
import java.net.URLEncoder; |
12 |
import java.util.ArrayList; |
import java.util.ArrayList; |
13 |
|
|
264 |
} |
} |
265 |
|
|
266 |
//"http://sms.coolsmsc.dk:8080/?username=%s&password=%s&to=%s&from=SMS+HTTP+GW&message=hello+world" |
//"http://sms.coolsmsc.dk:8080/?username=%s&password=%s&to=%s&from=SMS+HTTP+GW&message=hello+world" |
267 |
|
|
268 |
|
String url = "http://sms.coolsmsc.dk:8080/"; |
269 |
|
|
270 |
StringBuilder sb = new StringBuilder(); |
StringBuilder sb = new StringBuilder(); |
271 |
sb.append("http://sms.coolsmsc.dk:8080/"); |
sb.append("username=").append(user); |
|
sb.append("?username=").append(user); |
|
272 |
sb.append("&password=").append(pass); |
sb.append("&password=").append(pass); |
273 |
sb.append("&to=").append( getTo() ); |
sb.append("&to=").append( getTo() ); |
274 |
sb.append("&from=").append( encode(getFrom()) ); |
sb.append("&from=").append( encode(getFrom()) ); |
275 |
sb.append("&message=").append( encode(getMessage()) ); |
sb.append("&message=").append( encode(getMessage()) ); |
276 |
|
sb.append("&resulttype=urlencoded"); |
277 |
|
sb.append("&lang=en"); |
278 |
|
|
279 |
Log.v("SmsSend", sb.toString()); |
Log.v("SmsSend", sb.toString()); |
280 |
|
|
281 |
|
|
282 |
|
boolean success = false; |
283 |
|
String msg = ""; |
284 |
try { |
try { |
285 |
URL u = new URL( sb.toString() ); |
byte bytes[] = postContent(url, sb.toString(), 2500 ); |
286 |
URLConnection conn = u.openConnection(); |
String res = new String(bytes); |
287 |
conn.getInputStream(); |
String parts[] = res.split("&"); |
288 |
|
|
289 |
|
for (String part : parts) { |
290 |
|
String pair[] = part.split("="); |
291 |
|
if (pair[0].equals("status")) { |
292 |
|
if (pair[1].equals("success")) { |
293 |
|
success = true; |
294 |
|
} else { |
295 |
|
success = false; |
296 |
|
} |
297 |
|
} |
298 |
|
|
299 |
|
if (pair[0].equals("result")) { |
300 |
|
msg = URLDecoder.decode(pair[1]); |
301 |
|
} |
302 |
|
} |
303 |
|
|
304 |
|
|
305 |
|
|
306 |
} catch (Exception e) { |
} catch (Exception e) { |
307 |
Log.e("SmsSend", "sendCoolSms", e); |
Log.e("SmsSend", "sendCoolSms", e); |
308 |
return false; |
success = false; |
309 |
} |
} |
310 |
|
|
311 |
return true; |
return success; |
312 |
} |
} |
313 |
|
|
314 |
boolean sendSmsDaemon() { |
boolean sendSmsDaemon() { |
327 |
|
|
328 |
return true; |
return true; |
329 |
} |
} |
330 |
|
|
331 |
|
|
332 |
|
public static byte[] getContent(String uri, int timeout) throws IOException { |
333 |
|
byte buffer[] = new byte[256]; |
334 |
|
|
335 |
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes |
336 |
|
|
337 |
|
URL url = new URL(uri); |
338 |
|
URLConnection connection = url.openConnection(); |
339 |
|
HttpURLConnection c; |
340 |
|
connection.setConnectTimeout(timeout); |
341 |
|
InputStream is = connection.getInputStream(); |
342 |
|
|
343 |
|
try { |
344 |
|
int count; |
345 |
|
while ( (count = is.read(buffer)) != -1) { |
346 |
|
baos.write(buffer, 0, count); |
347 |
|
} |
348 |
|
} finally { |
349 |
|
try { |
350 |
|
is.close(); |
351 |
|
baos.close(); |
352 |
|
} catch (Exception e) {} //never mind if close throws an exception |
353 |
|
} |
354 |
|
|
355 |
|
return baos.toByteArray(); |
356 |
|
} |
357 |
|
|
358 |
|
public static byte[] postContent(String uri, String data, int timeout) throws IOException { |
359 |
|
byte buffer[] = new byte[256]; |
360 |
|
|
361 |
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes |
362 |
|
|
363 |
|
URL url = new URL(uri); |
364 |
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
365 |
|
connection.setDoOutput(true); |
366 |
|
connection.setRequestMethod("POST"); |
367 |
|
connection.setConnectTimeout(timeout); |
368 |
|
|
369 |
|
OutputStream os = connection.getOutputStream(); |
370 |
|
os.write( data.getBytes() ); |
371 |
|
|
372 |
|
InputStream is = connection.getInputStream(); |
373 |
|
|
374 |
|
try { |
375 |
|
int count; |
376 |
|
while ( (count = is.read(buffer)) != -1) { |
377 |
|
baos.write(buffer, 0, count); |
378 |
|
} |
379 |
|
} finally { |
380 |
|
try { |
381 |
|
is.close(); |
382 |
|
baos.close(); |
383 |
|
} catch (Exception e) {} //never mind if close throws an exception |
384 |
|
} |
385 |
|
|
386 |
|
return baos.toByteArray(); |
387 |
|
} |
388 |
|
|
389 |
OnItemSelectedListener methodSelected = new OnItemSelectedListener() { |
OnItemSelectedListener methodSelected = new OnItemSelectedListener() { |
390 |
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { |
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { |
466 |
|
|
467 |
} |
} |
468 |
|
|
|
} |
|
469 |
|
} |