1

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.

arash moeen
  • 4,533
  • 9
  • 40
  • 85

1 Answers1

0

if you suspect the server-side , you can just remove the android stack from the equation and test directly in curl, using switches -V and 'time' to measure as much stuff as you want.

when you have investigated server directly and are no longer suspicious of server-side, you return to your android stack.

example POST on REST endpoint:

curl -X POST  -H "X-Parse-Application-Id: ovv"  -H "X-Parse-REST-API-Key: heoj"  -H "X-Parse-Session-Token: i9ds" -H "Content-Type: application/json" --data @media1.json https://api.parse.com/1/classes/MediaItem
Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43
  • Thanks for your reply, I already tested with curl and it was no problem that way, so I server-side was off the table, but another thing is that my workplace got a messed up wifi network, laptop is connected via cable so I assume that's the problem. I'll look into it tomorrow. but thanks anyway – arash moeen Aug 05 '14 at 14:00