Advertisement
Guest User

Dana

a guest
Jan 16th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.74 KB | None | 0 0
  1. package com.example.dmust.restaurantapp;
  2.  
  3. import android.util.Log;
  4. import android.widget.Toast;
  5.  
  6. import org.json.JSONArray;
  7. import org.json.JSONObject;
  8.  
  9. import java.io.IOException;
  10.  
  11. import okhttp3.OkHttpClient;
  12. import retrofit2.Call;
  13. import retrofit2.Callback;
  14. import retrofit2.Response;
  15. import retrofit2.Retrofit;
  16. import retrofit2.converter.gson.GsonConverterFactory;
  17.  
  18.  
  19. public class UserManager
  20. {
  21.     User user = null;
  22.     private ServiceCallback callback = new ServiceCallback() {
  23.         @Override
  24.         public Response successful(Response response)
  25.         {
  26.             if(response!=null)
  27.             {
  28.                 Log.d("response.body", response.body().toString());
  29.             }
  30.             else
  31.             {
  32.                 response = null;
  33.             }
  34.             return response;
  35.         }
  36.  
  37.         @Override
  38.         public void fail(Throwable t)
  39.         {
  40.             Log.d("failed",t.getMessage());
  41.         }
  42.     };
  43.  
  44.     public UserManager()
  45.     {
  46.     }
  47.  
  48.     public String login(String username, String password)
  49.     {
  50.         String outcome;
  51.         UserClient client = ServiceGenerator.createService(UserClient.class);
  52.         Call<User> call = client.getUserByName(username);
  53.  
  54.         call.enqueue(
  55.                 new Callback<User>() {
  56.             @Override
  57.             public void onResponse(Call<User> call, Response<User> response)
  58.             {
  59. //                user = response.body();
  60. //                if(user != null)
  61. //                {
  62. //                    Log.d("Got the user!", user.toString());
  63. //                }
  64.                 callback.successful(response);
  65.             }
  66.  
  67.             @Override
  68.             public void onFailure(Call<User> call, Throwable t)
  69.             {
  70.                 Log.d("Error", "Error: " + t.getMessage());
  71.                 callback.fail(t);
  72.             }
  73.         });
  74.  
  75.  
  76.  
  77. //        if(user != null)
  78. //        {
  79. //            if (user.getPassword().equalsIgnoreCase(password))
  80. //            {
  81. //                outcome = "success";
  82. //            }
  83. //            else
  84. //            {
  85. //                outcome = "failure";
  86. //            }
  87. //        }
  88. //        else
  89. //        {
  90. //            outcome = "failure";
  91. //        }
  92.         return "success";
  93.  
  94.     }
  95.  
  96. }
  97.  
  98. package com.example.dmust.restaurantapp;
  99.  
  100. import okhttp3.OkHttpClient;
  101. import retrofit2.Retrofit;
  102. import retrofit2.converter.gson.GsonConverterFactory;
  103.  
  104. public class ServiceGenerator
  105. {
  106.     private final static String API_BASE_URL = "http://10.0.2.2:5000/";
  107.     private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
  108.  
  109.     private static Retrofit.Builder builder = new Retrofit.Builder()
  110.             .baseUrl(API_BASE_URL)
  111.             .addConverterFactory(GsonConverterFactory.create());
  112.  
  113.     private static Retrofit retrofit = builder.client(httpClient.build()).build();
  114.  
  115.     public static <S> S createService(Class<S> serviceClass)
  116.     {
  117.         return retrofit.create(serviceClass);
  118.     }
  119. }
  120.  
  121. package com.example.dmust.restaurantapp;
  122.  
  123. import retrofit2.Response;
  124.  
  125. public interface ServiceCallback
  126. {
  127.     Response successful(Response response);
  128.     void fail(Throwable t);
  129. }
  130.  
  131. package com.example.dmust.restaurantapp;
  132.  
  133. import android.os.AsyncTask;
  134. import android.os.Bundle;
  135. import android.support.design.widget.FloatingActionButton;
  136. import android.support.design.widget.Snackbar;
  137. import android.support.v7.app.AppCompatActivity;
  138. import android.support.v7.widget.Toolbar;
  139. import android.util.JsonReader;
  140. import android.util.Log;
  141. import android.view.View;
  142. import android.view.Menu;
  143. import android.view.MenuItem;
  144. import android.widget.ArrayAdapter;
  145. import android.widget.Button;
  146. import android.widget.EditText;
  147. import android.widget.ListView;
  148. import android.widget.Spinner;
  149. import android.widget.TextView;
  150. import android.widget.Toast;
  151.  
  152. import org.json.JSONException;
  153. import org.json.JSONObject;
  154.  
  155. import java.io.IOException;
  156. import java.io.InputStream;
  157. import java.io.InputStreamReader;
  158. import java.net.HttpURLConnection;
  159. import java.net.MalformedURLException;
  160. import java.net.URL;
  161. import java.util.List;
  162.  
  163. import okhttp3.OkHttpClient;
  164. import retrofit2.Call;
  165. import retrofit2.Callback;
  166. import retrofit2.Response;
  167. import retrofit2.Retrofit;
  168. import retrofit2.converter.gson.GsonConverterFactory;
  169.  
  170. public class MainActivity extends AppCompatActivity
  171. {
  172.     private TextView textView;
  173.     private UserManager userManager;
  174.  
  175.     @Override
  176.     protected void onCreate(Bundle savedInstanceState)
  177.     {
  178.         super.onCreate(savedInstanceState);
  179.         setContentView(R.layout.activity_main);
  180.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  181.         textView = (TextView) findViewById(R.id.textView);
  182.         setSupportActionBar(toolbar);
  183.  
  184.         Spinner spinner = (Spinner) findViewById(R.id.spinner);
  185.         // Create an ArrayAdapter using the string array and a default spinner layout
  186.         ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
  187.                         R.array.user_types, android.R.layout.simple_spinner_item);
  188.         // Specify the layout to use when the list of choices appears
  189.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  190.         // Apply the adapter to the spinner
  191.         spinner.setAdapter(adapter);
  192.  
  193.         userManager = new UserManager();
  194.  
  195.         /*
  196.         String API_BASE_URL = "http://10.0.2.2:5000/";
  197.         OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
  198.  
  199.         Retrofit.Builder builder = new Retrofit.Builder()
  200.                 .baseUrl(API_BASE_URL)
  201.                 .addConverterFactory(GsonConverterFactory.create());
  202.  
  203.         Retrofit retrofit = builder.client(httpClient.build()).build();
  204.  
  205.         UserClient client = retrofit.create(UserClient.class);
  206.         Call<List<User>> call = client.allUsers();
  207.  
  208.         call.enqueue(new Callback<List<User>>() {
  209.             @Override
  210.             public void onResponse(Call<List<User>> call, Response<List<User>> response)
  211.             {
  212.                List<User> users = response.body();
  213.  
  214.                textView.append(users.toString());
  215.             }
  216.  
  217.             @Override
  218.             public void onFailure(Call<List<User>> call, Throwable t)
  219.             {
  220.                 Toast.makeText(MainActivity.this, "error", Toast.LENGTH_LONG).show();
  221.             }
  222.         });
  223.         */
  224.  
  225.     }
  226.  
  227.     @Override
  228.     public boolean onCreateOptionsMenu(Menu menu) {
  229.         // Inflate the menu; this adds items to the action bar if it is present.
  230.         getMenuInflater().inflate(R.menu.menu_main, menu);
  231.         return true;
  232.     }
  233.  
  234.     @Override
  235.     public boolean onOptionsItemSelected(MenuItem item) {
  236.         // Handle action bar item clicks here. The action bar will
  237.         // automatically handle clicks on the Home/Up button, so long
  238.         // as you specify a parent activity in AndroidManifest.xml.
  239.         int id = item.getItemId();
  240.  
  241.         //noinspection SimplifiableIfStatement
  242.         if (id == R.id.action_settings) {
  243.             return true;
  244.         }
  245.  
  246.         return super.onOptionsItemSelected(item);
  247.     }
  248.  
  249.     public void login(View view)
  250.     {
  251.         String outcome;
  252.         EditText username = (EditText) findViewById(R.id.login);
  253.         EditText password = (EditText) findViewById(R.id.password);
  254.  
  255.         outcome = userManager.login(username.getText().toString(), password.getText().toString());
  256.  
  257.         if(outcome == "success")
  258.         {
  259.             Toast.makeText(MainActivity.this, "Logged in!", Toast.LENGTH_LONG).show();
  260.         }
  261.         else
  262.         {
  263.             Toast.makeText(MainActivity.this, "Wrong login credentials!", Toast.LENGTH_LONG).show();
  264.         }
  265.         /*
  266.         String API_BASE_URL = "http://10.0.2.2:5000/";
  267.         OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
  268.  
  269.         Retrofit.Builder builder = new Retrofit.Builder()
  270.                 .baseUrl(API_BASE_URL)
  271.                 .addConverterFactory(GsonConverterFactory.create());
  272.  
  273.         Retrofit retrofit = builder.client(httpClient.build()).build();
  274.  
  275.         UserClient client = retrofit.create(UserClient.class);
  276.         Call<User> call = client.getUserByName(username.getText().toString());
  277.  
  278.         call.enqueue(new Callback<User>() {
  279.             @Override
  280.             public void onResponse(Call<User> call, Response<User> response)
  281.             {
  282.                 User user = response.body();
  283.  
  284.                 textView.append(user.toString());
  285.             }
  286.  
  287.             @Override
  288.             public void onFailure(Call<User> call, Throwable t)
  289.             {
  290.                 Toast.makeText(MainActivity.this, "error: " + t.getMessage(), Toast.LENGTH_LONG).show();
  291.             }
  292.         });
  293.         */
  294.  
  295.     }
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement