Android Open Source - chat.android Chat Auth Server Authenticate






From Project

Back to project page chat.android.

License

The source code is released under:

GNU General Public License

If you think the Android project chat.android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.chat.android.Auth;
//from w w  w .java2  s . c  om
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class ChatAuthServerAuthenticate implements ServerAuthenticate {
  
  private static final String AUTH_PATH = "auth";
  private static final String ARG_CREDENTIAL="?google_access_token=";
  private static final String ARG_DEVICE_ID = "&deviceid=";
  private static final String ARG_CLIENT_ACCESS_TOKEN = "client_access_token";
  private String base_url;
  
  public ChatAuthServerAuthenticate(String base_url) {
    super();
    this.base_url = base_url;
  }



  @Override
  public String getAccessToken(String email, String password, String deviceid) {
    HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        HttpURLConnection conn = null;
        
        String url = base_url.concat(AUTH_PATH);
    url = url.concat(ARG_CREDENTIAL);
    url = url.concat(password);
    url = url.concat(ARG_DEVICE_ID);
    url = url.concat(deviceid);
    
    try {
      URL mUrl = new URL(url);

      Log.i("URL", "> " + url);
      conn = (HttpURLConnection) mUrl.openConnection();
      
//      conn.setUseCaches(false);
//      conn.setDoOutput(true);
//      conn.setRequestMethod("GET");
//      conn.setRequestProperty("Content-Type",
//          "application/x-www-form-urlencoded;charset=UTF-8");
      
      // handle the response
      int status = conn.getResponseCode();
      if (status != 200) {
        //Closes the connection.
              Log.e("ChatAuthServerAuthenticate", String.valueOf(status));
              if(status==HttpStatus.SC_UNAUTHORIZED)
              {
                InputStream in = new BufferedInputStream(
                conn.getErrorStream());
            StringBuffer sb = new StringBuffer("");
            BufferedReader reader = new BufferedReader(
                new InputStreamReader(in));
            String inputLine = "";
            while ((inputLine = reader.readLine()) != null) {
              sb.append(inputLine);
//                sb.append("\n");
            }
            in.close();
            responseString=sb.toString();
            Log.e("ChatAuthServerAuthenticate", responseString);
              }
                throw new IOException();
      }
      else
      {
        InputStream in = new BufferedInputStream(
            conn.getInputStream());
        StringBuffer sb = new StringBuffer("");
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(in));
        String inputLine = "";
        while ((inputLine = reader.readLine()) != null) {
          sb.append(inputLine);
//            sb.append("\n");
        }
        in.close();
        responseString=sb.toString();
      }
      
//      response = httpclient.execute(new HttpGet(url));
//          StatusLine statusLine = response.getStatusLine();
//          
//          // checking response to see if it worked ok
//            if (statusLine.getStatusCode() == HttpStatus.SC_OK){
//              // all that obvious receiving business
//                ByteArrayOutputStream out = new ByteArrayOutputStream();
//                response.getEntity().writeTo(out);
//                out.close();
//                responseString = out.toString();
//                Log.i("SyncAdapter", "Response text: \n"+responseString);
                
                // transform response into a JSON object
                //JSONObject jsonObject = new JSONObject(responseString);
                //return jsonObject.getString(ARG_CLIENT_ACCESS_TOKEN);
                return responseString;
    } catch (ClientProtocolException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    finally {
      if (conn != null) {
        conn.disconnect();
      }
    }
    return null;
  }

  

}




Java Source Code List

org.chat.android.BaseActivity.java
org.chat.android.CHADelivery.java
org.chat.android.CHAOverviewActivity.java
org.chat.android.CHASelectChildActivity.java
org.chat.android.CHASelectChildAdapter.java
org.chat.android.ChatUtil.java
org.chat.android.ClientsAdapter.java
org.chat.android.DatabaseHelper.java
org.chat.android.GPSTracker.java
org.chat.android.HealthDeliveryActivity.java
org.chat.android.HealthDetailsActivity.java
org.chat.android.HealthOverviewActivity.java
org.chat.android.HealthOverviewRecordActivity.java
org.chat.android.HomeActivity.java
org.chat.android.ImmunizationsReceivedActivity.java
org.chat.android.ImmunizationsSummaryActivity.java
org.chat.android.LoginActivity.java
org.chat.android.Mail.java
org.chat.android.ModelHelper.java
org.chat.android.MyApplication.java
org.chat.android.ResourcesActivity.java
org.chat.android.RestoreVisitActivity.java
org.chat.android.ServiceDeliveryActivity.java
org.chat.android.ServiceDeliveryAdapter.java
org.chat.android.ServiceDetailsActivity.java
org.chat.android.ServiceOtherActivity.java
org.chat.android.ServiceOverviewActivity.java
org.chat.android.ServicesAdapter.java
org.chat.android.SetupDB.java
org.chat.android.SetupVisitActivity.java
org.chat.android.SyncResourcesActivity.java
org.chat.android.Auth.AccountGeneral.java
org.chat.android.Auth.AuthenticatorService.java
org.chat.android.Auth.Authenticator.java
org.chat.android.Auth.ChatAuthServerAuthenticate.java
org.chat.android.Auth.MainActivity.java
org.chat.android.Auth.ServerAuthenticate.java
org.chat.android.Sync.StubProvider.java
org.chat.android.Sync.SyncAdapter.java
org.chat.android.Sync.SyncService.java
org.chat.android.Sync.provider.ChatContentProvider.java
org.chat.android.models.Attendance.java
org.chat.android.models.CHAAccessed.java
org.chat.android.models.Client.java
org.chat.android.models.HealthPage.java
org.chat.android.models.HealthSelectRecorded.java
org.chat.android.models.HealthSelect.java
org.chat.android.models.HealthTheme.java
org.chat.android.models.HealthTopicAccessed.java
org.chat.android.models.HealthTopic.java
org.chat.android.models.Household.java
org.chat.android.models.PageAssessment1.java
org.chat.android.models.PageSelect1.java
org.chat.android.models.PageText1.java
org.chat.android.models.PageVideo1.java
org.chat.android.models.ResourceAccessed.java
org.chat.android.models.Resource.java
org.chat.android.models.Role.java
org.chat.android.models.ServiceAccessed.java
org.chat.android.models.Service.java
org.chat.android.models.TopicVideo.java
org.chat.android.models.Util.java
org.chat.android.models.VaccineRecorded.java
org.chat.android.models.Vaccine.java
org.chat.android.models.VideoAccessed.java
org.chat.android.models.Video.java
org.chat.android.models.Visit.java
org.chat.android.models.Worker.java
org.chat.android.pages.Assessment1Fragment.java
org.chat.android.pages.BaseFragment.java
org.chat.android.pages.ReferralFragment.java
org.chat.android.pages.Select1Fragment.java
org.chat.android.pages.Text1Fragment.java
org.chat.android.pages.Video1Fragment.java