Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PASTEBINnew pastetrends API tools faq
- search...
- Guest User
- -
- Public Pastes
- Untitled
- 11 sec ago
- ssr address 2018.01
- 13 sec ago
- Untitled
- 24 sec ago
- Untitled
- 29 sec ago
- Untitled
- 30 sec ago
- Untitled
- 34 sec ago
- Untitled
- 38 sec ago
- Untitled
- 40 sec ago
- daily pastebin goal
- 29%
- help support pastebin
- SHARE
- TWEET
- Untitled
- A GUEST JAN 16TH, 2018 49 NEVER
- NOTE: Your guest paste has been posted. If you sign up for a free account, you can edit and delete your pastes!
- rawdownloadcloneembedreportprint text 9.21 KB
- package com.example.dmust.restaurantapp;
- import android.util.Log;
- import android.widget.Toast;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import java.io.IOException;
- import okhttp3.OkHttpClient;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- import retrofit2.Retrofit;
- import retrofit2.converter.gson.GsonConverterFactory;
- public class UserManager
- {
- User user = null;
- private ServiceCallback callback = new ServiceCallback() { //POSSIBLE FAILS AS ITS A SERVICECALLBACK AND NOT THE RETROFIT CALLBACK??- BUT REMOVE ANYWAYS
- @Override
- public Response successful(Response response)
- {
- if(response!=null)
- {
- Log.d("response.body", response.body().toString());
- }
- else
- {
- response = null;
- }
- return response;
- }
- @Override
- public void fail(Throwable t)
- {
- Log.d("failed",t.getMessage());
- }
- };
- public UserManager()
- {
- }
- public String login(String username, String password)
- {
- String outcome;
- UserClient client = ServiceGenerator.createService(UserClient.class);
- Call<User> call = client.getUserByName(username);
- call.enqueue(
- new Callback<User>() {
- @Override
- public void onResponse(Call<User> call, Response<User> response)
- {
- // user = response.body();
- // if(user != null)
- // {
- // Log.d("Got the user!", user.toString());
- // }
- HERE IS WHERE THE RESPONSE IS , SO NOW YOU COULD PASS INFO TO A FUNCTION TO VALIDATE AND THEN FINALLY IF ALL IS GOOD DO SOMETHING LIKE
- getActivityMain().lOGINSUCCESS(user);
- //callback.successful(response);
- }
- @Override
- public void onFailure(Call<User> call, Throwable t)
- {
- Log.d("Error", "Error: " + t.getMessage());
- HERE IS WHERE THE Fail IS , DO SOMETHING LIKE
- getActivityMain().lOGINFAILED(user);
- // callback.fail(t);
- }
- });
- // if(user != null)
- // {
- // if (user.getPassword().equalsIgnoreCase(password))
- // {
- // outcome = "success";
- // }
- // else
- // {
- // outcome = "failure";
- // }
- // }
- // else
- // {
- // outcome = "failure";
- // }
- return "success";
- }
- }
- package com.example.dmust.restaurantapp;
- import okhttp3.OkHttpClient;
- import retrofit2.Retrofit;
- import retrofit2.converter.gson.GsonConverterFactory;
- public class ServiceGenerator
- {
- private final static String API_BASE_URL = "http://10.0.2.2:5000/";
- private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
- private static Retrofit.Builder builder = new Retrofit.Builder()
- .baseUrl(API_BASE_URL)
- .addConverterFactory(GsonConverterFactory.create());
- private static Retrofit retrofit = builder.client(httpClient.build()).build();
- public static <S> S createService(Class<S> serviceClass)
- {
- return retrofit.create(serviceClass);
- }
- }
- package com.example.dmust.restaurantapp;
- import retrofit2.Response;
- public interface ServiceCallback
- {
- Response successful(Response response);
- void fail(Throwable t);
- }
- package com.example.dmust.restaurantapp;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.util.JsonReader;
- import android.util.Log;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.Spinner;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.List;
- import okhttp3.OkHttpClient;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- import retrofit2.Retrofit;
- import retrofit2.converter.gson.GsonConverterFactory;
- public class MainActivity extends AppCompatActivity
- {
- private TextView textView;
- private UserManager userManager;
- Callback loginCallback;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- textView = (TextView) findViewById(R.id.textView);
- setSupportActionBar(toolbar);
- Spinner spinner = (Spinner) findViewById(R.id.spinner);
- // Create an ArrayAdapter using the string array and a default spinner layout
- ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
- R.array.user_types, android.R.layout.simple_spinner_item);
- // Specify the layout to use when the list of choices appears
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- // Apply the adapter to the spinner
- spinner.setAdapter(adapter);
- userManager = new UserManager();
- /*
- String API_BASE_URL = "http://10.0.2.2:5000/";
- OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
- Retrofit.Builder builder = new Retrofit.Builder()
- .baseUrl(API_BASE_URL)
- .addConverterFactory(GsonConverterFactory.create());
- Retrofit retrofit = builder.client(httpClient.build()).build();
- UserClient client = retrofit.create(UserClient.class);
- Call<List<User>> call = client.allUsers();
- call.enqueue(new Callback<List<User>>() {
- @Override
- public void onResponse(Call<List<User>> call, Response<List<User>> response)
- {
- List<User> users = response.body();
- textView.append(users.toString());
- }
- @Override
- public void onFailure(Call<List<User>> call, Throwable t)
- {
- Toast.makeText(MainActivity.this, "error", Toast.LENGTH_LONG).show();
- }
- });
- */
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- public void login(View view)
- {
- // String outcome;
- EditText username = (EditText) findViewById(R.id.login);
- EditText password = (EditText) findViewById(R.id.password);
- THIS WONT WORK IS THIS IS ASYNC - YOUR JUST TELLING THE USERMANAGER TO LOGIN()
- outcome = userManager.login(username.getText().toString(), password.getText().toString());
- CHANGE TO THIS
- loginCallback = new Callback()
- {
- public boolean handleMessage(User user) {
- lOGINSUCCESS(user);
- }
- };
- userManager.login(username.getText().toString(), password.getText().toString(),loginCallback);
- /*
- String API_BASE_URL = "http://10.0.2.2:5000/";
- OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
- Retrofit.Builder builder = new Retrofit.Builder()
- .baseUrl(API_BASE_URL)
- .addConverterFactory(GsonConverterFactory.create());
- Retrofit retrofit = builder.client(httpClient.build()).build();
- UserClient client = retrofit.create(UserClient.class);
- Call<User> call = client.getUserByName(username.getText().toString());
- call.enqueue(new Callback<User>() {
- @Override
- public void onResponse(Call<User> call, Response<User> response)
- {
- User user = response.body();
- textView.append(user.toString());
- }
- @Override
- public void onFailure(Call<User> call, Throwable t)
- {
- Toast.makeText(MainActivity.this, "error: " + t.getMessage(), Toast.LENGTH_LONG).show();
- }
- });
- */
- PUBLIC VOID lOGINSUCCESS()
- {
- Toast.makeText(MainActivity.this, "Logged in!", Toast.LENGTH_LONG).show();
- }
- PUBLIC VOID lOGINFAILED()
- {
- Toast.makeText(MainActivity.this, "FAILED!", Toast.LENGTH_LONG).show();
- }
- }
- }
- RAW Paste Data
- package com.example.dmust.restaurantapp;
- import android.util.Log;
- import android.widget.Toast;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import java.io.IOException;
- import okhttp3.OkHttpClient;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- import retrofit2.Retrofit;
- import retrofit2.converter.gson.GsonConverterFactory;
- public class UserManager
- {
- User user = null;
- private ServiceCallback callback = new ServiceCallback() { //POSSIBLE FAILS AS ITS A SERVICECALLBACK AND NOT THE RETROFIT CALLBACK??- BUT REMOVE ANYWAYS
- @Override
- public Response successful(Response response)
- {
- if(response!=null)
- {
- Log.d("response.body", response.body().toString());
- }
- else
- {
- response = null;
- }
- return response;
- }
- @Override
- public void fail(Throwable t)
- {
- Log.d("failed",t.getMessage());
- }
- };
- public UserManager()
- {
- }
- public String login(String username, String password, Callback loginCallback)
- {
- String outcome;
- UserClient client = ServiceGenerator.createService(UserClient.class);
- Call<User> call = client.getUserByName(username);
- call.enqueue(
- new Callback<User>() {
- @Override
- public void onResponse(Call<User> call, Response<User> response)
- {
- HERE IS WHERE THE RESPONSE IS , SO NOW YOU COULD PASS INFO TO A FUNCTION TO VALIDATE AND THEN FINALLY IF ALL IS GOOD DO SOMETHING LIKE
- loginCallback.handleMessage(user);
- //callback.successful(response);
- }
- @Override
- public void onFailure(Call<User> call, Throwable t)
- {
- Log.d("Error", "Error: " + t.getMessage());
- HERE IS WHERE THE Fail IS , DO SOMETHING LIKE
- getActivityMain().lOGINFAILED(user);
- }
- });
- return "success";
- }
- }
- package com.example.dmust.restaurantapp;
- import okhttp3.OkHttpClient;
- import retrofit2.Retrofit;
- import retrofit2.converter.gson.GsonConverterFactory;
- public class ServiceGenerator
- {
- private final static String API_BASE_URL = "http://10.0.2.2:5000/";
- private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
- private static Retrofit.Builder builder = new Retrofit.Builder()
- .baseUrl(API_BASE_URL)
- .addConverterFactory(GsonConverterFactory.create());
- private static Retrofit retrofit = builder.client(httpClient.build()).build();
- public static <S> S createService(Class<S> serviceClass)
- {
- return retrofit.create(serviceClass);
- }
- }
- package com.example.dmust.restaurantapp;
- import retrofit2.Response;
- public interface ServiceCallback
- {
- Response successful(Response response);
- void fail(Throwable t);
- }
- package com.example.dmust.restaurantapp;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.util.JsonReader;
- import android.util.Log;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.Spinner;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.List;
- import okhttp3.OkHttpClient;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- import retrofit2.Retrofit;
- import retrofit2.converter.gson.GsonConverterFactory;
- public class MainActivity extends AppCompatActivity
- {
- private TextView textView;
- private UserManager userManager;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- textView = (TextView) findViewById(R.id.textView);
- setSupportActionBar(toolbar);
- Spinner spinner = (Spinner) findViewById(R.id.spinner);
- // Create an ArrayAdapter using the string array and a default spinner layout
- ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
- R.array.user_types, android.R.layout.simple_spinner_item);
- // Specify the layout to use when the list of choices appears
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- // Apply the adapter to the spinner
- spinner.setAdapter(adapter);
- userManager = new UserManager();
- /*
- String API_BASE_URL = "http://10.0.2.2:5000/";
- OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
- Retrofit.Builder builder = new Retrofit.Builder()
- .baseUrl(API_BASE_URL)
- .addConverterFactory(GsonConverterFactory.create());
- Retrofit retrofit = builder.client(httpClient.build()).build();
- UserClient client = retrofit.create(UserClient.class);
- Call<List<User>> call = client.allUsers();
- call.enqueue(new Callback<List<User>>() {
- @Override
- public void onResponse(Call<List<User>> call, Response<List<User>> response)
- {
- List<User> users = response.body();
- textView.append(users.toString());
- }
- @Override
- public void onFailure(Call<List<User>> call, Throwable t)
- {
- Toast.makeText(MainActivity.this, "error", Toast.LENGTH_LONG).show();
- }
- });
- */
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- public void login(View view)
- {
- // String outcome;
- EditText username = (EditText) findViewById(R.id.login);
- EditText password = (EditText) findViewById(R.id.password);
- THIS WONT WORK IS THIS IS ASYNC - YOUR JUST TELLING THE USERMANAGER TO LOGIN()
- outcome = userManager.login(username.getText().toString(), password.getText().toString());
- CHANGE TO THIS
- userManager.login(username.getText().toString(), password.getText().toString());
- /*
- String API_BASE_URL = "http://10.0.2.2:5000/";
- OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
- Retrofit.Builder builder = new Retrofit.Builder()
- .baseUrl(API_BASE_URL)
- .addConverterFactory(GsonConverterFactory.create());
- Retrofit retrofit = builder.client(httpClient.build()).build();
- UserClient client = retrofit.create(UserClient.class);
- Call<User> call = client.getUserByName(username.getText().toString());
- call.enqueue(new Callback<User>() {
- @Override
- public void onResponse(Call<User> call, Response<User> response)
- {
- User user = response.body();
- textView.append(user.toString());
- }
- @Override
- public void onFailure(Call<User> call, Throwable t)
- {
- Toast.makeText(MainActivity.this, "error: " + t.getMessage(), Toast.LENGTH_LONG).show();
- }
- });
- */
- PUBLIC VOID lOGINSUCCESS()
- {
- Toast.makeText(MainActivity.this, "Logged in!", Toast.LENGTH_LONG).show();
- }
- PUBLIC VOID lOGINFAILED()
- {
- Toast.makeText(MainActivity.this, "FAILED!", Toast.LENGTH_LONG).show();
- }
- }
- }
- Pastebin PRO WINTER Special!
- Get 40% OFF Pastebin PRO accounts!
- create new paste / dealsnew! / api / trends / syntax languages / faq / tools / privacy / cookies / contact / dmca / scraping / go
- Site design & logo © 2017 Pastebin; user contributions (pastes) licensed under cc by-sa 3.0 -- Dedicated Server Hosting by Steadfast
- Top
- xSign Up For FreeCircleCI 2.0 is freaking fast. We reduced builds from minutes to 12 seconds.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement