0

I have problem with my apk. It was everything done already but last testing makes my heade ache.

Here we go. App insert some data into mysql database with PHP handler below:

<?php

$dbh=mysql_connect("www.someDomain.com",$_REQUEST['uiBaze'],$_REQUEST['gBaze']);
mysql_set_charset("utf8", $dbh);

mysql_select_db($_REQUEST['dbName']);

$q=mysql_query($_REQUEST['sql']);
while($e=mysql_fetch_assoc($q)){
$output[]=$e;
}

$encoded = json_encode($output);

print($encoded);

mysql_close();
?>

With this simple php I can use insert, update and select, enough what I need for app.

Then I use AsyncTask class below for connecting :

protected JSONArray doInBackground(String... params) {
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(linkPhp);
        UrlEncodedFormEntity uefe=new UrlEncodedFormEntity(postparams, HTTP.UTF_8);
        uefe.setContentEncoding(HTTP.UTF_8);
        httpPost.setEntity(uefe);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();

        is = httpEntity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }
    try {

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "UTF-8"));

        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    try {
        if (json!=null) {
            jAry = new JSONArray(json);
        } else {
            jAry=new JSONArray();
        }
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
    return jAry;
}

It worked OK when I was on wifi but then I tried over 3g (HSDPA) and 2g(EDGE) and it didn't work. Well, not totally because I could connect to server to check for username and password and retrieve some more info on successful input (about 300 characters in json array) but when I tried to retrieve a bit longer String (about 8k characters) it gave me this string: "httprequest was interrupted by host" instead of String for JSONArray. I also made a loop to try more times if signal was not good enough maybe but it didn't help as well.

Does my code missing some parts? I'm testing on samsung galaxy tab 2. Oh and when I replaced sim card with the one from my phone (different provider) it worked. Sometimes I had to press refresh button in my app but it worked. I understand that it has to do with this provider, but other apps works with it and doesn't throw that kind of error so i need to go arround this problem. Help? :)

I searched for that problem a lot but no proper answer was given yet for developers.

If you need any more info then please nudge me.

Thank you!

PROGRESS EDIT:

Sending to PHP == OK
Executing PHP script == OK
Retrieving this output == NOT OK!

New findings. When I call method to retrieve data from server and this transfer is successful, I get Toasts in that order:
1. Create JSONArray OK
2. Inside JSONArray doInBackground(String... params)
3. HTTPPost OK
4. InputStream created
5. StringBuffer filled
6. Save to json String OK
7. (JSONArray) jAry created

When it is not successful then it is in this order:
1. Create JSONArray OK
2. Thrown error, JSONArray=null
3. Inside JSONArray doInBackground(String... params)
4. HTTPPost OK
5. InputStream created
6. StringBuffer filled
7. Save to json String OK
8. (JSONArray) jAry error: HTTPRequest was interrupted by the host

I'm thinking... I'm calling methods in this order:

JSONArray jAryS = getJsonArray(nameValuePair);

This line execute task:

public JSONArray getJsonArray(ArrayList<NameValuePair> params) {
    VrniJSONArray task = new VrniJSONArray(params, PodatkiPrograma.LOKACIJA_PHP);
    try {
        return task.execute().get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}

and above code from doInBackground() is executed.

Would it help, if I move code into getJsonArray(); method? I suspect that error is thrown before mobile network have time to react because main thread is not "waiting" for this task to finish.

Matjaz
  • 468
  • 5
  • 21

2 Answers2

1

OK problem solved after three days! :)

Source was OK, PHP was OK, server settings was OK, all was good except default setting on Galaxy tab for connecting on internet. ISP set proxy (for sniffing what we, ordinary people are doing of course) that blocked response from PHP so no answer was recieved. After removing proxy everything worked like it should.

Thanks to @forgivegod and @chrkad that helped me and learned me some more about numerous java classes and settings. :)

Cheers :)

Matjaz
  • 468
  • 5
  • 21
0

Your httpclient is timing out. Instead create it like this.

    int timeout = 20000;  // 20 seconds

    // Configure the params and ssl 
    HttpParams httpParams = new BasicHttpParams();
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

    ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(httpParams, schemeRegistry);

    // HTC Thunderbolt and certain LG Devices  has an issue where the socket buffer size was set to 2 MB (!), which
    // would cause OutOfMemoryErrors.  As a workaround, manually set the buffer size back to the
    // usual default of 8 KB.  See:
    //   http://stackoverflow.com/questions/5358014/android-httpclient-oom-on-4g-lte-htc-thunderbolt
    HttpConnectionParams.setSocketBufferSize(httpParams, 8192);

    HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
    HttpConnectionParams.setSoTimeout(httpParams, timeout);

    // Create the HttpClient instance
    DefaultHttpClient httpClient = new DefaultHttpClient(connManager, httpParams);
petey
  • 16,914
  • 6
  • 65
  • 97
  • I still get the same result: "HttpRequest was interrupted by the host!" – Matjaz Mar 28 '13 at 19:03
  • @Matjaz Then it's your server that is doing something it shouldn't. – kaderud Mar 28 '13 at 19:15
  • I don't think it is server problem. As I said, I tested with another sim card that have different provider and it worked ok. – Matjaz Mar 28 '13 at 20:13
  • increase the timeout (first line) – petey Mar 28 '13 at 20:16
  • Also did it but it's not problem in that as I get response back from PHP very quick (couple of ms after I click on refresh button). Can I make any test to find out what is wrong? I'm out of ideas atm... – Matjaz Mar 28 '13 at 20:23
  • I know I probably imported right libraries but just in case, can you edit answer with imports or just write it down somewhere to check them? Thanks – Matjaz Mar 28 '13 at 20:32
  • Please check update to my question. I added couple of things. – Matjaz Mar 29 '13 at 07:54
  • Just to inform you, problem solved. Check my answer to question. Thanks :) – Matjaz Mar 29 '13 at 11:56