Contournement du bug mystère au niveau du réseau (à la fac).

This commit is contained in:
Georges Dupéron 2011-03-09 11:51:29 +01:00
parent 2b20c3bc51
commit badb971fb4
2 changed files with 147 additions and 66 deletions

View File

@ -0,0 +1,99 @@
package org.pticlic.model;
/***
Copyright (c) 2009
Author: Stefan Klumpp <stefan.klumpp@gmail.com>
Web: http://stefanklumpp.com
Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.util.Log;
public class HttpClient {
private static final String TAG = "HttpClient";
public static String SendHttpPost(String URL) {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpPostRequest = new HttpGet(URL);
//StringEntity se;
//se = new StringEntity(str);
// Set HTTP parameters
//httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");
httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression
long t = System.currentTimeMillis();
HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");
// Get hold of the response entity (-> the data):
HttpEntity entity = response.getEntity();
if (entity != null) {
// Read the content stream
InputStream instream = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
// convert content stream to a String
BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
instream.close();
instream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
} else {
return null;
}
}
catch (Exception e)
{
// More about HTTP exception handling in another tutorial.
// For now we just print the stack trace.
e.printStackTrace();
return null;
}
}
}

View File

@ -7,7 +7,6 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import org.pticlic.exception.PtiClicException;
@ -123,29 +122,20 @@ public class Network {
String id = sp.getString(Constant.USER_ID, "joueur");
String passwd = sp.getString(Constant.USER_PASSWD, "");
URL url = null;
Gson gson = null;
BufferedReader reader = null;
String json = null;
boolean res = false;
try {
String urlS = serverURL
+ "action=" + Action.CHECK_LOGIN.value()
+ "?action=" + Action.CHECK_LOGIN.value()
+ "&user=" + id
+ "&passwd=" + passwd;
url = new URL(urlS);
gson = new Gson();
reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
json = reader.readLine();
json = HttpClient.SendHttpPost(urlS);
Check check = gson.fromJson(json, Check.class);
res = check.isLogin_ok();
} catch (MalformedURLException e) {
return false;
} catch (IOException e) {
return false;
}
return res;
}
@ -165,21 +155,9 @@ public class Network {
}
private DownloadedBaseGame DownloadBaseGame(int nbGames) throws PtiClicException, Exception {
DownloadedBaseGame game = null;
URL url = null;
Gson gson = null;
BufferedReader reader = null;
String json = null;
try {
// TODO : ne restera le temps que les requete du serveur passe du GET au POST
String urlS = this.serverURL
+ "action=" + Action.GET_GAMES.value()
+ "&user=" + this.id
+ "&passwd=" + this.passwd
+ "&nb=" + String.valueOf(nbGames)
+ "&mode="+mode.value();
url = new URL(urlS);
DownloadedBaseGame game = null;
// URLConnection connection = url.openConnection();
// connection.addRequestProperty("action", Action.GET_GAMES.value());
@ -187,10 +165,19 @@ public class Network {
// connection.addRequestProperty("passwd", this.passwd);
// connection.addRequestProperty("nb", String.valueOf(nbGames));
// connection.addRequestProperty("mode", mode.value());
reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
json = reader.readLine();
String urlS = this.serverURL
+ "?action=" + Action.GET_GAMES.value()
+ "&user=" + this.id
+ "&passwd=" + this.passwd
+ "&nb=" + String.valueOf(nbGames)
+ "&mode="+mode.value();
gson = new Gson();
json = HttpClient.SendHttpPost(urlS);
try {
//JsonReader reader = new JsonReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
InputStream in = new ByteArrayInputStream(json.getBytes("UTF-8"));
JsonReader jsonReader = new JsonReader(new InputStreamReader(in));
@ -279,15 +266,13 @@ public class Network {
public double sendBaseGame(Match game) throws PtiClicException, Exception {
double score = -1;
URL url = null;
Gson gson = null;
BufferedReader reader = null;
String json = null;
try {
// TODO : ne restera le temps que les requete du serveur passe du GET au POST
String urlS = this.serverURL
+ "action=" + Action.SEND_GAME.value()
+ "?action=" + Action.SEND_GAME.value()
+ "&user=" + this.id
+ "&passwd=" + this.passwd
+ "&pgid=" + game.getGame().getPgid()
@ -308,8 +293,6 @@ public class Network {
urlS += "&" + i + "=" + ((DownloadedBaseGame)game.getGame()).getCat4();
}
url = new URL(urlS);
// URL url = new URL(this.serverURL); // Attention ! this.serverURL contient "/server.php"
// URLConnection connection = url.openConnection();
// connection.addRequestProperty("action", Action.SEND_GAME.value());
@ -318,10 +301,9 @@ public class Network {
// connection.addRequestProperty("mode", mode.value());
// connection.addRequestProperty("pgid", String.valueOf(game.getGame().getId()));
reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
json = reader.readLine();
gson = new Gson();
json = HttpClient.SendHttpPost(urlS);
//JsonReader reader = new JsonReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
InputStream in = new ByteArrayInputStream(json.getBytes("UTF-8"));
JsonReader jsonReader = new JsonReader(new InputStreamReader(in));