Advertisement
Guest User

Cocktails

a guest
Feb 16th, 2012
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.31 KB | None | 0 0
  1. package at.dtr.cocktails;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7.  
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.NameValuePair;
  11. import org.apache.http.client.HttpClient;
  12. import org.apache.http.client.entity.UrlEncodedFormEntity;
  13. import org.apache.http.client.methods.HttpPost;
  14. import org.apache.http.impl.client.DefaultHttpClient;
  15. import org.json.JSONArray;
  16. import org.json.JSONException;
  17. import org.json.JSONObject;
  18.  
  19. import android.app.ListActivity;
  20. import android.app.ProgressDialog;
  21. import android.os.Bundle;
  22. import android.os.Handler;
  23. import android.os.Message;
  24. import android.util.Log;
  25. import android.view.Menu;
  26. import android.view.MenuInflater;
  27. import android.view.MenuItem;
  28. import android.view.View;
  29. import android.widget.ArrayAdapter;
  30. import android.widget.ListView;
  31. import android.widget.Toast;
  32.  
  33. public class MainActivity extends ListActivity implements Runnable {
  34.     /** Called when the activity is first created. */
  35.     InputStream is;
  36.     ArrayList<String> cListArray = new ArrayList<String>();
  37.     ArrayList<String> cDetailsArray = new ArrayList<String>();
  38.     JSONObject json_data;
  39.     private ProgressDialog pd;
  40.  
  41.     @Override
  42.     public void onCreate(Bundle savedInstanceState) {
  43.         super.onCreate(savedInstanceState);
  44.        
  45.         // Kontrolle, ob eine Internetverbindung aufgebaut ist
  46.         if (AppStatus.getInstance(this).isOnline(this)) {
  47.                        
  48.             // ProgressDialog wird erstellt
  49.             pd = ProgressDialog.show(this, "", "loading...", true, false);
  50.            
  51.             // neuer Thread wird erstellt
  52.             Thread thread = new Thread(this);
  53.             // nach thread.start() wird die run()-Methode ausgeführt
  54.             thread.start();
  55.  
  56.            
  57.         } else {
  58.             Toast.makeText(this, R.string.e_connection, 10000).show();
  59.             Log.e("log_tag", "No network connection");
  60.         }
  61.  
  62.     }
  63.  
  64.     public void run() {
  65.         // TODO Auto-generated method stub
  66.        
  67.         String IP = "192.168.1.108";
  68.         String PhpFile = "getdata.php";
  69.        
  70.         // Liste der Cocktails
  71.         String cList = "";
  72.         // Details des jeweiligen Cocktails
  73.         String cDetails = "";
  74.        
  75.         ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  76.  
  77.         // Verbindung zur PHP-Datei wird hergestellt
  78.         try {
  79.             HttpClient httpclient = new DefaultHttpClient();
  80.             HttpPost httppost = new HttpPost("http://" + IP + "/" + PhpFile);
  81.             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  82.             HttpResponse response = httpclient.execute(httppost);
  83.             HttpEntity entity = response.getEntity();
  84.             is = entity.getContent();
  85.         } catch (Exception e) {
  86.             Log.e("log_tag", "Fehler bei der HTTP-Verbindung " + e.toString());
  87.         }
  88.  
  89.         // die Tabellendaten, die von der PHP-Datei ausgelesen wurden, werden in
  90.         // den String 'cList' gespeichert
  91.         try {
  92.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  93.                     is, "iso-8859-1"), 8);
  94.             StringBuilder sb = new StringBuilder();
  95.             String line = null;
  96.             while ((line = reader.readLine()) != null) {
  97.                 sb.append(line + "n");
  98.             }
  99.             is.close();
  100.             cList = sb.toString();
  101.         } catch (Exception e) {
  102.             Log.e("log_tag", "Error converting result " + e.toString());
  103.         }
  104.  
  105.         // String 'cList' wird in ein JSON-Array konvertiert
  106.         try {
  107.             JSONArray jArray = new JSONArray(cList);
  108.             for (int i = 0; i < jArray.length(); i++) {
  109.                 json_data = jArray.getJSONObject(i);
  110.                 cListArray.add((String) json_data.get("name"));
  111.             }
  112.  
  113.         } catch (JSONException e) {
  114.             Log.e("log_tag", "Error parsing data " + e.toString());
  115.         }
  116.         // leere Nachricht wird an den Handler gesendet
  117.         handler.sendEmptyMessage(0);
  118.  
  119.     }
  120.  
  121.     // neuer Handler wird erstellt
  122.     private Handler handler = new Handler() {
  123.         @Override
  124.         public void handleMessage(Message msg) {
  125.            
  126.             // ProgessDialog wird beendet und fillList() wird ausgeführt
  127.             pd.dismiss();
  128.             fillList();
  129.         }
  130.     };
  131.  
  132.    
  133.    
  134.     // Array 'cListArray' wird in die listView geschrieben
  135.     public void fillList() {
  136.        
  137.         this.setListAdapter(new ArrayAdapter<String>(this,
  138.                 android.R.layout.simple_list_item_1, cListArray));
  139.     }
  140.    
  141.    
  142.     // Array 'cListArray' wird neu befüllt
  143.     public void refreshList(){
  144.        
  145.         if (AppStatus.getInstance(this).isOnline(this)) {
  146.             cListArray.clear();
  147.             pd = ProgressDialog.show(this, "", "loading...", true, false);
  148.             Thread thread = new Thread(this);
  149.             thread.start();
  150.         } else {
  151.             Toast.makeText(this, R.string.e_connection, 10000).show();
  152.             Log.e("log_tag", "No network connection");
  153.             }
  154.     }
  155.  
  156.    
  157.    
  158.     @Override
  159.     protected void onListItemClick(ListView l, View v, int position, long id) {
  160.         String item = (String) getListAdapter().getItem(position);
  161.         Toast.makeText(this, item + " selected", Toast.LENGTH_SHORT).show();
  162.     }
  163.    
  164.    
  165.    
  166.    
  167.    
  168.     // Options Menu wird generiert, Daten kommen von /res/menu/menu.xml
  169.     @Override
  170.     public boolean onCreateOptionsMenu(Menu menu) {
  171.         MenuInflater inflater = getMenuInflater();
  172.         inflater.inflate(R.menu.menu, menu);
  173.         return true;
  174.     }
  175.  
  176.     // onClickListener fürs Options Menu
  177.     @Override
  178.     public boolean onOptionsItemSelected(MenuItem item) {
  179.         if (item.getItemId() == R.id.refresh) {
  180.             refreshList();
  181.         }
  182.         if (item.getItemId() == R.id.about) {
  183.             AboutDialog about = new AboutDialog(this);
  184.             about.setTitle(R.string.om_about_heading);
  185.             about.show();
  186.         }
  187.         return true;
  188.     }
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement