我需要从网址获取不同的数据。我的代码用于获取JSON数据。但是,我需要为galleryActivity下载图像。我尝试使用另一个asynctask,但我的应用程序崩溃......
网址:http://url:8080/getAllPictures提供JSON数据:
[{"id":"590c7612c1e2a8289404c991","droneId":1212,"gps":"41,-8","url":"1493988882.jpg","googleData":null},{"id":"5919f9be32785a2ebc90a6a1","droneId":2,"gps":"41.17784570000001,-8.608100599999943","url":"1494874557.jpg","googleData":null},{"id":"5919fa0d32785a2ebc90a6a4","droneId":2,"gps":"41.174247,-8.60360700000001","url":"1494874637.jpg","googleData":null},{"id":"5919fa3332785a2ebc90a6a7","droneId":2,"gps":"41.1456753,-8.614598500000056","url":"1494874675.jpg","googleData":null},{"id":"591c2bf632785a2dc83319fc","droneId":1,"gps":"41.1530568,-8.610164599999962","url":"1495018486.jpg","googleData":null},{"id":"591c2c6f32785a2dc83319ff","droneId":1,"gps":"41.1588059,-8.621924799999988","url":"1495018607.jpg","googleData":null},{"id":"591ec3dd90f65236507f3204","droneId":1,"gps":"41.1588059,-8.621924799999988","url":"1495188443.jpg","googleData":null}]
url:http://192.168.1.111:8080/getImage?name=1493988882.jpg在byte []中给出一个图像。
我测试在PostExecute()上启动第二个asynctask但它不起作用..
这是我的第一个asynctask:
class MyAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String resultString = null;
resultString = getJSON(params[0]);
pictureList = intializePictures(resultString);
return resultString;
}
@Override
protected void onPostExecute(String strings) {
super.onPostExecute(strings);
MyAsyncTaskImage task = new MyAsyncTaskImage();
task.execute("http://192.168.1.111:8080/getImage?name=" + pictureList.get(0).url);
}
public String getJSON(String url) {
HttpURLConnection c = null;
try {
URL u = new URL(url);
c = (HttpURLConnection) u.openConnection();
c.connect();
int status = c.getResponseCode();
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
return sb.toString();
}
} catch (Exception ex) {
return ex.toString();
} finally {
if (c != null) {
try {
c.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
return null;
}
public List<Picture> intializePictures(String json) {
List<Picture> listPictures = new ArrayList<Picture>();
Picture picture;
try {
jsonArr = new JSONArray(json);
for (int i = 0; i < jsonArr.length(); i++) {
jsonObj = jsonArr.getJSONObject(i);
picture = new Picture();
picture.setDroneId(Integer.parseInt(jsonObj.getString("droneId")));
picture.setGps(jsonObj.getString("gps"));
picture.setUrl(jsonObj.getString("url"));
listPictures.add(picture);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return listPictures;
}
}
这里的借口:
class MyAsyncTaskImage extends AsyncTask<String, Void, byte[]> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected byte[] doInBackground(String... params) {
byte[] img;
img = getByteArrayImage(params[0]);
return img;
}
@Override
protected void onPostExecute(byte[] img) {
super.onPostExecute(img);
Bitmap bm = BitmapFactory.decodeByteArray(img, 0, img.length);
bitmapList.add(bm);
}
private byte[] getByteArrayImage(String url) {
try {
URL imageUrl = new URL(url);
URLConnection ucon = imageUrl.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(500);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
return baf.toByteArray();
} catch (Exception e) {
Log.d("ImageManager", "Error: " + e.toString());
}
return null;
}
}
logcat的:
05-27 15:55:17.471 3470-3485/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.huawei.lcagent.client.LogCollectManager.getUserType()' on a null object reference
05-27 15:55:17.471 3470-3485/? W/System.err: at com.android.server.util.ReportTools.getUserType(ReportTools.java:86)
05-27 15:55:17.471 3470-3485/? W/System.err: at com.android.server.util.ReportTools.isBetaUser(ReportTools.java:73)
05-27 15:55:17.471 3470-3485/? W/System.err: at com.android.server.util.ReportTools.report(ReportTools.java:58)
05-27 15:55:17.471 3470-3485/? W/System.err: at com.android.server.util.HwUserBehaviourRecord.appExitRecord(HwUserBehaviourRecord.java:65)
05-27 15:55:17.471 3470-3485/? W/System.err: at com.android.server.am.ActivityManagerService$UiHandler.handleMessage(ActivityManagerService.java:1572)
05-27 15:55:17.471 3470-3485/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
05-27 15:55:17.471 3470-3485/? W/System.err: at android.os.Looper.loop(Looper.java:150)
05-27 15:55:17.471 3470-3485/? W/System.err: at android.os.HandlerThread.run(HandlerThread.java:61)
05-27 15:55:17.471 3470-3485/? W/System.err: at com.android.server.ServiceThread.run(ServiceThread.java:46)