/[projects]/android/SmsSend/src/dk/thoerup/smssend/SmsSend.java
ViewVC logotype

Diff of /android/SmsSend/src/dk/thoerup/smssend/SmsSend.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 753 by torben, Wed May 26 07:55:04 2010 UTC revision 754 by torben, Wed May 26 14:43:20 2010 UTC
# Line 10  import java.net.URLConnection; Line 10  import java.net.URLConnection;
10  import java.net.URLDecoder;  import java.net.URLDecoder;
11  import java.net.URLEncoder;  import java.net.URLEncoder;
12  import java.util.ArrayList;  import java.util.ArrayList;
13    import java.util.HashMap;
14    import java.util.Map;
15    
16  import android.app.Activity;  import android.app.Activity;
17  import android.app.ProgressDialog;  import android.app.ProgressDialog;
# Line 240  public class SmsSend extends Activity { Line 242  public class SmsSend extends Activity {
242                  return true;                  return true;
243          }          }
244                    
245          String encode(String url) {  
                 try {  
                         return URLEncoder.encode(url, "UTF-8");  
                 } catch (Exception e) {  
                         return url;  
                 }  
         }  
246    
247          boolean sendCoolSms() {          boolean sendCoolSms() {
248                  SharedPreferences prefs = this.getSharedPreferences("SmsSend", MODE_PRIVATE);                  SharedPreferences prefs = this.getSharedPreferences("SmsSend", MODE_PRIVATE);
# Line 267  public class SmsSend extends Activity { Line 263  public class SmsSend extends Activity {
263                                    
264                  String url = "http://sms.coolsmsc.dk:8080/";                  String url = "http://sms.coolsmsc.dk:8080/";
265                                    
266                  StringBuilder sb = new StringBuilder();                  Map<String,String> params = new HashMap<String,String>();
267                  sb.append("username=").append(user);                  params.put("username", user);
268                  sb.append("&password=").append(pass);                  params.put("password", pass);
269                  sb.append("&to=").append( getTo() );                  params.put("to", getTo());
270                  sb.append("&from=").append( encode(getFrom()) );                  params.put("from", getFrom());
271                  sb.append("&message=").append( encode(getMessage()) );                  params.put("message", getMessage() );
272                  sb.append("&resulttype=urlencoded");                  params.put("resulttype", "urlencoded" );
273                  sb.append("&lang=en");                  params.put("lang", "lang" );
274                    
275                    String paramStr = HttpUtil.encodeParams(params);
276                                    
277                  Log.v("SmsSend", sb.toString());                  Log.v("SmsSend", paramStr);
278                                    
279                                    
280                  boolean success = false;                  boolean success = false;
281                  String msg = "";                  String msg = "";
282                  try {                  try {
283                          byte bytes[] = postContent(url,  sb.toString(), 2500 );                          byte bytes[] = HttpUtil.postContent(url,  paramStr, 2500 );
284                          String res = new String(bytes);                          String res = new String(bytes);
285                          String parts[] = res.split("&");                          Map<String,String> vals = HttpUtil.decodeParams(res);
                           
                         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]);  
                                 }  
                         }  
                           
286                                                    
287                            success = vals.get("status").equals("success");
288                            msg = vals.get("result");
289                                                    
290                  } catch (Exception e) {                  } catch (Exception e) {
291                          Log.e("SmsSend", "sendCoolSms", e);                          Log.e("SmsSend", "sendCoolSms", e);
# Line 310  public class SmsSend extends Activity { Line 294  public class SmsSend extends Activity {
294                                    
295                  return success;                  return success;
296          }          }
297            
298          boolean sendSmsDaemon() {          boolean sendSmsDaemon() {
299    
300                  int count = getCount();                                  int count = getCount();                
# Line 329  public class SmsSend extends Activity { Line 313  public class SmsSend extends Activity {
313          }          }
314                    
315                    
         public static byte[] getContent(String uri, int timeout) throws IOException {  
                 URL url = new URL(uri);  
                 URLConnection connection = url.openConnection();  
                   
                 connection.setConnectTimeout(timeout);  
                 InputStream is = connection.getInputStream();  
                   
                 return readInputStream(is);  
         }  
           
         public static byte[] postContent(String uri, String data,  int timeout) throws IOException {  
                 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();  
                   
                 return readInputStream(is);  
316    
         }  
           
         static byte[] readInputStream(InputStream is) throws IOException{  
                 byte buffer[] = new byte[1024];  
                 ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes  
                 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();                
         }  
317    
318          OnItemSelectedListener methodSelected = new OnItemSelectedListener() {          OnItemSelectedListener methodSelected = new OnItemSelectedListener() {
319                  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {                  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

Legend:
Removed from v.753  
changed lines
  Added in v.754

  ViewVC Help
Powered by ViewVC 1.1.20