Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.15 KB | None | 0 0
  1. PASTEBINnew pastetrends API tools faq
  2. search...
  3.  
  4.  
  5. Guest User
  6. -
  7.  
  8. Public Pastes
  9. Untitled
  10. 11 sec ago
  11. ssr address 2018.01
  12. 13 sec ago
  13. Untitled
  14. 24 sec ago
  15. Untitled
  16. 29 sec ago
  17. Untitled
  18. 30 sec ago
  19. Untitled
  20. 34 sec ago
  21. Untitled
  22. 38 sec ago
  23. Untitled
  24. 40 sec ago
  25. daily pastebin goal
  26. 29%
  27. help support pastebin
  28.  
  29. SHARE
  30. TWEET
  31.  
  32. Untitled
  33. A GUEST JAN 16TH, 2018 49 NEVER
  34.  
  35. NOTE: Your guest paste has been posted. If you sign up for a free account, you can edit and delete your pastes!
  36. rawdownloadcloneembedreportprint text 9.21 KB
  37. package com.example.dmust.restaurantapp;
  38.  
  39. import android.util.Log;
  40. import android.widget.Toast;
  41.  
  42. import org.json.JSONArray;
  43. import org.json.JSONObject;
  44.  
  45. import java.io.IOException;
  46.  
  47. import okhttp3.OkHttpClient;
  48. import retrofit2.Call;
  49. import retrofit2.Callback;
  50. import retrofit2.Response;
  51. import retrofit2.Retrofit;
  52. import retrofit2.converter.gson.GsonConverterFactory;
  53.  
  54.  
  55. public class UserManager
  56. {
  57. User user = null;
  58. private ServiceCallback callback = new ServiceCallback() { //POSSIBLE FAILS AS ITS A SERVICECALLBACK AND NOT THE RETROFIT CALLBACK??- BUT REMOVE ANYWAYS
  59. @Override
  60. public Response successful(Response response)
  61. {
  62. if(response!=null)
  63. {
  64. Log.d("response.body", response.body().toString());
  65. }
  66. else
  67. {
  68. response = null;
  69. }
  70. return response;
  71. }
  72.  
  73. @Override
  74. public void fail(Throwable t)
  75. {
  76. Log.d("failed",t.getMessage());
  77. }
  78. };
  79.  
  80. public UserManager()
  81. {
  82. }
  83.  
  84. public String login(String username, String password)
  85. {
  86. String outcome;
  87. UserClient client = ServiceGenerator.createService(UserClient.class);
  88. Call<User> call = client.getUserByName(username);
  89.  
  90. call.enqueue(
  91. new Callback<User>() {
  92. @Override
  93. public void onResponse(Call<User> call, Response<User> response)
  94. {
  95. // user = response.body();
  96. // if(user != null)
  97. // {
  98. // Log.d("Got the user!", user.toString());
  99. // }
  100.  
  101. 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
  102. getActivityMain().lOGINSUCCESS(user);
  103. //callback.successful(response);
  104. }
  105.  
  106. @Override
  107. public void onFailure(Call<User> call, Throwable t)
  108. {
  109. Log.d("Error", "Error: " + t.getMessage());
  110. HERE IS WHERE THE Fail IS , DO SOMETHING LIKE
  111. getActivityMain().lOGINFAILED(user);
  112.  
  113. // callback.fail(t);
  114. }
  115. });
  116.  
  117.  
  118.  
  119. // if(user != null)
  120. // {
  121. // if (user.getPassword().equalsIgnoreCase(password))
  122. // {
  123. // outcome = "success";
  124. // }
  125. // else
  126. // {
  127. // outcome = "failure";
  128. // }
  129. // }
  130. // else
  131. // {
  132. // outcome = "failure";
  133. // }
  134. return "success";
  135.  
  136. }
  137.  
  138. }
  139.  
  140. package com.example.dmust.restaurantapp;
  141.  
  142. import okhttp3.OkHttpClient;
  143. import retrofit2.Retrofit;
  144. import retrofit2.converter.gson.GsonConverterFactory;
  145.  
  146. public class ServiceGenerator
  147. {
  148. private final static String API_BASE_URL = "http://10.0.2.2:5000/";
  149. private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
  150.  
  151. private static Retrofit.Builder builder = new Retrofit.Builder()
  152. .baseUrl(API_BASE_URL)
  153. .addConverterFactory(GsonConverterFactory.create());
  154.  
  155. private static Retrofit retrofit = builder.client(httpClient.build()).build();
  156.  
  157. public static <S> S createService(Class<S> serviceClass)
  158. {
  159. return retrofit.create(serviceClass);
  160. }
  161. }
  162.  
  163. package com.example.dmust.restaurantapp;
  164.  
  165. import retrofit2.Response;
  166.  
  167. public interface ServiceCallback
  168. {
  169. Response successful(Response response);
  170. void fail(Throwable t);
  171. }
  172.  
  173. package com.example.dmust.restaurantapp;
  174.  
  175. import android.os.AsyncTask;
  176. import android.os.Bundle;
  177. import android.support.design.widget.FloatingActionButton;
  178. import android.support.design.widget.Snackbar;
  179. import android.support.v7.app.AppCompatActivity;
  180. import android.support.v7.widget.Toolbar;
  181. import android.util.JsonReader;
  182. import android.util.Log;
  183. import android.view.View;
  184. import android.view.Menu;
  185. import android.view.MenuItem;
  186. import android.widget.ArrayAdapter;
  187. import android.widget.Button;
  188. import android.widget.EditText;
  189. import android.widget.ListView;
  190. import android.widget.Spinner;
  191. import android.widget.TextView;
  192. import android.widget.Toast;
  193.  
  194. import org.json.JSONException;
  195. import org.json.JSONObject;
  196.  
  197. import java.io.IOException;
  198. import java.io.InputStream;
  199. import java.io.InputStreamReader;
  200. import java.net.HttpURLConnection;
  201. import java.net.MalformedURLException;
  202. import java.net.URL;
  203. import java.util.List;
  204.  
  205. import okhttp3.OkHttpClient;
  206. import retrofit2.Call;
  207. import retrofit2.Callback;
  208. import retrofit2.Response;
  209. import retrofit2.Retrofit;
  210. import retrofit2.converter.gson.GsonConverterFactory;
  211.  
  212. public class MainActivity extends AppCompatActivity
  213. {
  214. private TextView textView;
  215. private UserManager userManager;
  216.  
  217. Callback loginCallback;
  218. @Override
  219. protected void onCreate(Bundle savedInstanceState)
  220. {
  221. super.onCreate(savedInstanceState);
  222. setContentView(R.layout.activity_main);
  223. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  224. textView = (TextView) findViewById(R.id.textView);
  225. setSupportActionBar(toolbar);
  226.  
  227. Spinner spinner = (Spinner) findViewById(R.id.spinner);
  228. // Create an ArrayAdapter using the string array and a default spinner layout
  229. ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
  230. R.array.user_types, android.R.layout.simple_spinner_item);
  231. // Specify the layout to use when the list of choices appears
  232. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  233. // Apply the adapter to the spinner
  234. spinner.setAdapter(adapter);
  235.  
  236. userManager = new UserManager();
  237.  
  238. /*
  239. String API_BASE_URL = "http://10.0.2.2:5000/";
  240. OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
  241.  
  242. Retrofit.Builder builder = new Retrofit.Builder()
  243. .baseUrl(API_BASE_URL)
  244. .addConverterFactory(GsonConverterFactory.create());
  245.  
  246. Retrofit retrofit = builder.client(httpClient.build()).build();
  247.  
  248. UserClient client = retrofit.create(UserClient.class);
  249. Call<List<User>> call = client.allUsers();
  250.  
  251. call.enqueue(new Callback<List<User>>() {
  252. @Override
  253. public void onResponse(Call<List<User>> call, Response<List<User>> response)
  254. {
  255. List<User> users = response.body();
  256.  
  257. textView.append(users.toString());
  258. }
  259.  
  260. @Override
  261. public void onFailure(Call<List<User>> call, Throwable t)
  262. {
  263. Toast.makeText(MainActivity.this, "error", Toast.LENGTH_LONG).show();
  264. }
  265. });
  266. */
  267.  
  268. }
  269.  
  270. @Override
  271. public boolean onCreateOptionsMenu(Menu menu) {
  272. // Inflate the menu; this adds items to the action bar if it is present.
  273. getMenuInflater().inflate(R.menu.menu_main, menu);
  274. return true;
  275. }
  276.  
  277. @Override
  278. public boolean onOptionsItemSelected(MenuItem item) {
  279. // Handle action bar item clicks here. The action bar will
  280. // automatically handle clicks on the Home/Up button, so long
  281. // as you specify a parent activity in AndroidManifest.xml.
  282. int id = item.getItemId();
  283.  
  284. //noinspection SimplifiableIfStatement
  285. if (id == R.id.action_settings) {
  286. return true;
  287. }
  288.  
  289. return super.onOptionsItemSelected(item);
  290. }
  291.  
  292. public void login(View view)
  293. {
  294. // String outcome;
  295. EditText username = (EditText) findViewById(R.id.login);
  296. EditText password = (EditText) findViewById(R.id.password);
  297.  
  298. THIS WONT WORK IS THIS IS ASYNC - YOUR JUST TELLING THE USERMANAGER TO LOGIN()
  299. outcome = userManager.login(username.getText().toString(), password.getText().toString());
  300.  
  301. CHANGE TO THIS
  302. loginCallback = new Callback()
  303. {
  304. public boolean handleMessage(User user) {
  305. lOGINSUCCESS(user);
  306. }
  307. };
  308. userManager.login(username.getText().toString(), password.getText().toString(),loginCallback);
  309. /*
  310. String API_BASE_URL = "http://10.0.2.2:5000/";
  311. OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
  312.  
  313. Retrofit.Builder builder = new Retrofit.Builder()
  314. .baseUrl(API_BASE_URL)
  315. .addConverterFactory(GsonConverterFactory.create());
  316.  
  317. Retrofit retrofit = builder.client(httpClient.build()).build();
  318.  
  319. UserClient client = retrofit.create(UserClient.class);
  320. Call<User> call = client.getUserByName(username.getText().toString());
  321.  
  322. call.enqueue(new Callback<User>() {
  323. @Override
  324. public void onResponse(Call<User> call, Response<User> response)
  325. {
  326. User user = response.body();
  327.  
  328. textView.append(user.toString());
  329. }
  330.  
  331. @Override
  332. public void onFailure(Call<User> call, Throwable t)
  333. {
  334. Toast.makeText(MainActivity.this, "error: " + t.getMessage(), Toast.LENGTH_LONG).show();
  335. }
  336. });
  337. */
  338.  
  339. PUBLIC VOID lOGINSUCCESS()
  340. {
  341. Toast.makeText(MainActivity.this, "Logged in!", Toast.LENGTH_LONG).show();
  342. }
  343.  
  344. PUBLIC VOID lOGINFAILED()
  345. {
  346. Toast.makeText(MainActivity.this, "FAILED!", Toast.LENGTH_LONG).show();
  347. }
  348.  
  349.  
  350. }
  351. }
  352. RAW Paste Data
  353.  
  354. package com.example.dmust.restaurantapp;
  355.  
  356. import android.util.Log;
  357. import android.widget.Toast;
  358.  
  359. import org.json.JSONArray;
  360. import org.json.JSONObject;
  361.  
  362. import java.io.IOException;
  363.  
  364. import okhttp3.OkHttpClient;
  365. import retrofit2.Call;
  366. import retrofit2.Callback;
  367. import retrofit2.Response;
  368. import retrofit2.Retrofit;
  369. import retrofit2.converter.gson.GsonConverterFactory;
  370.  
  371.  
  372. public class UserManager
  373. {
  374. User user = null;
  375. private ServiceCallback callback = new ServiceCallback() { //POSSIBLE FAILS AS ITS A SERVICECALLBACK AND NOT THE RETROFIT CALLBACK??- BUT REMOVE ANYWAYS
  376. @Override
  377. public Response successful(Response response)
  378. {
  379. if(response!=null)
  380. {
  381. Log.d("response.body", response.body().toString());
  382. }
  383. else
  384. {
  385. response = null;
  386. }
  387. return response;
  388. }
  389.  
  390. @Override
  391. public void fail(Throwable t)
  392. {
  393. Log.d("failed",t.getMessage());
  394. }
  395. };
  396.  
  397. public UserManager()
  398. {
  399. }
  400.  
  401. public String login(String username, String password, Callback loginCallback)
  402. {
  403. String outcome;
  404. UserClient client = ServiceGenerator.createService(UserClient.class);
  405. Call<User> call = client.getUserByName(username);
  406.  
  407. call.enqueue(
  408. new Callback<User>() {
  409. @Override
  410. public void onResponse(Call<User> call, Response<User> response)
  411. {
  412. 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
  413. loginCallback.handleMessage(user);
  414. //callback.successful(response);
  415. }
  416.  
  417. @Override
  418. public void onFailure(Call<User> call, Throwable t)
  419. {
  420. Log.d("Error", "Error: " + t.getMessage());
  421. HERE IS WHERE THE Fail IS , DO SOMETHING LIKE
  422. getActivityMain().lOGINFAILED(user);
  423.  
  424. }
  425. });
  426. return "success";
  427.  
  428. }
  429.  
  430. }
  431.  
  432. package com.example.dmust.restaurantapp;
  433.  
  434. import okhttp3.OkHttpClient;
  435. import retrofit2.Retrofit;
  436. import retrofit2.converter.gson.GsonConverterFactory;
  437.  
  438. public class ServiceGenerator
  439. {
  440. private final static String API_BASE_URL = "http://10.0.2.2:5000/";
  441. private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
  442.  
  443. private static Retrofit.Builder builder = new Retrofit.Builder()
  444. .baseUrl(API_BASE_URL)
  445. .addConverterFactory(GsonConverterFactory.create());
  446.  
  447. private static Retrofit retrofit = builder.client(httpClient.build()).build();
  448.  
  449. public static <S> S createService(Class<S> serviceClass)
  450. {
  451. return retrofit.create(serviceClass);
  452. }
  453. }
  454.  
  455. package com.example.dmust.restaurantapp;
  456.  
  457. import retrofit2.Response;
  458.  
  459. public interface ServiceCallback
  460. {
  461. Response successful(Response response);
  462. void fail(Throwable t);
  463. }
  464.  
  465. package com.example.dmust.restaurantapp;
  466.  
  467. import android.os.AsyncTask;
  468. import android.os.Bundle;
  469. import android.support.design.widget.FloatingActionButton;
  470. import android.support.design.widget.Snackbar;
  471. import android.support.v7.app.AppCompatActivity;
  472. import android.support.v7.widget.Toolbar;
  473. import android.util.JsonReader;
  474. import android.util.Log;
  475. import android.view.View;
  476. import android.view.Menu;
  477. import android.view.MenuItem;
  478. import android.widget.ArrayAdapter;
  479. import android.widget.Button;
  480. import android.widget.EditText;
  481. import android.widget.ListView;
  482. import android.widget.Spinner;
  483. import android.widget.TextView;
  484. import android.widget.Toast;
  485.  
  486. import org.json.JSONException;
  487. import org.json.JSONObject;
  488.  
  489. import java.io.IOException;
  490. import java.io.InputStream;
  491. import java.io.InputStreamReader;
  492. import java.net.HttpURLConnection;
  493. import java.net.MalformedURLException;
  494. import java.net.URL;
  495. import java.util.List;
  496.  
  497. import okhttp3.OkHttpClient;
  498. import retrofit2.Call;
  499. import retrofit2.Callback;
  500. import retrofit2.Response;
  501. import retrofit2.Retrofit;
  502. import retrofit2.converter.gson.GsonConverterFactory;
  503.  
  504. public class MainActivity extends AppCompatActivity
  505. {
  506. private TextView textView;
  507. private UserManager userManager;
  508.  
  509. @Override
  510. protected void onCreate(Bundle savedInstanceState)
  511. {
  512. super.onCreate(savedInstanceState);
  513. setContentView(R.layout.activity_main);
  514. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  515. textView = (TextView) findViewById(R.id.textView);
  516. setSupportActionBar(toolbar);
  517.  
  518. Spinner spinner = (Spinner) findViewById(R.id.spinner);
  519. // Create an ArrayAdapter using the string array and a default spinner layout
  520. ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
  521. R.array.user_types, android.R.layout.simple_spinner_item);
  522. // Specify the layout to use when the list of choices appears
  523. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  524. // Apply the adapter to the spinner
  525. spinner.setAdapter(adapter);
  526.  
  527. userManager = new UserManager();
  528.  
  529. /*
  530. String API_BASE_URL = "http://10.0.2.2:5000/";
  531. OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
  532.  
  533. Retrofit.Builder builder = new Retrofit.Builder()
  534. .baseUrl(API_BASE_URL)
  535. .addConverterFactory(GsonConverterFactory.create());
  536.  
  537. Retrofit retrofit = builder.client(httpClient.build()).build();
  538.  
  539. UserClient client = retrofit.create(UserClient.class);
  540. Call<List<User>> call = client.allUsers();
  541.  
  542. call.enqueue(new Callback<List<User>>() {
  543. @Override
  544. public void onResponse(Call<List<User>> call, Response<List<User>> response)
  545. {
  546. List<User> users = response.body();
  547.  
  548. textView.append(users.toString());
  549. }
  550.  
  551. @Override
  552. public void onFailure(Call<List<User>> call, Throwable t)
  553. {
  554. Toast.makeText(MainActivity.this, "error", Toast.LENGTH_LONG).show();
  555. }
  556. });
  557. */
  558.  
  559. }
  560.  
  561. @Override
  562. public boolean onCreateOptionsMenu(Menu menu) {
  563. // Inflate the menu; this adds items to the action bar if it is present.
  564. getMenuInflater().inflate(R.menu.menu_main, menu);
  565. return true;
  566. }
  567.  
  568. @Override
  569. public boolean onOptionsItemSelected(MenuItem item) {
  570. // Handle action bar item clicks here. The action bar will
  571. // automatically handle clicks on the Home/Up button, so long
  572. // as you specify a parent activity in AndroidManifest.xml.
  573. int id = item.getItemId();
  574.  
  575. //noinspection SimplifiableIfStatement
  576. if (id == R.id.action_settings) {
  577. return true;
  578. }
  579.  
  580. return super.onOptionsItemSelected(item);
  581. }
  582.  
  583. public void login(View view)
  584. {
  585. // String outcome;
  586. EditText username = (EditText) findViewById(R.id.login);
  587. EditText password = (EditText) findViewById(R.id.password);
  588.  
  589. THIS WONT WORK IS THIS IS ASYNC - YOUR JUST TELLING THE USERMANAGER TO LOGIN()
  590. outcome = userManager.login(username.getText().toString(), password.getText().toString());
  591.  
  592. CHANGE TO THIS
  593. userManager.login(username.getText().toString(), password.getText().toString());
  594. /*
  595. String API_BASE_URL = "http://10.0.2.2:5000/";
  596. OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
  597.  
  598. Retrofit.Builder builder = new Retrofit.Builder()
  599. .baseUrl(API_BASE_URL)
  600. .addConverterFactory(GsonConverterFactory.create());
  601.  
  602. Retrofit retrofit = builder.client(httpClient.build()).build();
  603.  
  604. UserClient client = retrofit.create(UserClient.class);
  605. Call<User> call = client.getUserByName(username.getText().toString());
  606.  
  607. call.enqueue(new Callback<User>() {
  608. @Override
  609. public void onResponse(Call<User> call, Response<User> response)
  610. {
  611. User user = response.body();
  612.  
  613. textView.append(user.toString());
  614. }
  615.  
  616. @Override
  617. public void onFailure(Call<User> call, Throwable t)
  618. {
  619. Toast.makeText(MainActivity.this, "error: " + t.getMessage(), Toast.LENGTH_LONG).show();
  620. }
  621. });
  622. */
  623.  
  624. PUBLIC VOID lOGINSUCCESS()
  625. {
  626. Toast.makeText(MainActivity.this, "Logged in!", Toast.LENGTH_LONG).show();
  627. }
  628.  
  629. PUBLIC VOID lOGINFAILED()
  630. {
  631. Toast.makeText(MainActivity.this, "FAILED!", Toast.LENGTH_LONG).show();
  632. }
  633.  
  634.  
  635. }
  636. }
  637.  
  638. Pastebin PRO WINTER Special!
  639. Get 40% OFF Pastebin PRO accounts!
  640.  
  641. create new paste / dealsnew! / api / trends / syntax languages / faq / tools / privacy / cookies / contact / dmca / scraping / go
  642. Site design & logo © 2017 Pastebin; user contributions (pastes) licensed under cc by-sa 3.0 -- Dedicated Server Hosting by Steadfast
  643.  
  644. Top
  645. 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