I'm using the below code to fetch some data from server: (it happens on button onclick)
@Override
protected ArrayList<Category> doInBackground(String... arg0) {
ArrayList<Category> result = new ArrayList<Category>();
JSONArray array = new JSONArray();
BufferedReader in = null;
try
{
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpclient = new DefaultHttpClient(params);
HttpGet request = new HttpGet();
URI website = new URI(_URL);
request.setURI(website);
HttpResponse response = httpclient.execute(request);
in = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String data = in.readLine();
array = new JSONArray(data);
for(int i = 0;i < array.length();i++)
{
JSONObject object = array.getJSONObject(i);
Category category = new Category(object.getInt("Id"), object.getString("Name"));
result.add(category);
}
}
catch(Throwable t)
{
Log.e("Error getting categories", t.getMessage());
}
return result;
}
the problem is that 8 out of 10 tries,it throws the following exception:
org.apache.http.conn.HttpHostConnectException: Connection to http://kiagallery.ir refused
and most of the times it takes a long time to fetch the data, but sometimes it's fast as a lightning bolt, the data is not big, here's the data that's supposed to be fetched at the moment:
[{"Id":44,"Name":"Collection 101"},{"Id":45,"Name":"local 01"}]
so my question is, how come sometimes it can fetch the data at reasonable speed and sometimes it throws an exception, could be the network speed? because my workplace got a terrible network, I also tried at home and the result was better but the exception occurred still once in a while
Additional Info: I used curl to fetch it and it was fast, paste the url in my browser and it was fast.