/[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 751 by torben, Wed May 26 07:44:39 2010 UTC revision 755 by torben, Wed May 26 15:10:14 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.smssend;  package dk.thoerup.smssend;
2    
3  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;  
4  import java.util.ArrayList;  import java.util.ArrayList;
5    import java.util.HashMap;
6    import java.util.Map;
7    
8  import android.app.Activity;  import android.app.Activity;
9  import android.app.ProgressDialog;  import android.app.ProgressDialog;
# Line 240  public class SmsSend extends Activity { Line 234  public class SmsSend extends Activity {
234                  return true;                  return true;
235          }          }
236                    
237          String encode(String url) {  
                 try {  
                         return URLEncoder.encode(url, "UTF-8");  
                 } catch (Exception e) {  
                         return url;  
                 }  
         }  
238    
239          boolean sendCoolSms() {          boolean sendCoolSms() {
240                  SharedPreferences prefs = this.getSharedPreferences("SmsSend", MODE_PRIVATE);                  SharedPreferences prefs = this.getSharedPreferences("SmsSend", MODE_PRIVATE);
# Line 267  public class SmsSend extends Activity { Line 255  public class SmsSend extends Activity {
255                                    
256                  String url = "http://sms.coolsmsc.dk:8080/";                  String url = "http://sms.coolsmsc.dk:8080/";
257                                    
258                  StringBuilder sb = new StringBuilder();                  Map<String,String> params = new HashMap<String,String>();
259                  sb.append("username=").append(user);                  params.put("username", user);
260                  sb.append("&password=").append(pass);                  params.put("password", pass);
261                  sb.append("&to=").append( getTo() );                  params.put("to", getTo());
262                  sb.append("&from=").append( encode(getFrom()) );                  params.put("from", getFrom());
263                  sb.append("&message=").append( encode(getMessage()) );                  params.put("message", getMessage() );
264                  sb.append("&resulttype=urlencoded");                  params.put("resulttype", "urlencoded" );
265                  sb.append("&lang=en");                  params.put("lang", "lang" );
266                                    
267                  Log.v("SmsSend", sb.toString());                  String paramStr = HttpUtil.encodeParams(params);
268                    
269                    Log.v("SmsSend", paramStr);
270                                    
271                                    
272                  boolean success = false;                  boolean success = false;
273                  String msg = "";                  String msg = "";
274                  try {                  try {
275                          byte bytes[] = postContent(url,  sb.toString(), 2500 );                          byte bytes[] = HttpUtil.postContent(url,  paramStr, 2500 );
276                          String res = new String(bytes);                          String res = new String(bytes);
277                          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]);  
                                 }  
                         }  
                           
278                                                    
279                            success = vals.get("status").equals("success");
280                            msg = vals.get("result");
281                                                    
282                  } catch (Exception e) {                  } catch (Exception e) {
283                          Log.e("SmsSend", "sendCoolSms", e);                          Log.e("SmsSend", "sendCoolSms", e);
# Line 310  public class SmsSend extends Activity { Line 286  public class SmsSend extends Activity {
286                                    
287                  return success;                  return success;
288          }          }
289            
290          boolean sendSmsDaemon() {          boolean sendSmsDaemon() {
291    
292                  int count = getCount();                                  int count = getCount();                
# Line 329  public class SmsSend extends Activity { Line 305  public class SmsSend extends Activity {
305          }          }
306                    
307                    
308          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();  
         }  
309    
310          OnItemSelectedListener methodSelected = new OnItemSelectedListener() {          OnItemSelectedListener methodSelected = new OnItemSelectedListener() {
311                  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.751  
changed lines
  Added in v.755

  ViewVC Help
Powered by ViewVC 1.1.20