Advertisement
Guest User

Untitled

a guest
Feb 1st, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. package com.mediajackagency.test;
  2.  
  3. import android.os.AsyncTask;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.support.v7.widget.Toolbar;
  7. import android.util.Log;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.TextView;
  13.  
  14. import java.io.BufferedReader;
  15. import java.io.InputStream;
  16. import java.io.InputStreamReader;
  17. import java.net.HttpURLConnection;
  18. import java.net.URI;
  19. import java.net.URL;
  20.  
  21. public class MainActivity extends AppCompatActivity {
  22.  
  23. public Button signInBtn = null;
  24. public Button getUserInfoBtn = null;
  25. public TextView xmlTextView = null;
  26.  
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);
  31. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  32. setSupportActionBar(toolbar);
  33.  
  34. this.xmlTextView = (TextView)findViewById(R.id.xmlTxtView);
  35.  
  36. this.signInBtn = (Button)findViewById(R.id.signInBtn);
  37. this.signInBtn.setOnClickListener(new View.OnClickListener() {
  38. @Override
  39. public void onClick(View v) {
  40. AsyncTask task = new AsyncTask() {
  41. @Override
  42. protected Object doInBackground(Object[] params) {
  43. String xml = loadUrl("https://www.fake.site/Login?userName=test&password=pass123");
  44.  
  45. return xml;
  46. }
  47.  
  48. @Override
  49. protected void onPostExecute(Object o) {
  50. super.onPostExecute(o);
  51. xmlTextView.setText(o.toString());
  52. }
  53. };
  54. task.execute();
  55. }
  56. });
  57.  
  58. this.getUserInfoBtn = (Button)findViewById(R.id.getUserInfoBtn);
  59. this.getUserInfoBtn.setOnClickListener(new View.OnClickListener() {
  60. @Override
  61. public void onClick(View v) {
  62. AsyncTask task = new AsyncTask() {
  63. @Override
  64. protected Object doInBackground(Object[] params) {
  65. String xml = loadUrl("https://www.fake.site/GetCurrentUser");
  66.  
  67. return xml;
  68. }
  69.  
  70. @Override
  71. protected void onPostExecute(Object o) {
  72. super.onPostExecute(o);
  73. xmlTextView.setText(o.toString());
  74. }
  75. };
  76. task.execute();
  77. }
  78. });
  79. }
  80.  
  81. public String loadUrl(String url) {
  82. URI uri = MainActivity.encodeUrl(url);
  83. Log.i("XMLParser", "Get URL: " + url);
  84.  
  85. String xml = null;
  86. URL link;
  87. BufferedReader reader = null;
  88. StringBuilder stringBuilder = null;
  89. InputStream is = null;
  90. HttpURLConnection connection = null;
  91.  
  92. try {
  93. link = new URL(url);
  94. connection = (HttpURLConnection) link.openConnection();
  95. connection.setRequestMethod("GET");
  96. connection.connect();
  97.  
  98. is = connection.getInputStream();
  99. reader = new BufferedReader(new InputStreamReader(is));
  100. stringBuilder = new StringBuilder();
  101.  
  102. String line = null;
  103. while ((line = reader.readLine()) != null)
  104. {
  105. stringBuilder.append(line + "r");
  106. }
  107.  
  108. } catch (Exception e) {
  109. e.printStackTrace();
  110. } finally {
  111. if (reader != null) {
  112. try {
  113. if(reader != null) reader.close();
  114. } catch (Exception e) {
  115. e.printStackTrace();
  116. }
  117.  
  118. try {
  119. if(is != null) is.close();
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. }
  123.  
  124. try {
  125. if(connection != null) connection.disconnect();
  126. } catch (Exception e) {
  127. e.printStackTrace();
  128. }
  129. }
  130. }
  131.  
  132. try {
  133. xml = stringBuilder.toString();
  134. } catch(Exception e) {
  135. e.printStackTrace();
  136. }
  137. return xml;
  138. }
  139.  
  140. public static URI encodeUrl(String url) {
  141. URL urlObject;
  142. URI uri = null;
  143. try {
  144. urlObject = new URL(url);
  145. uri = new URI(urlObject.getProtocol(), urlObject.getUserInfo(), urlObject.getHost(),
  146. urlObject.getPort(), urlObject.getPath(), urlObject.getQuery(), urlObject.getRef());
  147.  
  148. } catch (Exception e) {
  149. e.printStackTrace();
  150. }
  151. return uri;
  152. }
  153.  
  154. @Override
  155. public boolean onCreateOptionsMenu(Menu menu) {
  156. // Inflate the menu; this adds items to the action bar if it is present.
  157. getMenuInflater().inflate(R.menu.menu_main, menu);
  158. return true;
  159. }
  160.  
  161. @Override
  162. public boolean onOptionsItemSelected(MenuItem item) {
  163. // Handle action bar item clicks here. The action bar will
  164. // automatically handle clicks on the Home/Up button, so long
  165. // as you specify a parent activity in AndroidManifest.xml.
  166. int id = item.getItemId();
  167.  
  168. //noinspection SimplifiableIfStatement
  169. if (id == R.id.action_settings) {
  170. return true;
  171. }
  172.  
  173. return super.onOptionsItemSelected(item);
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement