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
|
// pourquoi la méthode n'est pas statique ?
public static JSONObject getJSONFromUrl(String url, List<NameValuePair> params) throws IOException
{
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
Log.d("JSONParser","Sending request to "+url);
HttpResponse httpResponse = httpClient.execute(httpPost);
Log.d("JSONParser","Response received");
HttpEntity httpEntity = httpResponse.getEntity();
String resultStr = EntityUtils.toString(httpEntity,"iso-8859-1");
Log.d("JSONParser",resultStr);
return new JSONObject(resultStr);
} catch (UnsupportedEncodingException e) {
Log.e("JSONParser","Unexpected encoding exception",e);
throw new IllegalStateException("Unexpected encoding exception",e);
} catch (ClientProtocolException e) {
Log.e("JSONParser","URL Protocol exception",e);
throw new IllegalArguement("Invalid URL: '"+url+"'",e);
} catch (JSONException e) {
Log.e("JSONParser","Invalid JSON Response",e);
throw new IOException("Invalid JSON Response",e);
}
} |
Partager