Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dazzleapp.dazzle;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.os.Looper;
- import android.util.Log;
- import android.view.View;
- import android.widget.EditText;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.*;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.util.Scanner;
- import static android.widget.Toast.LENGTH_SHORT;
- import static android.widget.Toast.makeText;
- @SuppressLint({ "NewApi", "NewApi" })
- public class Auth extends Activity {
- public String usrname, uname, pw, pass, param, response;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.auth);
- }
- public void auth(View v) throws JSONException {
- EditText username = (EditText) findViewById(R.id.usernameInput);
- final String usrname = username.getText().toString();
- final String uname = "&username=" + usrname;
- EditText upw = (EditText) findViewById(R.id.passwordInput);
- final String pw = upw.getText().toString();
- final String pass = "&password=" + pw;
- if (upw.length()>0 || username.length()>0){
- class Data extends AsyncTask<String, Void, String>{
- protected String doInBackground(String... urls){
- Looper.prepare();
- URL url;
- HttpURLConnection conn;
- InputStream is = null;
- String result = "";
- JSONObject jArray = null;
- //post
- try{
- url = new URL("https://alpha.app.net/oauth/access_token");
- String param = "client_id=" + URLEncoder.encode("<client_id>","UTF-8")+
- "&password_grant_secret=" + URLEncoder.encode("<pw_grant>", "UTF-8") +
- "&grant_type=" + URLEncoder.encode("password", "UTF-8") +
- "&username=" + URLEncoder.encode(usrname, "UTF-8") +
- "&password=" + URLEncoder.encode(pw, "UTF-8") +
- "&scope=" + URLEncoder.encode("basic,stream,email,write_post,follow,messages", "UTF-8");
- conn = (HttpURLConnection)url.openConnection();
- conn.setDoOutput(true);
- conn.setDoInput(true);
- conn.setRequestMethod("POST");
- conn.setFixedLengthStreamingMode(param.getBytes().length);
- conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- PrintWriter out = new PrintWriter(conn.getOutputStream());
- out.print(param);
- out.close();
- BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()),8096);
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = reader.readLine()) != null){
- sb.append(line + "\n");
- }
- is.close();
- result = sb.toString();
- /*} catch(Exception e){
- Log.e("log_tag", "Error in http connection " + e.toString());
- }
- //convert response
- try{
- BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()),8096);
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = reader.readLine()) != null){
- sb.append(line + "\n");
- }
- is.close();
- result = sb.toString(); */
- }catch(Exception e){
- Log.e("log_tag", "Error converting result " + e.toString());
- }
- //parse response to JSON object
- try{
- jArray = new JSONObject(result);
- }catch (JSONException e){
- Log.e("log_tag", "Error parsing data " + e.toString());
- }
- return null;
- }
- }
- Data authTask = new Data();
- authTask.execute();
- } else {
- makeText(getBaseContext(), "All field are required", LENGTH_SHORT).show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment