Advertisement
uraharadono

HCI ispit

Aug 31st, 2016
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.46 KB | None | 0 0
  1. Button btnLogin = (Button) findViewById(R.id.loginBtn);
  2.         btnLogin.setOnClickListener(new View.OnClickListener() {
  3.             @Override
  4.             public void onClick(View view) {
  5.                 do_btnLogin_click();
  6.             }
  7.         });
  8. ___________________________________________________________________________________________________________
  9.  
  10. private EditText txtUsername;
  11. txtUsername = (EditText) findViewById(R.id.usernameInput);
  12. ___________________________________________________________________________________________________________
  13.  
  14. Toast.makeText(MainActivity.this, "Molimo popunite sva polja.", Toast.LENGTH_LONG).show();
  15. ___________________________________________________________________________________________________________
  16.  
  17. ProgressDialog progressDialog;
  18. progressDialog = ProgressDialog.show(MainActivity.this, "Logiranje", "U toku");
  19. progressDialog.show();
  20. ___________________________________________________________________________________________________________
  21. new AsyncTask<Void, Void, AutentifikacijaProvjeraViewModel>() {
  22.             ProgressDialog progressDialog;
  23.  
  24.             @Override
  25.             protected void onPreExecute() {
  26.                 progressDialog = ProgressDialog.show(MainActivity.this, "Logiranje", "U toku");
  27.                 progressDialog.show();
  28.             }
  29.  
  30.             @Override
  31.             protected AutentifikacijaProvjeraViewModel doInBackground(Void... params) {
  32.                 return AutentifikacijaApi.Provjera(txtUsername.getText().toString(), txtPassword.getText().toString());
  33.             }
  34.  
  35.  
  36.             @Override
  37.             protected void onPostExecute(AutentifikacijaProvjeraViewModel result) {
  38.                 if (result.Id == 0) {
  39.                     Toast.makeText(MainActivity.this, "Korisnik ne postoji!", Toast.LENGTH_LONG).show();
  40.                 } else {
  41.                     Toast.makeText(MainActivity.this, "Welcome back " + result.Ime, Toast.LENGTH_LONG).show();
  42.                     Global.isThisInitialLoad = true;
  43.                     Global.korisnik = result;
  44.                     Intent i = new Intent(getApplicationContext(), PocetniActivity.class);
  45.                     startActivity(i);
  46.                 }
  47.                 progressDialog.dismiss();
  48.             }
  49.         }.execute();
  50.  
  51. ___________________________________________________________________________________________________________
  52.  
  53.     public static AutentifikacijaProvjeraViewModel Provjera(String username, String password)
  54.     {
  55.         String strJson= HttpManager.responseGet("http://192.168.0.15:21951/api/autentikacija/provjera",
  56.                 new BasicNameValuePair("email",username),
  57.                 new BasicNameValuePair("password",password)
  58.         );
  59.  
  60.         //System.out.println("Ovo ispod je JSON string");
  61.         //System.out.println(strJson);
  62.  
  63.         Gson gson= new Gson();
  64.         AutentifikacijaProvjeraViewModel x = gson.fromJson(strJson, AutentifikacijaProvjeraViewModel.class);
  65.  
  66.         return x;
  67.  
  68.  
  69.     }
  70. ___________________________________________________________________________________________________________
  71. public class HttpManager {
  72.     public static String responseGet(String url, NameValuePair... inputParams) {
  73.         String urlParams = URLEncodedUtils.format(Arrays.asList(inputParams), "utf-8");
  74.         HttpGet httpGet = new HttpGet(url + "?" + urlParams);
  75.  
  76.         DefaultHttpClient client = new DefaultHttpClient();
  77.  
  78.  
  79.         try {
  80.             HttpResponse response = client.execute(httpGet);
  81.             //InputStream stream= response.getEntity().getContent();
  82.             HttpEntity entity = response.getEntity();
  83.             InputStream stream = entity.getContent();
  84.  
  85.             String convertedFromStreamToJsonString = convertStreamToString(stream);
  86.  
  87.             //System.out.println(convertedFromStreamToJsonString);
  88.             stream.close();
  89.  
  90.             //return convertStreamToString(stream);
  91.             return convertedFromStreamToJsonString;
  92.  
  93.         } catch (IOException e) {
  94.             Log.e("Error u HttpManageru", e.getMessage());
  95.             return null;
  96.               }
  97.     }
  98.     public static String convertStreamToString(InputStream inputStream) throws IOException {
  99.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  100.         StringBuilder stringBuilder = new StringBuilder();
  101.         String line = null;
  102.  
  103.         while ((line = bufferedReader.readLine()) != null) {
  104.             stringBuilder.append(line + "\n");
  105.         }
  106.         return stringBuilder.toString();
  107.     }
  108. }
  109. ___________________________________________________________________________________________________________
  110. private void do_btnOstaviUtisak_click(String utisak, int utisakBroj) {
  111.         String message = "";
  112.  
  113.         final UtisakViewModel utisakForPost = new UtisakViewModel();
  114.         utisakForPost.KorisnikId = Global.korisnik.Id;
  115.         utisakForPost.Misljenje = utisak;
  116.         utisakForPost.Rating = utisakBroj;
  117.         utisakForPost.ProizvodId = Global.trenutnoSelektovaniProizvod.Id;
  118.  
  119.         //if (UtisciApi.PostUtisak(utisakForPost))
  120.         //    message = "Utisak uspjesno dodan";
  121.         //else
  122.         //    message = "Greska! Utisak nije dodan.";
  123.  
  124.  
  125.         RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
  126.         StringRequest MyStringRequest = new StringRequest(Request.Method.POST, "http://192.168.0.15:21951/api/Utisci/PostUtisak/", new Response.Listener<String>() {
  127.             @Override
  128.             public void onResponse(String response) {
  129.                 //This code is executed if the server responds, whether or not the response contains data.
  130.                 //The String 'response' contains the server's response.
  131.                 Toast.makeText(OstaviUtisakActivity.this, "Utisak uspjesno ostavljen.", Toast.LENGTH_LONG).show();
  132.                 Intent i = new Intent(getApplicationContext(), ProizvodDetailjiActivity.class);
  133.                 i.putExtra("proizvodId", String.valueOf(Global.trenutnoSelektovaniProizvod.Id));
  134.                 startActivity(i);
  135.             }
  136.         }, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
  137.             @Override
  138.             public void onErrorResponse(VolleyError error) {
  139.                 //This code is executed if there is an error.
  140.                 Toast.makeText(OstaviUtisakActivity.this, "Greska! Utisak nije spasen, molimo pokusajte kasnije.", Toast.LENGTH_LONG).show();
  141.             }
  142.         }) {
  143.             protected Map<String, String> getParams() {
  144.                 Map<String, String> MyData = new HashMap<String, String>();
  145.                 //MyData.put("Field", "Value"); //Add the data you'd like to send to the server.
  146.                 MyData.put("Id", "0");
  147.                 MyData.put("IsDeleted", "0");
  148.                 MyData.put("Misljenje", utisakForPost.Misljenje);
  149.                 MyData.put("Rating", String.valueOf(utisakForPost.Rating));
  150.                 MyData.put("KorisnikId", String.valueOf(utisakForPost.KorisnikId));
  151.                 MyData.put("ProizvodId", String.valueOf(utisakForPost.ProizvodId));
  152.  
  153.                 return MyData;
  154.             }
  155.         };
  156.         MyRequestQueue.add(MyStringRequest);
  157.     }
  158. ___________________________________________________________________________________________________________
  159. LINKOVI:
  160.  
  161. http://www.tutorialsbuzz.com/2014/06/android-dialogfragment-listview.html   - za onaj dialog fragment sto je bio na prosloom roku
  162. http://www.androidhive.info/2011/08/how-to-switch-between-activities-in-android/  - Intent
  163. http://www.androidinterview.com/android-custom-listview-with-image-and-text-using-arrayadapter/ -listview sa slikama
  164.  
  165. _______________________________VISE PARAMETARA U ROUTE NA API CONTROLERU___________________________________
  166. http://stackoverflow.com/questions/10937524/how-should-i-pass-multiple-parameters-to-an-asp-net-web-api-get
  167. --odgovor od Mark Pieszak
  168.  
  169. -inace sam googlao ovako
  170. https://www.google.ba/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=set%20route%20for%20api%20controller%20with%202%20parameters
  171.  
  172. ___________________________________________________________________________________________________________
  173. ___________________________________________________________________________________________________________
  174. ___________________________________________________________________________________________________________
  175. ___________________________________________________________________________________________________________
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement