美好的一天, 对于我现在遇到的问题,我的asynctask适用于SDK 24或更高版本,但当我把它放在SDK 21,22,23模拟器和真实设备上时,它不起作用。
奇怪的是: 1.)我的第一个活动和片段上的Asynctasks正在工作,但第二个活动的asysntask不起作用,错误代码:400,异常java.lang.IllegalStateException:myURL。
我使用doInBackGround代码进行异步操作:
@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;
}
}
}
对于此代码,适用于SDK 24,25,26。它会在con.getInputStream()中抛出异常。我不确定是什么问题。是线程还是其他什么问题?感谢。
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)
答案 0 :(得分:0)
是否aynctask无法正常工作或HttpURLConnection无法按预期工作?我可以理解,根据代码,您使用get url来检索数据。
我可以建议使用getResponseCode方法等待响应,然后尝试使用getInputStream方法。
@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;
}