Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid apk fail to retrieve data over 3g using PHP, on wifi works ok
    text
    copied!<p>I have problem with my apk. It was everything done already but last testing makes my heade ache.</p> <p>Here we go. App insert some data into mysql database with PHP handler below:</p> <pre><code>&lt;?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(); ?&gt; </code></pre> <p>With this simple php I can use insert, update and select, enough what I need for app.</p> <p>Then I use AsyncTask class below for connecting :</p> <pre><code>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; } </code></pre> <p>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. </p> <p>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? :)</p> <p>I searched for that problem a lot but no proper answer was given yet for developers.</p> <p>If you need any more info then please nudge me.</p> <p>Thank you!</p> <p>PROGRESS EDIT:</p> <p>Sending to PHP == OK<br> Executing PHP script == OK<br> <b>Retrieving this output == NOT OK!</b></p> <p>New findings. When I call method to retrieve data from server and this transfer is successful, I get Toasts in that order:<br> 1. Create JSONArray OK<br> 2. Inside JSONArray doInBackground(String... params)<br> 3. HTTPPost OK<br> 4. InputStream created<br> 5. StringBuffer filled<br> 6. Save to json String OK<br> 7. (JSONArray) jAry created<br></p> <p>When it is not successful then it is in this order:<br> 1. Create JSONArray OK<br> 2. Thrown error, JSONArray=null<br> 3. Inside JSONArray doInBackground(String... params)<br> 4. HTTPPost OK<br> 5. InputStream created<br> 6. StringBuffer filled<br> 7. Save to json String OK<br> 8. (JSONArray) jAry error: HTTPRequest was interrupted by the host<br></p> <p>I'm thinking... I'm calling methods in this order:</p> <pre><code>JSONArray jAryS = getJsonArray(nameValuePair); </code></pre> <p>This line execute task:</p> <pre><code>public JSONArray getJsonArray(ArrayList&lt;NameValuePair&gt; 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; } } </code></pre> <p>and above code from doInBackground() is executed.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload