Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.layouty;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.UnsupportedEncodingException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.net.URLEncoder;
- import java.sql.Date;
- import java.sql.Time;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpStatus;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.R.integer;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.text.format.DateFormat;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.database.Cursor;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.database.sqlite.SQLiteDatabase;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- public class VyberAktualizovania extends Activity {
- private Databaza db = null;
- private Cursor constantsCursor = null;
- private String datumAktualizacii = "1.1.1940";
- private String pocetPodujati = "0";
- private String pocetSkupin = "0";
- private String pocetRecenzii = "0";
- private String dnesnyDatum = "0";
- private TextView datumAktualizacieTextview;
- private TextView pocetPodujatiTextview;
- private TextView pocetSkupinTextview;
- private TextView pocetRecenziiTextview;
- private int pocetPodujatiDoTabulky = 0;
- private ImageView downloadedImg;
- private ProgressDialog simpleWaitDialog;
- private String downloadUrl = "http://festival.vv.si/obrazky/";
- ArrayList<Integer> idecka = new ArrayList<Integer>();
- private String nazov=null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.aktualizovanie);
- datumAktualizacieTextview = (TextView) findViewById(R.id.NaposledyAktualizovanieVypis);
- pocetPodujatiTextview = (TextView) findViewById(R.id.pocetPodujatiVypis);
- pocetSkupinTextview = (TextView) findViewById(R.id.pocetSkupinVypis);
- pocetRecenziiTextview = (TextView) findViewById(R.id.pocetRecenziiVypis);
- downloadedImg = (ImageView) findViewById(R.id.obrazok);
- db = new Databaza(this);
- // WebServer Request URL
- String serverURL = "http://festival.vv.si/insert.php";
- // Use AsyncTask execute Method To Prevent ANR Problem
- db = new Databaza(this);
- constantsCursor = db.getReadableDatabase().rawQuery(
- "SELECT _id, datumAktualizacie, pocetPodujati, pocetSkupin, pocetRecenzii "
- + "FROM aktualizacia order by _id asc",
- null);
- while (constantsCursor.moveToNext()) {
- datumAktualizacii = (constantsCursor.getString(constantsCursor
- .getColumnIndex("datumAktualizacie")));
- pocetPodujati = constantsCursor.getString(constantsCursor
- .getColumnIndex("pocetPodujati"));
- pocetSkupin = constantsCursor.getString(constantsCursor
- .getColumnIndex("pocetSkupin"));
- pocetRecenzii = constantsCursor.getString(constantsCursor
- .getColumnIndex("pocetRecenzii"));
- }
- SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");
- dnesnyDatum = df.format(Calendar.getInstance().getTime());
- datumAktualizacii += " / ";
- pocetPodujati += " / ";
- pocetSkupin += " / ";
- pocetRecenzii += " / ";
- new LongOperation().execute(serverURL);
- }
- // Class with extends AsyncTask class
- protected class LongOperation extends AsyncTask<String, Void, Void> {
- // Required initialization
- private final HttpClient Client = new DefaultHttpClient();
- private String Content;
- private String Error = null;
- private ProgressDialog Dialog = new ProgressDialog(VyberAktualizovania.this);
- String data = "";
- protected void onPreExecute() {
- // NOTE: You can call UI Element here.
- //Start Progress Dialog (Message)
- String data ="";
- Dialog.setMessage("Prosim cakajte..");
- Dialog.show();
- try{
- // Set Request parameter
- data +="&" + URLEncoder.encode("data", "UTF-8") ;
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- // Call after onPreExecute method
- protected Void doInBackground(String... urls) {
- BufferedReader reader = null;
- // Send data
- try {
- // Defined URL where to send data
- URL url = new URL(urls[0]);
- // Send POST data request
- URLConnection conn = url.openConnection();
- conn.setDoOutput(true);
- OutputStreamWriter wr = new OutputStreamWriter(
- conn.getOutputStream());
- wr.write(data);
- wr.flush();
- // Get the server response
- reader = new BufferedReader(new InputStreamReader(
- conn.getInputStream()));
- StringBuilder sb = new StringBuilder();
- String line = null;
- // Read Server Response
- while ((line = reader.readLine()) != null) {
- // Append server response in string
- sb.append(line + " ");
- }
- // Append Server Response To Content String
- Content = sb.toString();
- } catch (Exception ex) {
- Error = ex.getMessage();
- } finally {
- try {
- reader.close();
- }
- catch (Exception ex) {
- }
- }
- return null;
- }
- protected void onPostExecute(Void unused) {
- // NOTE: You can call UI Element here.
- // Close progress dialog
- Dialog.dismiss();
- if (Error != null) {
- // uiUpdate.setText("Output : "+Error);
- } else {
- // Show Response Json On Screen (activity)
- // uiUpdate.setText( Content );
- /****************** Start Parse Response JSON Data *************/
- String OutputData = "";
- JSONObject jsonResponse;
- try {
- // premazanie tabuliek
- db.getReadableDatabase().execSQL(
- "delete from skupina_podujatie");
- db.getReadableDatabase().execSQL("delete from album");
- db.getReadableDatabase().execSQL("delete from clenSkupiny");
- db.getReadableDatabase().execSQL("delete from recenzia");
- db.getReadableDatabase()
- .execSQL("delete from castProgramu");
- db.getReadableDatabase().execSQL("delete from skupina");
- db.getReadableDatabase().execSQL("delete from podujatie");
- // db.getReadableDatabase()
- // .execSQL("delete from aktualizacia");
- /******
- * Creates a new JSONObject with name/value mappings from
- * the JSON string.
- ********/
- jsonResponse = new JSONObject(Content);
- /*****
- * Returns the value mapped by name if it exists and is a
- * JSONArray.
- ***/
- /******* Returns null otherwise. *******/
- /********************************** naplnanie tabulky podujatie **********************/
- JSONArray podujatie = jsonResponse
- .optJSONArray("podujatie");
- /*********** Process each JSON Node ************/
- int lengthJsonArr = podujatie.length();
- for (int i = 0; i < lengthJsonArr; i++) {
- /****** Get Object for each JSON node. ***********/
- JSONObject jsonChildPodujatie = podujatie
- .getJSONObject(i);
- /******* Fetch node values **********/
- int idInsertPodujatie = jsonChildPodujatie
- .getInt("idI");
- idecka.add(idInsertPodujatie);
- String nazovPodujatiaInsert = jsonChildPodujatie
- .getString("nazovPodujatiaI");
- String popisPodujatiaInsert = jsonChildPodujatie
- .getString("popisPodujatiaI");
- String webInsert = jsonChildPodujatie.getString("webI");
- String miestoKonaniaInsert = jsonChildPodujatie
- .getString("miestoKonaniaI");
- String odDatumInsert = jsonChildPodujatie
- .getString("odDatumI");
- String doDatumInsert = jsonChildPodujatie
- .getString("doDatumI");
- String doVstupneInsert = jsonChildPodujatie
- .getString("vstupneI");
- double latitudeInsert = jsonChildPodujatie
- .getDouble("latitudeI");
- double longitudeInsert = jsonChildPodujatie
- .getDouble("longitudeI");
- db.getReadableDatabase()
- .execSQL(
- "Insert into podujatie(_id,nazovPodujatia,popisPodujatia,web,"
- + "odDatum,doDatum,miestoKonania,vstupne,latitude,longitude)"
- + " values("
- + idInsertPodujatie + ",'"
- + nazovPodujatiaInsert + "','"
- + popisPodujatiaInsert + "','"
- + webInsert + "','"
- + odDatumInsert + "','"
- + doDatumInsert + "','"
- + miestoKonaniaInsert + "',"
- + doVstupneInsert+","
- + latitudeInsert + ","
- + longitudeInsert + ")");
- }
- /***************************** naplnanie tabulky skupiny *********************/
- JSONArray skupiny = jsonResponse.optJSONArray("skupina");
- /*********** Process each JSON Node ************/
- lengthJsonArr = skupiny.length();
- for (int i = 0; i < lengthJsonArr; i++) {
- /****** Get Object for each JSON node. ***********/
- JSONObject jsonChildSkupiny = skupiny.getJSONObject(i);
- /******* Fetch node values **********/
- int idInsertSkupina = jsonChildSkupiny.getInt("idI");
- String nazovSkupinyInsert = jsonChildSkupiny
- .getString("nazovSkupinyI");
- String krajinaPovoduInsert = jsonChildSkupiny
- .getString("krajinaPovoduI");
- int rokZalozeniaInsert = jsonChildSkupiny
- .getInt("rokZalozeniaSkupinyI");
- String popisSkupinyInsert = jsonChildSkupiny
- .getString("popisSkupinyI");
- String zanerSkupinyInsert = jsonChildSkupiny
- .getString("zanerSkupinyI");
- db.getReadableDatabase().execSQL(
- "Insert into skupina(_id,nazovSkupiny,krajinaPovodu,rokZalozeniaSkupiny,"
- + "popisSkupiny,zanerSkupiny)"
- + " values(" + idInsertSkupina + ",'"
- + nazovSkupinyInsert + "','"
- + krajinaPovoduInsert + "',"
- + rokZalozeniaInsert + ",'"
- + popisSkupinyInsert + "','"
- + zanerSkupinyInsert + "')");
- }
- /************************************** naplnanie tabulky castProgramu ************************/
- JSONArray castProgramu = jsonResponse
- .optJSONArray("castProgramu");
- /*********** Process each JSON Node ************/
- lengthJsonArr = castProgramu.length();
- for (int i = 0; i < lengthJsonArr; i++) {
- /****** Get Object for each JSON node. ***********/
- JSONObject jsonChildPodujatie = castProgramu
- .getJSONObject(i);
- /******* Fetch node values **********/
- int idInsertCastProgramu = jsonChildPodujatie
- .getInt("idI");
- String popisCastiProgramuInsert = jsonChildPodujatie
- .getString("popisCastiProgramuI");
- String datumCastiProgramuInsert = jsonChildPodujatie
- .getString("datumCastiProgramuI");
- int podujatie_idInsert = jsonChildPodujatie
- .getInt("podujatie_idI");
- db.getReadableDatabase().execSQL(
- "Insert into castProgramu(_id,popisCastiProgramu,"
- + "datumCastiProgramu,podujatie_id)"
- + " values(" + idInsertCastProgramu + ",'"
- + popisCastiProgramuInsert + "','"
- + datumCastiProgramuInsert + "',"
- + podujatie_idInsert + ")");
- }
- /************************************** naplnanie tabulky recenzia ************************/
- JSONArray recenzia = jsonResponse.optJSONArray("recenzia");
- /*********** Process each JSON Node ************/
- lengthJsonArr = recenzia.length();
- for (int i = 0; i < lengthJsonArr; i++) {
- /****** Get Object for each JSON node. ***********/
- JSONObject jsonChildPodujatie = recenzia
- .getJSONObject(i);
- /******* Fetch node values **********/
- int idInsertRecenzia = jsonChildPodujatie.getInt("idI");
- String prezyvkaInsert = jsonChildPodujatie
- .getString("prezyvkaI");
- String hlavickaInsert = jsonChildPodujatie
- .getString("hlavickaI");
- String popisRecenzieInsert = jsonChildPodujatie
- .getString("popisRecenzieI");
- int pocetHviezdInsert = jsonChildPodujatie
- .getInt("pocetHviezdI");
- int podujatie_idInsert = jsonChildPodujatie
- .getInt("podujatie_idI");
- db.getReadableDatabase().execSQL(
- "Insert into recenzia(_id,prezyvka,hlavicka,popisRecenzie,"
- + "pocetHviezd,podujatie_id)"
- + " values(" + idInsertRecenzia + ",'"
- + prezyvkaInsert + "','"
- + hlavickaInsert + "','"
- + popisRecenzieInsert + "',"
- + pocetHviezdInsert + ","
- + podujatie_idInsert + ")");
- }
- /************************************** naplnanie tabulky clenSkupiny ************************/
- JSONArray clenSkupiny = jsonResponse
- .optJSONArray("clenSkupiny");
- /*********** Process each JSON Node ************/
- lengthJsonArr = clenSkupiny.length();
- for (int i = 0; i < lengthJsonArr; i++) {
- /****** Get Object for each JSON node. ***********/
- JSONObject jsonChildPodujatie = clenSkupiny
- .getJSONObject(i);
- /******* Fetch node values **********/
- int idInsertClenSkupiny = jsonChildPodujatie
- .getInt("idI");
- String menoInsert = jsonChildPodujatie
- .getString("menoI");
- int rokVstupuInsert = jsonChildPodujatie
- .getInt("rokVstupuI");
- String poziciaInsert = jsonChildPodujatie
- .getString("poziciaI");
- int skupina_idInsert = jsonChildPodujatie
- .getInt("skupina_idI");
- db.getReadableDatabase().execSQL(
- "Insert into clenSkupiny(_id,meno,rokVstupu,pozicia,"
- + "skupina_id)" + " values("
- + idInsertClenSkupiny + ",'"
- + menoInsert + "'," + rokVstupuInsert
- + ",'" + poziciaInsert + "',"
- + skupina_idInsert + ")");
- }
- /************************************** naplnanie tabulky album ************************/
- JSONArray album = jsonResponse.optJSONArray("album");
- /*********** Process each JSON Node ************/
- lengthJsonArr = album.length();
- for (int i = 0; i < lengthJsonArr; i++) {
- /****** Get Object for each JSON node. ***********/
- JSONObject jsonChildPodujatie = album.getJSONObject(i);
- /******* Fetch node values **********/
- int idInsertAlbum = jsonChildPodujatie.getInt("idI");
- String nazovAlbumuInsert = jsonChildPodujatie
- .getString("nazovAlbumuI");
- int rokVydaniaInsert = jsonChildPodujatie
- .getInt("rokVydaniaI");
- int skupina_idInsert = jsonChildPodujatie
- .getInt("skupina_idI");
- db.getReadableDatabase().execSQL(
- "Insert into album(_id,nazovAlbumu,rokVydania,"
- + "skupina_id)" + " values("
- + idInsertAlbum + ",'"
- + nazovAlbumuInsert + "',"
- + rokVydaniaInsert + ","
- + skupina_idInsert + ")");
- }
- /************************************** naplnanie tabulky skupina_podujatie ************************/
- JSONArray skupina_podujatie = jsonResponse
- .optJSONArray("skupina_podujatie");
- /*********** Process each JSON Node ************/
- lengthJsonArr = skupina_podujatie.length();
- for (int i = 0; i < lengthJsonArr; i++) {
- /****** Get Object for each JSON node. ***********/
- JSONObject jsonChildPodujatie = skupina_podujatie
- .getJSONObject(i);
- /******* Fetch node values **********/
- int idInsertSkupina_Podujatie = jsonChildPodujatie
- .getInt("idI");
- int podujatie_idInsert = jsonChildPodujatie
- .getInt("podujatie_idI");
- int skupina_idInsert = jsonChildPodujatie
- .getInt("skupina_idI");
- db.getReadableDatabase().execSQL(
- "Insert into skupina_podujatie(_id,podujatie_id,skupina_id)"
- + " values("
- + idInsertSkupina_Podujatie + ","
- + podujatie_idInsert + ","
- + skupina_idInsert + ")");
- }
- /****************** End Parse Response JSON Data *************/
- //Context context = getApplicationContext();
- //int duration = Toast.LENGTH_SHORT;
- /*
- * Toast toast = Toast.makeText(context, OutputData,
- * duration); toast.show();
- */
- // Show Parsed Output on screen (activity)
- // jsonParsed.setText( OutputData );
- Dialog.dismiss();
- int pocetSkupinDoTabulky =0;
- int pocetRecenziiDoTabulky = 0;
- /************************** pridanie aktualneho poctu podujati ***************/
- constantsCursor = db.getReadableDatabase().rawQuery(
- "SELECT _id, count(_id) " + "FROM podujatie", null);
- while (constantsCursor.moveToNext()) {
- pocetPodujatiDoTabulky = constantsCursor.getInt(constantsCursor
- .getColumnIndex("count(_id)"));
- }
- /************************** pridanie aktualneho poctu skupin ***************/
- constantsCursor = db.getReadableDatabase().rawQuery(
- "SELECT _id, count(_id) " + "FROM skupina", null);
- while (constantsCursor.moveToNext()) {
- pocetSkupinDoTabulky = constantsCursor.getInt(constantsCursor
- .getColumnIndex("count(_id)"));
- }
- /************************** pridanie aktualneho poctu recenzii ***************/
- constantsCursor = db.getReadableDatabase().rawQuery(
- "SELECT _id, count(_id) " + "FROM recenzia", null);
- while (constantsCursor.moveToNext()) {
- pocetRecenziiDoTabulky = constantsCursor.getInt(constantsCursor
- .getColumnIndex("count(_id)"));
- }
- datumAktualizacii += dnesnyDatum;
- pocetPodujati += pocetPodujatiDoTabulky;
- pocetSkupin += pocetSkupinDoTabulky;
- pocetRecenzii += pocetRecenziiDoTabulky;
- /*************************** aktualizacia tabulky aktualizacii ***********************/
- db.getReadableDatabase().execSQL(
- "Insert into aktualizacia(datumAktualizacie,pocetPodujati,"
- + "pocetSkupin,pocetRecenzii) values('"
- + dnesnyDatum + "'," + pocetPodujatiDoTabulky + ","
- + pocetSkupinDoTabulky + "," + pocetRecenziiDoTabulky
- + ")");
- /***************************/
- datumAktualizacieTextview.setText(datumAktualizacii);
- pocetPodujatiTextview.setText(pocetPodujati);
- pocetSkupinTextview.setText(pocetSkupin);
- pocetRecenziiTextview.setText(pocetRecenzii);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- //for(int i=0; i<idecka.size();i++){
- // nazov=""+idecka.get(i);
- String urlObrazku =downloadUrl+"5.png";
- // System.out.println(""+downloadUrl+idecka.get(i)+".png");
- // new ImageDownloader().execute(urlObrazku);
- urlObrazku =downloadUrl+"4.png";
- new ImageDownloader().execute(urlObrazku);
- //}
- }
- }
- }
- private class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
- @Override
- protected Bitmap doInBackground(String... param) {
- // TODO Auto-generated method stub
- return downloadBitmap(param[0]);
- }
- @Override
- protected void onPreExecute() {
- Log.i("Async-Example", "onPreExecute Called");
- simpleWaitDialog = ProgressDialog.show(VyberAktualizovania.this,"Wait", "Stahujem obrazky");
- }
- @Override
- protected void onPostExecute(Bitmap result) {
- Log.i("Async-Example", "onPostExecute Called");
- simpleWaitDialog.dismiss();
- FileOutputStream fos;
- try {
- fos = openFileOutput(nazov, Context.MODE_PRIVATE);
- result.compress(Bitmap.CompressFormat.PNG, 100, fos);
- fos.close();
- }catch(Exception e){
- }
- FileInputStream fis;
- Bitmap bitmapA=null;
- try {
- fis = openFileInput(nazov);
- bitmapA = BitmapFactory.decodeStream(fis);
- fis.close();
- }catch(Exception e){
- }
- //downloadedImg.setImageBitmap(bitmapA);
- }
- private Bitmap downloadBitmap(String url) {
- // initilize the default HTTP client object
- final DefaultHttpClient client = new DefaultHttpClient();
- //forming a HttoGet request
- final HttpGet getRequest = new HttpGet(url);
- try {
- HttpResponse response = client.execute(getRequest);
- //check 200 OK for success
- final int statusCode = response.getStatusLine().getStatusCode();
- if (statusCode != HttpStatus.SC_OK) {
- Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
- return null;
- }
- final HttpEntity entity = response.getEntity();
- if (entity != null) {
- InputStream inputStream = null;
- try {
- // getting contents from the stream
- inputStream = entity.getContent();
- // decoding stream data back into image Bitmap that android understands
- final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
- return bitmap;
- } finally {
- if (inputStream != null) {
- inputStream.close();
- }
- entity.consumeContent();
- }
- }
- } catch (Exception e) {
- // You Could provide a more explicit error message for IOException
- getRequest.abort();
- Log.e("ImageDownloader", "Something went wrong while retrieving bitmap from " + url + e.toString());
- }
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement