1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
public class AsyncTaskRunner extends AsyncTask<String, Void, JSONArray > {
String ville ="";
final static HttpClient client = new DefaultHttpClient();
final HttpPost post = new HttpPost();
static HttpResponse response;
static JSONArray jObj = null;
static String json = "";
MarkerOptions markerr;
JSONObject JO = null;
String latt;
String longg;
Double l1 = null;
Double l2= null;
protected JSONArray doInBackground(String... arg0) {
// TODO Auto-generated method stub
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
final HttpPost post = new HttpPost(arg0[0]);
try {
ville = MainActivity.getVille();
final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("ville", " "+ville));
System.out.println(ville);
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = client.execute(post);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
/*Checking response */
if(response!=null){
try {
InputStream in = response.getEntity().getContent(); //Get the data in the entity
BufferedReader reader = new BufferedReader(new InputStreamReader(
in, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
json = sb.toString();
jObj = new JSONArray(json);
}
in.close();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
}
return jObj;
}
protected void onPostExecute(JSONArray jObj)
{
if(jObj != null)
{
// ...
}
return jObj;
} |
Partager