Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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() {
- @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());
- // }
- callback.successful(response);
- }
- @Override
- public void onFailure(Call<User> call, Throwable t)
- {
- Log.d("Error", "Error: " + t.getMessage());
- 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;
- @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);
- outcome = userManager.login(username.getText().toString(), password.getText().toString());
- if(outcome == "success")
- {
- Toast.makeText(MainActivity.this, "Logged in!", Toast.LENGTH_LONG).show();
- }
- else
- {
- Toast.makeText(MainActivity.this, "Wrong login credentials!", Toast.LENGTH_LONG).show();
- }
- /*
- 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();
- }
- });
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement