• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 package com.badlogic.gdx.tests.net;
18 
19 import com.badlogic.gdx.Gdx;
20 import com.badlogic.gdx.Net;
21 import com.badlogic.gdx.Net.HttpRequest;
22 import com.badlogic.gdx.Net.HttpResponse;
23 import com.badlogic.gdx.Net.HttpResponseListener;
24 import com.badlogic.gdx.graphics.Color;
25 import com.badlogic.gdx.graphics.GL20;
26 import com.badlogic.gdx.graphics.Pixmap;
27 import com.badlogic.gdx.graphics.Texture;
28 import com.badlogic.gdx.graphics.g2d.BitmapFont;
29 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
30 import com.badlogic.gdx.scenes.scene2d.InputEvent;
31 import com.badlogic.gdx.scenes.scene2d.Stage;
32 import com.badlogic.gdx.scenes.scene2d.Touchable;
33 import com.badlogic.gdx.scenes.scene2d.ui.Label;
34 import com.badlogic.gdx.scenes.scene2d.ui.Skin;
35 import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
36 import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
37 import com.badlogic.gdx.tests.utils.GdxTest;
38 import com.badlogic.gdx.utils.Align;
39 
40 public class NetAPITest extends GdxTest implements HttpResponseListener {
41 
42 	SpriteBatch batch;
43 	Skin skin;
44 	Stage stage;
45 	TextButton btnDownloadImage;
46 	TextButton btnDownloadText;
47 	TextButton btnDownloadLarge;
48 	TextButton btnDownloadError;
49 	TextButton btnPost;
50 	TextButton btnCancel;
51 	TextButton btnOpenUri;
52 	Label statusLabel;
53 	Texture texture;
54 	String text;
55 	BitmapFont font;
56 	HttpRequest httpRequest;
57 
58 	Object clickedButton;
59 
needsGL20()60 	public boolean needsGL20 () {
61 		// just because the non pot, we could change the image instead...
62 		return true;
63 	}
64 
65 	@Override
dispose()66 	public void dispose () {
67 		batch.dispose();
68 		stage.dispose();
69 		skin.dispose();
70 		font.dispose();
71 		if (texture != null) texture.dispose();
72 	}
73 
74 	@Override
create()75 	public void create () {
76 		batch = new SpriteBatch();
77 		skin = new Skin(Gdx.files.internal("data/uiskin.json"));
78 		font = new BitmapFont();
79 		stage = new Stage();
80 		Gdx.input.setInputProcessor(stage);
81 
82 		{
83 			statusLabel = new Label("", skin);
84 			statusLabel.setWrap(true);
85 			statusLabel.setWidth(Gdx.graphics.getWidth() * 0.96f);
86 			statusLabel.setAlignment(Align.center);
87 			statusLabel.setPosition(Gdx.graphics.getWidth() * 0.5f - statusLabel.getWidth() * 0.5f, 30f);
88 			statusLabel.setColor(Color.CYAN);
89 			stage.addActor(statusLabel);
90 		}
91 
92 		{
93 			ClickListener clickListener = new ClickListener() {
94 				@Override
95 				public void clicked (InputEvent event, float x, float y) {
96 					super.clicked(event, x, y);
97 
98 					clickedButton = event.getListenerActor();
99 					setButtonDisabled(true);
100 					if (texture != null) texture.dispose();
101 					texture = null;
102 					text = null;
103 
104 					String url;
105 					String httpMethod = Net.HttpMethods.GET;
106 					String requestContent = null;
107 					if (clickedButton == btnDownloadImage)
108 						url = "http://i.imgur.com/vxomF.jpg";
109 					else if (clickedButton == btnDownloadText)
110 						url = "http://www.apache.org/licenses/LICENSE-2.0.txt";
111 					else if (clickedButton == btnDownloadLarge)
112 						url = "http://libgdx.badlogicgames.com/releases/libgdx-1.2.0.zip";
113 					else if (clickedButton == btnDownloadError)
114 						url = "http://www.badlogicgames.com/doesnotexist";
115 					else if (clickedButton == btnOpenUri) {
116 						Gdx.net.openURI("http://libgdx.badlogicgames.com/");
117 						return;
118 					}
119 					else {
120 						url = "http://posttestserver.com/post.php?dump";
121 						httpMethod = Net.HttpMethods.POST;
122 						requestContent = "name1=value1&name2=value2";
123 					}
124 
125 					httpRequest = new HttpRequest(httpMethod);
126 					httpRequest.setUrl(url);
127 					httpRequest.setContent(requestContent);
128 					Gdx.net.sendHttpRequest(httpRequest, NetAPITest.this);
129 
130 					statusLabel.setText("Downloading data from " + httpRequest.getUrl());
131 				}
132 			};
133 
134 			ClickListener cancelListener = new ClickListener() {
135 				@Override
136 				public void clicked (InputEvent event, float x, float y) {
137 					super.clicked(event, x, y);
138 					if (httpRequest != null) {
139 						Gdx.net.cancelHttpRequest(httpRequest);
140 						Gdx.app.log("NetAPITest", "Cancelling request " + httpRequest.getUrl());
141 						statusLabel.setText("Cancelling request " + httpRequest.getUrl());
142 					}
143 				}
144 			};
145 
146 			btnCancel = new TextButton("Cancel", skin);
147 			btnCancel.setPosition(Gdx.graphics.getWidth() * 0.10f, 60f);
148 			btnCancel.addListener(cancelListener);
149 			stage.addActor(btnCancel);
150 
151 			btnDownloadImage = new TextButton("GET Image", skin);
152 			btnDownloadImage.setPosition(btnCancel.getX() + btnCancel.getWidth() + 10, 60f);
153 			btnDownloadImage.addListener(clickListener);
154 			stage.addActor(btnDownloadImage);
155 
156 			btnDownloadText = new TextButton("GET Text", skin);
157 			btnDownloadText.setPosition(btnDownloadImage.getX() + btnDownloadImage.getWidth() + 10, 60f);
158 			btnDownloadText.addListener(clickListener);
159 			stage.addActor(btnDownloadText);
160 
161 			btnDownloadLarge = new TextButton("GET Large", skin);
162 			btnDownloadLarge.setPosition(btnDownloadText.getX() + btnDownloadText.getWidth() + 10, 60f);
163 			btnDownloadLarge.addListener(clickListener);
164 			stage.addActor(btnDownloadLarge);
165 
166 			btnDownloadError = new TextButton("GET Error", skin);
167 			btnDownloadError.setPosition(btnDownloadLarge.getX() + btnDownloadLarge.getWidth() + 10, 60f);
168 			btnDownloadError.addListener(clickListener);
169 			stage.addActor(btnDownloadError);
170 
171 			btnPost = new TextButton("POST", skin);
172 			btnPost.setPosition(btnDownloadError.getX() + btnDownloadError.getWidth() + 10, 60f);
173 			btnPost.addListener(clickListener);
174 			stage.addActor(btnPost);
175 
176 			btnOpenUri = new TextButton("Open URI", skin);
177 			btnOpenUri.setPosition(btnPost.getX() + btnPost.getWidth() + 10, 60f);
178 			btnOpenUri.addListener(clickListener);
179 
180 			stage.addActor(btnOpenUri);
181 		}
182 
183 	}
184 
185 	@Override
handleHttpResponse(HttpResponse httpResponse)186 	public void handleHttpResponse (HttpResponse httpResponse) {
187 
188 		final int statusCode = httpResponse.getStatus().getStatusCode();
189 		// We are not in main thread right now so we need to post to main thread for ui updates
190 		Gdx.app.postRunnable(new Runnable() {
191 			@Override
192 			public void run () {
193 				statusLabel.setText("HTTP Request status: " + statusCode);
194 				setButtonDisabled(false);
195 			}
196 		});
197 
198 		if (statusCode != 200) {
199 			Gdx.app.log("NetAPITest", "An error ocurred since statusCode is not OK");
200 			setText(httpResponse);
201 			return;
202 		}
203 
204 		if (clickedButton == btnDownloadImage) {
205 			final byte[] rawImageBytes = httpResponse.getResult();
206 			Gdx.app.postRunnable(new Runnable() {
207 				public void run () {
208 					Pixmap pixmap = new Pixmap(rawImageBytes, 0, rawImageBytes.length);
209 					texture = new Texture(pixmap);
210 				}
211 			});
212 
213 		} else if (clickedButton == btnDownloadLarge) {
214 			Gdx.app.postRunnable(new Runnable() {
215 				public void run () {
216 					text = "Retrieving large file...";
217 				}
218 			});
219 			final byte[] rawFileBytes = httpResponse.getResult();
220 			Gdx.app.postRunnable(new Runnable() {
221 				public void run () {
222 					text = "Retrieved large file: " + rawFileBytes.length;
223 				}
224 			});
225 
226 		} else {
227 			setText(httpResponse);
228 		}
229 	}
230 
setText(HttpResponse httpResponse)231 	void setText (HttpResponse httpResponse) {
232 		final String newText = httpResponse.getResultAsString();
233 		Gdx.app.postRunnable(new Runnable() {
234 			public void run () {
235 				text = newText;
236 			}
237 		});
238 	}
239 
setButtonDisabled(boolean disabled)240 	void setButtonDisabled (boolean disabled) {
241 		Touchable t = disabled ? Touchable.disabled : Touchable.enabled;
242 
243 		btnDownloadImage.setDisabled(disabled);
244 		btnDownloadImage.setTouchable(t);
245 
246 		btnDownloadText.setDisabled(disabled);
247 		btnDownloadText.setTouchable(t);
248 
249 		btnDownloadError.setDisabled(disabled);
250 		btnDownloadError.setTouchable(t);
251 
252 		btnPost.setDisabled(disabled);
253 		btnPost.setTouchable(t);
254 	}
255 
256 	@Override
failed(Throwable t)257 	public void failed (Throwable t) {
258 		setButtonDisabled(false);
259 		statusLabel.setText("Failed to perform the HTTP Request: " + t.getMessage());
260 		t.printStackTrace();
261 	}
262 
263 	@Override
render()264 	public void render () {
265 		Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
266 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
267 
268 		if (texture != null) {
269 			batch.begin();
270 			batch.draw(texture, Gdx.graphics.getWidth() * 0.5f - texture.getWidth() * 0.5f, 100f);
271 			batch.end();
272 		} else if (text != null) {
273 			batch.begin();
274 			font.draw(batch, text, 10, Gdx.graphics.getHeight() - 10);
275 			batch.end();
276 		}
277 
278 		stage.act(Gdx.graphics.getDeltaTime());
279 		stage.draw();
280 	}
281 
282 	@Override
resize(int width, int height)283 	public void resize (int width, int height) {
284 		stage.getViewport().update(width, height, true);
285 	}
286 
287 	@Override
cancelled()288 	public void cancelled () {
289 		Gdx.app.postRunnable(new Runnable() {
290 			public void run () {
291 				setButtonDisabled(false);
292 				Gdx.app.log("NetAPITest", "HTTP request cancelled");
293 				statusLabel.setText("HTTP request cancelled");
294 			}
295 		});
296 	}
297 
298 }
299