Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. public class Login extends Activity {
  2.  
  3. //URL to get JSON Array
  4. private static String url = "http://*************/webservice.php?operation=getchallenge&username=admin";
  5.  
  6. //JSON Node Names
  7. private static final String TAG_RESULT = "result";
  8. private static final String TAG_TOKEN = "token";
  9.  
  10. // contacts JSONArray
  11. JSONArray contacts = null;
  12.  
  13. String token = null;
  14.  
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.login);
  19.  
  20. if (android.os.Build.VERSION.SDK_INT > 9) {
  21. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  22. StrictMode.setThreadPolicy(policy);
  23. }
  24.  
  25.  
  26. new AsyncTask<Void, Void, Void>() {
  27.  
  28. @SuppressWarnings("unused")
  29. JSONObject result;
  30.  
  31. @Override
  32. protected Void doInBackground(Void... params) {
  33.  
  34. // Creating new JSON Parser
  35. JSONParser jParser = new JSONParser();
  36.  
  37. // Getting JSON from URL
  38. JSONObject json = jParser.getJSONFromUrl(url);
  39.  
  40. try {
  41. // Getting JSON Array
  42. result = json.getJSONObject(TAG_RESULT);
  43. JSONObject json_result = json.getJSONObject(TAG_RESULT);
  44.  
  45. // Storing JSON item in a Variable
  46. token = json_result.getString(TAG_TOKEN);
  47.  
  48. //Importing TextView
  49.  
  50. } catch (JSONException e) {
  51. e.printStackTrace();
  52. }
  53.  
  54. String username="admin";
  55. String accesskeyvalue = "**************";
  56. String accessKey=md5(token + accesskeyvalue);
  57.  
  58. String data = null;
  59.  
  60. try {
  61. data = URLEncoder.encode("username", "UTF-8")
  62. + "=" + URLEncoder.encode(username, "UTF-8");
  63. data += "&" + URLEncoder.encode("accessKey", "UTF-8") + "="
  64. + URLEncoder.encode(accessKey, "UTF-8");
  65. } catch (UnsupportedEncodingException e) {
  66. // TODO Auto-generated catch block
  67. e.printStackTrace();
  68. }
  69. String text = "";
  70. BufferedReader reader=null;
  71. System.out.println(data);
  72.  
  73. // Send data
  74. try
  75. {
  76.  
  77. // Defined URL where to send data
  78. URL url = new URL("http://***************/webservice.php?operation=login");
  79.  
  80. // Send POST data request
  81. URLConnection conn = url.openConnection();
  82. conn.setDoOutput(true);
  83. OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
  84. wr.write( data );
  85. wr.flush();
  86.  
  87. // Get the server response
  88. reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  89. StringBuilder sb = new StringBuilder();
  90. String line = null;
  91.  
  92. // Read Server Response
  93. while((line = reader.readLine()) != null)
  94. {
  95. // Append server response in string
  96. sb.append(line + "n");
  97. }
  98.  
  99.  
  100. text = sb.toString();
  101. }
  102. catch(Exception ex)
  103. {
  104.  
  105. }
  106. finally
  107. {
  108. try
  109. {
  110.  
  111. reader.close();
  112. }
  113.  
  114. catch(Exception ex) {}
  115. }
  116.  
  117. // Show response
  118. System.out.println(text);
  119. String sessionid = text.substring(41, 62);
  120. System.out.println(sessionid);
  121.  
  122.  
  123.  
  124. return null;
  125. }
  126. @Override
  127. protected void onPostExecute(Void aVoid) {
  128. super.onPostExecute(aVoid);
  129. }
  130.  
  131. }.execute();
  132.  
  133. }
  134.  
  135. public String md5(String s)
  136. {
  137. MessageDigest digest;
  138. try
  139. {
  140. digest = MessageDigest.getInstance("MD5");
  141. digest.update(s.getBytes(),0,s.length());
  142. String hash = new BigInteger(1, digest.digest()).toString(16);
  143. return hash;
  144. }
  145. catch (NoSuchAlgorithmException e)
  146. {
  147. e.printStackTrace();
  148. }
  149. return "";
  150. }
  151.  
  152. public void sendMessage(View view)
  153. {
  154. Intent intent = new Intent(Login.this, Quote1.class);
  155. intent.putExtra("sessionId", sessionId);
  156. startActivity(intent);
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement