Android, the history of the first application

Good day. I hasten to note that this post does not pull or how much a benefit, it's only a story of a beginner, novice, telling about the beginning of my conquest of the android. Let's begin.

The idea to write his first in the life of an application for android appeared spontaneously. Basic knowledge of java were available, and in my head immediately got the idea to write a program to send SMS through (yes, this is the Republic of Moldova). This proved harder than I thought at first glance.

First Things First I decided to start the program interface. For me was a pleasant surprise that the whole interface is created in the xml file. For an application I needed 4 boxes for text input, two for its display, one to display a capture images, and a submit button. In the sample code to show only one example for each:
< code ><? xml version ="1.0" encoding ="utf-8" ? >
< AbsoluteLayout
android:id ="@+id/widget32"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
xmlns:android =""
android:background ="@drawable/bbg"
< TextView
android:id ="@+id/widget41"
android:layout_width ="27px"
android:layout_height ="20px"
android:text ="От:"
android:textStyle ="bold"
android:layout_x ="12px"
android:layout_y ="12px"
</ TextView >
< EditText
android:id ="@+id/widget42"
android:layout_width ="192px"
android:layout_height ="30px"
android:textSize ="18sp"
android:layout_x ="37px"
android:layout_y ="9px"
</ EditText >
< Button
android:id ="@+id/widget54"
android:layout_width ="186px"
android:layout_height ="34px"
android:text ="Отправить"
android:layout_x ="30px"
android:layout_y ="228px"
< ImageView
android:id ="@+id/widget56"
android:layout_width ="97px"
android:layout_height ="35px"
android:layout_x ="19px"
android:layout_y ="162px"
</ ImageView >

* This source code was highlighted with Source Code Highlighter .

