Good day, for what the problem I met now, my asynctask is working for SDK 24 or above,but when I put it on SDK 21,22,23 emulator and real devices, it doen't work.
The weird thing is: 1.) Asynctasks on my first activity and fragment are working, but the 2nd activity's asysntask is not working,error code: 400, exception java.lang.IllegalStateException : myURL.
My asynctask with doInBackGround code:
@Override
protected String doInBackground(Void... voids) {
try {
//creating a URL
URL url = new URL(urlWebService);
//Opening the URL using HttpURLConnection
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//StringBuilder object to read the string from the service
StringBuilder sb = new StringBuilder();
//We will use a buffered reader to read the string from service
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
//A simple string to read values from each line
String json;
while ((json = bufferedReader.readLine()) != null) {
if (!json.contains("<br")) {
sb.append(json + "\n");
}
}
//finally returning the read string
return sb.toString().trim();
} catch (Exception e) {
System.out.println("Error in http connection " + e.toString());
return null;
}
}
}
For this code, works on SDK 24,25,26. and it will throw an exception at con.getInputStream(). I am not sure what is the problem. Is it thread or what other problems? Thanks.
IOExeption
10-27 02:28:29.453 16954-17698/com.example.reonyx.tracking5 W/System.err: java.io.FileNotFoundException:mrURL
10-27 02:28:29.453 16954-17698/com.example.reonyx.tracking5 W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197)
10-27 02:28:29.453 16954-17698/com.example.reonyx.tracking5 W/System.err: at com.example.reonyx.tracking5.PlayBackActivity$1GetJSON.doInBackground(PlayBackActivity.java:1206)
10-27 02:28:29.453 16954-17698/com.example.reonyx.tracking5 W/System.err: at com.example.reonyx.tracking5.PlayBackActivity$1GetJSON.doInBackground(PlayBackActivity.java:964)
10-27 02:28:29.453 16954-17698/com.example.reonyx.tracking5 W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
10-27 02:28:29.453 16954-17698/com.example.reonyx.tracking5 W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-27 02:28:29.454 16954-17698/com.example.reonyx.tracking5 W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
10-27 02:28:29.454 16954-17698/com.example.reonyx.tracking5 W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-27 02:28:29.454 16954-17698/com.example.reonyx.tracking5 W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-27 02:28:29.454 16954-17698/com.example.reonyx.tracking5 W/System.err: at java.lang.Thread.run(Thread.java:818)
Is it aynctask not working or HttpURLConnection is not working as expected ? I can understand as per code you are using the get url to retrive the data.
I can suggest to use getResponseCode method to wait for the response and then try to use getInputStream method.
@Override
protected String doInBackground(String... strings) {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(strings[0]);
urlConnection = (HttpURLConnection) url.openConnection();
int responseCode = urlConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK) {
// use bufferreder to get the data from input stream.
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(urlConnection!=null)
urlConnection.disconnect();
}
return null;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.