Issue
I have two AsyncTasks in my fragment. First to load (AsyncTask1) the initial set of data and then another (AsyncTask2) to load subsequent data as the app is scrolled down.
Both the tasks work perfectly in the emulator (Pixel, Nexus devices). As I scroll the second task loads new data seamlessly.
But when I run the app on Samsung Note 4, Samsung A5 and one other device then AsyncTask1 works correctly but the AsyncTask2 stops executing after the below line. I tried it on a Moto phone and it worked properly.
OutputStream outputStream1 = httpURLConnection.getOutputStream();
Below is my doInBackground method of AsyncTask2
@Override
protected String doInBackground(Integer... args) {
String arg_item_number = String.valueOf((args[0]*5));
np_page = args[0];
String arg_language;
if (args[1]==1){
arg_language = "en";
}else{
arg_language = "hi";
}
try {
URL url1 = new URL(url_get_next_posts);
HttpURLConnection httpURLConnection = (HttpURLConnection) url1.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream outputStream1 = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter1 = new BufferedWriter(new OutputStreamWriter(outputStream1,"UTF-8"));
String data_string_language1 = URLEncoder.encode("item_number","UTF-8")+"="+URLEncoder.encode(arg_item_number,"UTF-8")
+"&"+URLEncoder.encode("selected_language","UTF-8")+"="+URLEncoder.encode(arg_language,"UTF-8");
bufferedWriter1.write(data_string_language1);
bufferedWriter1.flush();
bufferedWriter1.close();
outputStream1.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
while ((JSON_NEXT_POST = bufferedReader.readLine()) != null) {
stringBuilder.append(JSON_NEXT_POST + "\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Error log
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
D/tag_item_count: 1
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@8bda7dd time:13839484
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
W/System.err: java.net.UnknownHostException: http:websitenname.com/script_url.php
W/System.err: at com.android.okhttp.internal.http.HttpEngine.createAddress(HttpEngine.java:1347)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:439)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:353)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:476)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:249)
W/System.err: at com.app_name.android.app_name.PostTab$BackgroundNextPostsJSON.doInBackground(PostTab.java:374)
W/System.err: at com.app_name.android.app_name.PostTab$BackgroundNextPostsJSON.doInBackground(PostTab.java:347)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err: at java.lang.Thread.run(Thread.java:818)
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
Can it be something related to the Android version running on the phone (Note 4 is running 6.0.1)? I have no clue what is wrong with the code as it actually works on emulator & Moto phone. Can the problem occur on other non-Samsung devices too?
Solution
In your stacktrace
http:websitenname.com/
is invalid URL due to it is missing double slash in the url scheme. Should be http://websitenname.com/
probably.
It worked on emulator and some devices probably because they have some undocumented improvements, and try to open even invalid URLs.
Answered By - Vladyslav Matviienko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.