I will not hide, used to mark the program Droid Draw ( The second issue was for me, how to use it all in the program. It turned out easier to nowhere. It was necessary only to indicate with findViewById () on an already created object in our xml file.
ImageView imgView = (ImageView) findViewById (R. id. Widget56);
final EditText nume = (EditText) findViewById (R. id. widget42);
final EditText prefix = (EditText) findViewById (R. id. widget45);
final EditText number = (EditText) findViewById (R. id. widget46);
final EditText message = (EditText) findViewById (R. id. widget50);
final EditText captchatext = (EditText) findViewById (R. id. widget53);
final TextView io = (TextView) findViewById (R. id. widget87);
Button sendSMS = (Button) findViewById (R. id. Widget54);

And then began the most interesting. It turns out the first request to send requests the cookies, hanging at the beginning, he was received by a simple String headerValue = con.getHeaderField (6), but it also asks eventvalid values ​​and viewstate. For me it was a huge screeching halt, because I had no idea where he takes them. It turned out they fit into the HTML, and from where I was pulled out by simple manipulations. From the same was received id captcha images, which I later used to display it.
url = new URL (" / Default.aspx " );
con = url. openConnection ();
con. setDoInput (true);
con. setDoOutput (true);
con. setAllowUserInteraction (true);
con. setRequestProperty ("Connection", "keep-alive");
con. setRequestProperty ("Accept-Encoding", "gzip, deflate");
con. setRequestProperty ("Accept-Charset", "windows-1251, utf-8; q = 0.7, *; q = 0.7");
Con. setRequestProperty ("Referer", " " );
con. setRequestProperty ("Accept", "text / html, application / xhtml + xml, application / xml; q = 0.9 ,*/*; q =0.8");
con. setRequestProperty ("Accept-Language", "ru, en-us; q = 0.7, en; q = 0.3");
con. setRequestProperty ("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv: Firefox");
con. setRequestProperty ("Content-Type", "application / x-www-form-urlencoded; charset = utf-8");
con. setRequestProperty ("Pragma", "no-cache");
Pattern p = Pattern. Compile ("text / html; \ \ s + charset = ([^ \ \ s] +) \ \ s *");
Matcher m = p. matcher (con. getContentType ());
String charset = m. matches ()? M. group (1): "ISO-8859-1";
Reader r = new InputStreamReader (con. getInputStream (), charset);
StringBuilder buf = new StringBuilder ();
while (true) {
int ch = r. read ();
if (ch <0)
buf. append ((char) ch);
String str = buf. ToString ();
int len = str. length ();
eventvalid = str. substring (3122, (len 4540));
viewstate = str. substring (1496, (len 4746));
temp = str. substring (6490, (len 1240));
String headerValue = con. GetHeaderField (6) / / requested cookies (the number of cells found with the help of supplements for FireFix - HttpFox)
temp1 = headerValue. substring (0, (len1 - 17)) / / little cut unnecessary data from the cookie.

Then I had to screen a capture an image that was generated randomly each time you request. I tried to get her, sent tezhe cookies that were sent to me at first session, she did let herself get.
URL1 = new URL URL (" = " temp);
URLConnection con1 = url1. OpenConnection ();
con1. setRequestProperty ("Host", " " );
con1. setRequestProperty ("Referer", " " );
con1. setRequestProperty ("Accept", "image / png, image / *; q = 0.8 ,*/*; q = 0.5");
con1. setRequestProperty ("Cookie", temp1 + "lang = ro");
con1. setRequestProperty ("Accept-Encoding", "gzip, deflate");
con1. setRequestProperty ("Accept-Charset", "windows-1251, utf-8; q = 0.7, *; q = 0.7");
con1. setRequestProperty ("Accept-Language", "ru, en-us; q = 0.7, en; q = 0.3");
con1. setRequestProperty ("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv: Firefox");
con1. setRequestProperty ("Content-Type", "application / x-www-form-urlencoded; charset = utf-8");
con1. setRequestProperty ("Pragma", "no-cache");
con1. connect ();

Immediately after that I show it through ImageView.
InputStream is = (InputStream) con1. GetContent ();
Drawable d1 = Drawable. CreateFromStream (is, "src name");
imgView. setImageDrawable (d1);

The case remained for sending a POST request. I hung the whole thing on the button. Been looking for, how to pull the text out of Edith Box, was simply nowhere. The example below.
sendSMS. setOnClickListener (new Button. OnClickListener () {
public void onClick (View v)
try {

data + = "&" + URLEncoder. encode ("__VIEWSTATE", "UTF-8") + "=" + URLEncoder. encode (viewstate, "UTF-8");
data + = "&" + URLEncoder. encode ("__EVENTVALIDATION", "UTF-8") + "=" + URLEncoder. encode (eventvalid,"UTF-8");

data + = "&" + URLEncoder. encode ("btnOK", "UTF-8") + "=" + URLEncoder. encode ("trimite sms", "UTF-8");

By opening a new connection by sending the server through setRequestProperty () my old cookies, I made a POST request and simultaneously loads the page, so you can see off a SMS, or not.
OutputStreamWriter wr = new OutputStreamWriter (con3. getOutputStream ());
wr. write (data);
wr. flush ();
Pattern p1 = Pattern. Compile ("text / html; \ \ s + charset = ([^ \ \ s] +) \ \ s *");
Matcher m1 = p1. Matcher (con3. getContentType ());
String charset = m1. Matches ()? M1. Group (1): "ISO-8859-1";
Reader r1 = new InputStreamReader (con3. getInputStream (), charset);
StringBuilder buf1 = new StringBuilder ();
while (true) {
int ch1 = r1. read ();
if (ch1 <0)
buf1. append ((char) ch1);
String str5 = buf1. ToString ();

Page always static, and change it to just one sentence, depending on the action. First, it is - success, the second - the error Captcha, and the third - exceeded the allowable number of SMS. To find out what happened at my end and put it on the screen, I decided simply to look at html-f only one word, which is only a proposal. If it met there, I decided to print a message in the initial TextView.
int intIndex = str6. indexOf ("succes");
int intIndex1 = str6. indexOf ("depasit");
int intIndex2 = str6. indexOf ("incorect");
if (intIndex == - 1) {} else {
io. setText ("Message was sent successfully.");
if (intIndex1 == - 1) {} else {
io. setText ("You sent more than five messages.");
if (intIndex2 == - 1) {} else {
io. setText ("You entered the wrong code in the image.");

Unpleasant surprise for me was that it does not work. It turned out it in the constraints that need to register in the file AndroidManifest, which I did, giving the program full access to the Internet.
uses - permission android: name "android.permission.INTERNET"> </ uses - permission>


For me, the first application for the android was the gateway to the mobile programming. I was extremely delighted that this is the exact same java without limitation, wrapped in beautiful packaging. For those wishing to spread the sources. Download. zip download. apk