Advertisement
sandywicaksono

Untitled

Jun 25th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.07 KB | None | 0 0
  1. package com.andro.id;
  2.  
  3. import com.example.vnacademies.R;
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.client.HttpClient;
  11. import org.apache.http.client.methods.HttpPost;
  12. import org.apache.http.impl.client.DefaultHttpClient;
  13. import org.json.JSONArray;
  14. import org.json.JSONException;
  15. import org.json.JSONObject;
  16. import android.app.Activity;
  17. import android.graphics.Color;
  18. import android.os.Bundle;
  19. import android.util.Log;
  20. import android.view.View;
  21. import android.widget.Button;
  22. import android.widget.TableLayout;
  23. import android.widget.TableRow;
  24. import android.widget.TextView;
  25. import android.widget.Toast;
  26. import android.widget.TableRow.LayoutParams;
  27.  
  28. public class ShowData extends Activity{
  29.     public void onCreate(Bundle savedInstanceState){
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.layout_data);
  32.        
  33.         Button button = (Button) findViewById(R.id.load_data);
  34.         button.setOnClickListener(new View.OnClickListener(){
  35.             public void onClick(View view){
  36.                 String result = null;
  37.                 InputStream is = null;
  38.                 try{
  39.                     HttpClient httpclient = new DefaultHttpClient();
  40.                     HttpPost httppost = new HttpPost("http://10.0.2.2/web-andro/php_files/proses.php?act=show");
  41.                     HttpResponse response = httpclient.execute(httppost);
  42.                     HttpEntity entity = response.getEntity();
  43.                     is = entity.getContent();
  44.  
  45.                     Log.e("log_tag", "connection success ");
  46.                      //   Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
  47.                 }catch(Exception e){
  48.                     Log.e("log_tag", "Error in http connection "+e.toString());
  49.                     Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
  50.                 }
  51.                 //convert response to string
  52.                 try{
  53.                     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
  54.                     StringBuilder sb = new StringBuilder();
  55.                     String line = null;
  56.                     while ((line = reader.readLine()) != null){
  57.                         sb.append(line + "\n");
  58.                         //  Toast.makeText(getApplicationContext(), "Input Reading pass", Toast.LENGTH_SHORT).show();
  59.                     }
  60.                     is.close();
  61.                     result=sb.toString();
  62.                 }catch(Exception e){
  63.                     Log.e("log_tag", "Error converting result "+e.toString());
  64.                     Toast.makeText(getApplicationContext(), " Input reading fail", Toast.LENGTH_SHORT).show();
  65.                 }
  66.                 //parse json data
  67.                 try{           
  68.                     JSONArray jArray = new JSONArray(result);                      
  69.                     String re=jArray.getString(jArray.length()-1);                 
  70.                     TableLayout tv=(TableLayout) findViewById(R.id.tabeldata);
  71.                     tv.removeAllViewsInLayout();                                                           
  72.                     int flag=1;
  73.                     for(int i=-1;i<jArray.length()-1;i++){
  74.                         TableRow tr=new TableRow(ShowData.this);
  75.                         tr.setLayoutParams(new LayoutParams(
  76.                         LayoutParams.FILL_PARENT,
  77.                         LayoutParams.WRAP_CONTENT));
  78.                         if(flag==1){
  79.                             TextView b6=new TextView(ShowData.this);
  80.                             b6.setText("id");
  81.                             b6.setTextColor(Color.BLUE);
  82.                             b6.setTextSize(15);
  83.                             tr.addView(b6);
  84.                            
  85.                             TextView b19=new TextView(ShowData.this);
  86.                             b19.setPadding(10, 0, 0, 0);
  87.                             b19.setTextSize(15);
  88.                             b19.setText("Username");
  89.                             b19.setTextColor(Color.BLUE);
  90.                             tr.addView(b19);
  91.                                    
  92.                             TextView b29=new TextView(ShowData.this);
  93.                             b29.setPadding(10, 0, 0, 0);
  94.                             b29.setText("Email");
  95.                             b29.setTextColor(Color.BLUE);
  96.                             b29.setTextSize(15);
  97.                             tr.addView(b29);
  98.                            
  99.                             TextView b39=new TextView(ShowData.this);
  100.                             b39.setPadding(10, 0, 0, 0);
  101.                             b39.setText("Fullname");
  102.                             b39.setTextColor(Color.BLUE);
  103.                             b39.setTextSize(15);
  104.                             tr.addView(b39);
  105.                            
  106.                             TextView b49=new TextView(ShowData.this);
  107.                             b49.setPadding(10, 0, 0, 0);
  108.                             b49.setText("Password");
  109.                             b49.setTextColor(Color.BLUE);
  110.                             b49.setTextSize(15);
  111.                             tr.addView(b49);
  112.                              
  113.                             tv.addView(tr);
  114.                             final View vline = new View(ShowData.this);
  115.                             vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
  116.                             vline.setBackgroundColor(Color.BLUE);
  117.                             tv.addView(vline);
  118.                             flag=0;
  119.                         }else{
  120.                             JSONObject json_data = jArray.getJSONObject(i);
  121.                             Log.i("log_tag","ID: "+json_data.getString("id")+
  122.                                             ", Username: "+json_data.getString("username")+
  123.                                             ", Email: "+json_data.getString("Email")+
  124.                                             ", Fullname: "+json_data.getString("fullname")+
  125.                                             ", Password: "+json_data.getString("password"));
  126.                             TextView b=new TextView(ShowData.this);
  127.                             String stime=json_data.getString("id");
  128.                             b.setText(stime);
  129.                             b.setTextColor(Color.RED);
  130.                             b.setTextSize(15);
  131.                             tr.addView(b);
  132.                                                    
  133.                             TextView b1=new TextView(ShowData.this);
  134.                             b1.setPadding(10, 0, 0, 0);
  135.                             b1.setTextSize(15);
  136.                             String stime1=json_data.getString("username");
  137.                             b1.setText(stime1);
  138.                             b1.setTextColor(Color.RED);
  139.                             tr.addView(b1);
  140.                            
  141.                             TextView b2=new TextView(ShowData.this);
  142.                             b2.setPadding(10, 0, 0, 0);
  143.                             String stime2=json_data.getString("email");
  144.                             b2.setText(stime2);
  145.                             b2.setTextColor(Color.RED);
  146.                             b2.setTextSize(15);
  147.                             tr.addView(b2);
  148.                            
  149.                             TextView b3=new TextView(ShowData.this);
  150.                             b3.setPadding(10, 0, 0, 0);
  151.                             b3.setTextSize(15);
  152.                             String stime3=json_data.getString("fullname");
  153.                             b3.setText(stime3);
  154.                             b3.setTextColor(Color.RED);
  155.                             tr.addView(b3);
  156.                            
  157.                             TextView b4=new TextView(ShowData.this);
  158.                             b4.setPadding(10, 0, 0, 0);
  159.                             String stime4=json_data.getString("password");
  160.                             b4.setText(stime4);
  161.                             b4.setTextColor(Color.RED);
  162.                             b4.setTextSize(15);
  163.                             tr.addView(b4);
  164.                      
  165.                             tv.addView(tr);
  166.                                                        
  167.                             final View vline1 = new View(ShowData.this);
  168.                             vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
  169.                             vline1.setBackgroundColor(Color.RED);
  170.                             tv.addView(vline1);                                                    
  171.                         }
  172.                     }
  173.                 }catch(JSONException e){
  174.                     Log.e("log_tag", "Error parsing data "+e.toString());
  175.                     Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
  176.                 }
  177.             }
  178.         });
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement