Commit 0d01d62f115ca0201f50a0c149435575ccd03371
1 parent
c30d084a
Periodic HTTP request
Send an HTTP request every second and update the view according to the response
Showing
4 changed files
with
63 additions
and
0 deletions
Show diff stats
app/build.gradle
@@ -30,5 +30,6 @@ dependencies { | @@ -30,5 +30,6 @@ dependencies { | ||
30 | compile 'com.android.support:design:25.3.1' | 30 | compile 'com.android.support:design:25.3.1' |
31 | compile 'com.android.support:support-v4:25.3.1' | 31 | compile 'com.android.support:support-v4:25.3.1' |
32 | compile 'com.android.support:support-vector-drawable:25.3.1' | 32 | compile 'com.android.support:support-vector-drawable:25.3.1' |
33 | + compile 'com.android.volley:volley:1.0.0' | ||
33 | testCompile 'junit:junit:4.12' | 34 | testCompile 'junit:junit:4.12' |
34 | } | 35 | } |
app/src/main/AndroidManifest.xml
@@ -2,6 +2,7 @@ | @@ -2,6 +2,7 @@ | ||
2 | <manifest xmlns:android="http://schemas.android.com/apk/res/android" | 2 | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
3 | package="fr.utbm.lo53.p2017.positionningapp"> | 3 | package="fr.utbm.lo53.p2017.positionningapp"> |
4 | 4 | ||
5 | + <uses-permission android:name="android.permission.INTERNET" /> | ||
5 | <application | 6 | <application |
6 | android:allowBackup="true" | 7 | android:allowBackup="true" |
7 | android:icon="@mipmap/ic_launcher" | 8 | android:icon="@mipmap/ic_launcher" |
app/src/main/java/fr/utbm/lo53/p2017/positionningapp/PositioningActivity.java
1 | package fr.utbm.lo53.p2017.positionningapp; | 1 | package fr.utbm.lo53.p2017.positionningapp; |
2 | 2 | ||
3 | import android.os.Bundle; | 3 | import android.os.Bundle; |
4 | +import android.os.Handler; | ||
4 | import android.support.constraint.ConstraintLayout; | 5 | import android.support.constraint.ConstraintLayout; |
5 | import android.support.v7.widget.Toolbar; | 6 | import android.support.v7.widget.Toolbar; |
7 | +import android.util.Log; | ||
6 | import android.view.Menu; | 8 | import android.view.Menu; |
7 | import android.view.MenuItem; | 9 | import android.view.MenuItem; |
8 | import android.view.View; | 10 | import android.view.View; |
@@ -10,14 +12,55 @@ import android.view.animation.AccelerateInterpolator; | @@ -10,14 +12,55 @@ import android.view.animation.AccelerateInterpolator; | ||
10 | import android.view.animation.AlphaAnimation; | 12 | import android.view.animation.AlphaAnimation; |
11 | import android.view.animation.Animation; | 13 | import android.view.animation.Animation; |
12 | import android.widget.ImageView; | 14 | import android.widget.ImageView; |
15 | +import android.widget.TextView; | ||
16 | + | ||
17 | +import com.android.volley.Request; | ||
18 | +import com.android.volley.RequestQueue; | ||
19 | +import com.android.volley.Response; | ||
20 | +import com.android.volley.VolleyError; | ||
21 | +import com.android.volley.toolbox.StringRequest; | ||
22 | +import com.android.volley.toolbox.Volley; | ||
13 | 23 | ||
14 | public class PositioningActivity extends BaseActivity { | 24 | public class PositioningActivity extends BaseActivity { |
15 | 25 | ||
16 | private static final String TAG = "PositioningActivity"; | 26 | private static final String TAG = "PositioningActivity"; |
17 | 27 | ||
18 | private ConstraintLayout start_layout; | 28 | private ConstraintLayout start_layout; |
29 | + private TextView textView; | ||
30 | + | ||
19 | private Animation hideAnimation; | 31 | private Animation hideAnimation; |
20 | 32 | ||
33 | + // Instantiate the RequestQueue. | ||
34 | + private RequestQueue queue; | ||
35 | + | ||
36 | + private String url = "http://www.google.com"; | ||
37 | + | ||
38 | + // Request a string response from the provided URL. | ||
39 | + private StringRequest stringRequest = new StringRequest(Request.Method.GET, url, | ||
40 | + new Response.Listener<String>() { | ||
41 | + @Override | ||
42 | + public void onResponse(String response) { | ||
43 | + // Display the first 500 characters of the response string. | ||
44 | + textView.setText("Response is: "+ response.substring(0,500)); | ||
45 | + } | ||
46 | + }, new Response.ErrorListener() { | ||
47 | + @Override | ||
48 | + public void onErrorResponse(VolleyError error) { | ||
49 | + Log.d(TAG, error.getCause().toString()); | ||
50 | + textView.clearComposingText(); | ||
51 | + } | ||
52 | + }); | ||
53 | + | ||
54 | + private final Handler handler = new Handler(); | ||
55 | + private final Runnable runnable = new Runnable() { | ||
56 | + @Override | ||
57 | + public void run() { | ||
58 | + // Add the request to the RequestQueue. | ||
59 | + queue.add(stringRequest); | ||
60 | + handler.postDelayed(this, 1000); | ||
61 | + } | ||
62 | + }; | ||
63 | + | ||
21 | @Override | 64 | @Override |
22 | protected void onCreate(Bundle savedInstanceState) { | 65 | protected void onCreate(Bundle savedInstanceState) { |
23 | super.onCreate(savedInstanceState); | 66 | super.onCreate(savedInstanceState); |
@@ -27,6 +70,16 @@ public class PositioningActivity extends BaseActivity { | @@ -27,6 +70,16 @@ public class PositioningActivity extends BaseActivity { | ||
27 | 70 | ||
28 | start_layout = (ConstraintLayout) findViewById(R.id.start_layout); | 71 | start_layout = (ConstraintLayout) findViewById(R.id.start_layout); |
29 | hideAnimation = createFadeOutAndHideAnimation(); | 72 | hideAnimation = createFadeOutAndHideAnimation(); |
73 | + | ||
74 | + textView = (TextView) findViewById(R.id.ID_yolo); | ||
75 | + | ||
76 | + queue = Volley.newRequestQueue(this); | ||
77 | + } | ||
78 | + | ||
79 | + @Override | ||
80 | + protected void onDestroy() { | ||
81 | + handler.removeCallbacks(runnable); | ||
82 | + super.onDestroy(); | ||
30 | } | 83 | } |
31 | 84 | ||
32 | @Override | 85 | @Override |
@@ -39,6 +92,7 @@ public class PositioningActivity extends BaseActivity { | @@ -39,6 +92,7 @@ public class PositioningActivity extends BaseActivity { | ||
39 | 92 | ||
40 | public void startLocating(View v) { | 93 | public void startLocating(View v) { |
41 | start_layout.startAnimation(hideAnimation); | 94 | start_layout.startAnimation(hideAnimation); |
95 | + handler.post(runnable); | ||
42 | } | 96 | } |
43 | 97 | ||
44 | private Animation createFadeOutAndHideAnimation() { | 98 | private Animation createFadeOutAndHideAnimation() { |
app/src/main/res/layout/content_positioning.xml
@@ -23,6 +23,13 @@ | @@ -23,6 +23,13 @@ | ||
23 | android:layout_marginStart="8dp" | 23 | android:layout_marginStart="8dp" |
24 | android:layout_marginEnd="8dp" /> | 24 | android:layout_marginEnd="8dp" /> |
25 | 25 | ||
26 | + <TextView | ||
27 | + android:id="@+id/ID_yolo" | ||
28 | + android:layout_width="wrap_content" | ||
29 | + android:layout_height="wrap_content" | ||
30 | + android:text="TextView" | ||
31 | + android:textAppearance="@style/TextAppearance.AppCompat.Medium" /> | ||
32 | + | ||
26 | <android.support.constraint.ConstraintLayout | 33 | <android.support.constraint.ConstraintLayout |
27 | android:id="@+id/start_layout" | 34 | android:id="@+id/start_layout" |
28 | android:layout_width="0dp" | 35 | android:layout_width="0dp" |