Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Hajto/0f084954889c7d627d0e53bed2949a38 to your computer and use it in GitHub Desktop.
Save Hajto/0f084954889c7d627d0e53bed2949a38 to your computer and use it in GitHub Desktop.
package com.example.wk.projektkoncowywiolettakurek.Classes;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import com.example.wk.projektkoncowywiolettakurek.Collages.CollageActivity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
/**
* Created by Hecate on 2016-12-13.
*/
public class DownloadJSON extends AsyncTask<String, Void, String> {
private ProgressDialog pDialog; // kontrolka wyswietlana podczas długotrwałych operacji
JSONArray allImagesJson = new JSONArray();
private ArrayList<DownloadPhotosData> listImages = new ArrayList<>();
public DownloadJSON(Context context) {
pDialog = new ProgressDialog(context);
pDialog.setMessage("Pobieranie...");
pDialog.setCancelable(false); // nie da się zamknąć klikając w ekran
}
@Override
protected String doInBackground(String... strings) {
HttpPost httpPost = new HttpPost(strings[0]);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
String jsonString = null;
try {
jsonString = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(jsonString);
Log.e("result = ", jsonString);
} catch (JSONException e) {
e.printStackTrace();
}
try {
allImagesJson = jsonObj.getJSONArray("imagesData");
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < allImagesJson.length(); i++) {
JSONObject object = null;
try {
object = allImagesJson.getJSONObject(i);
String imageName = object.getString("imageName");
String imageSaveTime = object.getString("saveData");
listImages.add(new DownloadPhotosData(imageName, imageSaveTime));
Log.e("name:", imageName);
Log.e("data:", imageSaveTime);
} catch (JSONException e) {
e.printStackTrace();
}
}
Intent intent = new Intent();
intent.putExtra("imagesData", listImages);
return null;
}
@Override
protected void onPreExecute() {
pDialog.show();
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
pDialog.dismiss();
super.onPostExecute(s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment