Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. package com.tkordic.myapp;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.widget.TextView;
  7. import android.widget.Toast;
  8.  
  9. import com.android.volley.Request;
  10. import com.android.volley.RequestQueue;
  11. import com.android.volley.Response;
  12. import com.android.volley.VolleyError;
  13. import com.android.volley.toolbox.StringRequest;
  14. import com.android.volley.toolbox.Volley;
  15.  
  16. import org.json.JSONArray;
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19.  
  20. public class MainActivity extends AppCompatActivity {
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.  
  27.         final TextView mTextView = (TextView) findViewById(R.id.text);
  28.  
  29.         // Instantiate the RequestQueue.
  30.         RequestQueue queue = Volley.newRequestQueue(this);
  31.         String url ="http://192.168.1.51:8080/rasporedi";
  32.  
  33.         // Request a string response from the provided URL.
  34.         StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
  35.                 new Response.Listener<String>() {
  36.                     @Override
  37.                     public void onResponse(String response) {
  38.  
  39.                         try {
  40.  
  41.                             JSONArray jsonArray = new JSONArray(response);
  42.  
  43.                             for (int i = 0; i < jsonArray.length(); i++) {
  44.  
  45.                                 JSONObject c = jsonArray.getJSONObject(i);
  46.  
  47.                                 String id = c.getString("id");
  48.                                 String nastavnik = c.getString("nastavnik");
  49.                                 String predmet = c.getString("predmet");
  50.                                 String ucionica = c.getString("ucionica");
  51.                                 String vrijeme_od = c.getString("vrijeme_od");
  52.                                 String vrijeme_do = c.getString("vrijeme_do");
  53.  
  54.                                 Toast.makeText(getApplicationContext(), nastavnik, Toast.LENGTH_LONG).show();
  55.  
  56.                             }
  57.                         } catch (final JSONException e) {
  58.                             Log.e("Error", "Json parsing error: " + e.getMessage());
  59.                             runOnUiThread(new Runnable() {
  60.                                 @Override
  61.                                 public void run() {
  62.                                     Toast.makeText(getApplicationContext(), "Json parsing error: " + e.getMessage(), Toast.LENGTH_LONG).show();
  63.                                 }
  64.                             });
  65.  
  66.                         }
  67.  
  68.                     }
  69.                 }, new Response.ErrorListener() {
  70.             @Override
  71.             public void onErrorResponse(VolleyError error) {
  72.                 mTextView.setText("That didn't work!");
  73.             }
  74.         });
  75.  
  76.         // Add the request to the RequestQueue.
  77.         queue.add(stringRequest);
  78.  
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement