Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package in.campuskarma.test;
- import android.app.Activity;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.JsonReader;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.inputmethod.EditorInfo;
- import android.widget.AutoCompleteTextView;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.MalformedURLException;
- import java.net.URL;
- import javax.net.ssl.HttpsURLConnection;
- //import static android.app.PendingIntent.getActivity;
- public class LoginService{
- public void Login(String userid, String password)
- {
- // Toast.makeText(getApplicationContext(),"Your Message", Toast.LENGTH_LONG).show();
- // Log.i("Message1","Message2");
- /* try {
- sendPost();
- } catch (Exception e) {
- e.printStackTrace();
- } */
- AsyncTask.execute(new Runnable() {
- @Override
- public void run() {
- // All your networking logic
- // should be here
- // Create URL
- URL githubEndpoint = null;
- try {
- githubEndpoint = new URL("https://campuskarma.in/services/api/rest/json/");
- HttpsURLConnection myConnection =
- (HttpsURLConnection) githubEndpoint.openConnection();
- myConnection.setRequestMethod("POST");
- myConnection.setRequestProperty("call_method","POST");
- myConnection.setRequestProperty("function","auth_gettoken");
- myConnection.setRequestProperty("call_method","POST");
- myConnection.setRequestProperty("call_method","POST");
- //myConnection.setRequestProperty("User-Agent", "my-rest-app-v0.1");
- //myConnection.setRequestProperty("Accept",
- // "application/vnd.github.v3+json");
- //myConnection.setRequestProperty("Contact-Me",
- // "hathibelagal@example.com");
- if (myConnection.getResponseCode() == 200) {
- // Success
- // Further processing here
- InputStream responseBody = myConnection.getInputStream();
- InputStreamReader responseBodyReader =
- new InputStreamReader(responseBody, "UTF-8");
- JsonReader jsonReader = new JsonReader(responseBodyReader);
- jsonReader.beginObject(); // Start processing the JSON object
- while (jsonReader.hasNext()) { // Loop through all keys
- String key = jsonReader.nextName(); // Fetch the next key
- if (key.equals("status")) { // Check if desired key
- // Fetch the value as a String
- String value = jsonReader.nextString();
- // Do something with the value
- // ...
- Log.i("parsing value ", value);
- break; // Break out of the loop
- } else {
- jsonReader.skipValue(); // Skip values of other keys
- }
- }
- Log.i("working", jsonReader.toString());
- } else {
- // Error handling code goes here
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
- };
- // HTTP POST request
- private void sendPost() throws Exception {
- String url = "https://www.campuskarma.in/services/api/rest/json/";
- URL obj = new URL(url);
- HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
- //add reuqest header
- con.setRequestMethod("POST");
- String urlParameters = "method=auth.gettoken&username=your_username&password=your_password";
- // Send post request
- con.setDoOutput(true);
- DataOutputStream wr = new DataOutputStream(con.getOutputStream());
- wr.writeBytes(urlParameters);
- wr.flush();
- wr.close();
- int responseCode = con.getResponseCode();
- System.out.println("\nSending 'POST' request to URL : " + url);
- System.out.println("Post parameters : " + urlParameters);
- System.out.println("Response Code : " + responseCode);
- BufferedReader in = new BufferedReader(
- new InputStreamReader(con.getInputStream()));
- String inputLine;
- StringBuffer response = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- //print result
- System.out.println(response.toString());
- }
- }
Add Comment
Please, Sign In to add comment