avinandanbanerjee

LoginService

Jun 17th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. package in.campuskarma.test;
  2.  
  3. import android.app.Activity;
  4. import android.os.AsyncTask;
  5. import android.os.Bundle;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.util.JsonReader;
  8. import android.util.Log;
  9. import android.view.KeyEvent;
  10. import android.view.inputmethod.EditorInfo;
  11. import android.widget.AutoCompleteTextView;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. import java.io.BufferedReader;
  17. import java.io.DataOutputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.net.MalformedURLException;
  22. import java.net.URL;
  23.  
  24. import javax.net.ssl.HttpsURLConnection;
  25.  
  26.  
  27. //import static android.app.PendingIntent.getActivity;
  28.  
  29. public class LoginService{
  30.  
  31.  
  32.  
  33. public void Login(String userid, String password)
  34. {
  35.  
  36. // Toast.makeText(getApplicationContext(),"Your Message", Toast.LENGTH_LONG).show();
  37. // Log.i("Message1","Message2");
  38. /* try {
  39. sendPost();
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. } */
  43. AsyncTask.execute(new Runnable() {
  44. @Override
  45. public void run() {
  46.  
  47.  
  48. // All your networking logic
  49. // should be here
  50. // Create URL
  51. URL githubEndpoint = null;
  52. try {
  53. githubEndpoint = new URL("https://campuskarma.in/services/api/rest/json/");
  54. HttpsURLConnection myConnection =
  55. (HttpsURLConnection) githubEndpoint.openConnection();
  56. myConnection.setRequestMethod("POST");
  57. myConnection.setRequestProperty("call_method","POST");
  58. myConnection.setRequestProperty("function","auth_gettoken");
  59. myConnection.setRequestProperty("call_method","POST");
  60. myConnection.setRequestProperty("call_method","POST");
  61.  
  62.  
  63. //myConnection.setRequestProperty("User-Agent", "my-rest-app-v0.1");
  64. //myConnection.setRequestProperty("Accept",
  65. // "application/vnd.github.v3+json");
  66. //myConnection.setRequestProperty("Contact-Me",
  67. // "hathibelagal@example.com");
  68.  
  69. if (myConnection.getResponseCode() == 200) {
  70. // Success
  71. // Further processing here
  72.  
  73. InputStream responseBody = myConnection.getInputStream();
  74.  
  75. InputStreamReader responseBodyReader =
  76. new InputStreamReader(responseBody, "UTF-8");
  77.  
  78. JsonReader jsonReader = new JsonReader(responseBodyReader);
  79.  
  80.  
  81. jsonReader.beginObject(); // Start processing the JSON object
  82. while (jsonReader.hasNext()) { // Loop through all keys
  83. String key = jsonReader.nextName(); // Fetch the next key
  84. if (key.equals("status")) { // Check if desired key
  85. // Fetch the value as a String
  86. String value = jsonReader.nextString();
  87.  
  88. // Do something with the value
  89. // ...
  90. Log.i("parsing value ", value);
  91. break; // Break out of the loop
  92. } else {
  93. jsonReader.skipValue(); // Skip values of other keys
  94. }
  95. }
  96. Log.i("working", jsonReader.toString());
  97.  
  98. } else {
  99. // Error handling code goes here
  100. }
  101.  
  102. } catch (MalformedURLException e) {
  103. e.printStackTrace();
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. }
  107.  
  108.  
  109. }
  110. });
  111. };
  112.  
  113. // HTTP POST request
  114. private void sendPost() throws Exception {
  115.  
  116. String url = "https://www.campuskarma.in/services/api/rest/json/";
  117. URL obj = new URL(url);
  118. HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
  119.  
  120. //add reuqest header
  121. con.setRequestMethod("POST");
  122. String urlParameters = "method=auth.gettoken&username=your_username&password=your_password";
  123.  
  124. // Send post request
  125. con.setDoOutput(true);
  126. DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  127. wr.writeBytes(urlParameters);
  128. wr.flush();
  129. wr.close();
  130.  
  131. int responseCode = con.getResponseCode();
  132. System.out.println("\nSending 'POST' request to URL : " + url);
  133. System.out.println("Post parameters : " + urlParameters);
  134. System.out.println("Response Code : " + responseCode);
  135.  
  136. BufferedReader in = new BufferedReader(
  137. new InputStreamReader(con.getInputStream()));
  138. String inputLine;
  139. StringBuffer response = new StringBuffer();
  140.  
  141. while ((inputLine = in.readLine()) != null) {
  142. response.append(inputLine);
  143. }
  144. in.close();
  145.  
  146. //print result
  147. System.out.println(response.toString());
  148.  
  149. }
  150.  
  151.  
  152. }
Add Comment
Please, Sign In to add comment