Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.andro.id;
- import com.example.vnacademies.R;
- import java.io.BufferedReader;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TableLayout;
- import android.widget.TableRow;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.widget.TableRow.LayoutParams;
- public class ShowData extends Activity{
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.layout_data);
- Button button = (Button) findViewById(R.id.load_data);
- button.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view){
- String result = null;
- InputStream is = null;
- try{
- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httppost = new HttpPost("http://10.0.2.2/web-andro/php_files/proses.php?act=show");
- HttpResponse response = httpclient.execute(httppost);
- HttpEntity entity = response.getEntity();
- is = entity.getContent();
- Log.e("log_tag", "connection success ");
- // Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
- }catch(Exception e){
- Log.e("log_tag", "Error in http connection "+e.toString());
- Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
- }
- //convert response to string
- try{
- BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = reader.readLine()) != null){
- sb.append(line + "\n");
- // Toast.makeText(getApplicationContext(), "Input Reading pass", Toast.LENGTH_SHORT).show();
- }
- is.close();
- result=sb.toString();
- }catch(Exception e){
- Log.e("log_tag", "Error converting result "+e.toString());
- Toast.makeText(getApplicationContext(), " Input reading fail", Toast.LENGTH_SHORT).show();
- }
- //parse json data
- try{
- JSONArray jArray = new JSONArray(result);
- String re=jArray.getString(jArray.length()-1);
- TableLayout tv=(TableLayout) findViewById(R.id.tabeldata);
- tv.removeAllViewsInLayout();
- int flag=1;
- for(int i=-1;i<jArray.length()-1;i++){
- TableRow tr=new TableRow(ShowData.this);
- tr.setLayoutParams(new LayoutParams(
- LayoutParams.FILL_PARENT,
- LayoutParams.WRAP_CONTENT));
- if(flag==1){
- TextView b6=new TextView(ShowData.this);
- b6.setText("id");
- b6.setTextColor(Color.BLUE);
- b6.setTextSize(15);
- tr.addView(b6);
- TextView b19=new TextView(ShowData.this);
- b19.setPadding(10, 0, 0, 0);
- b19.setTextSize(15);
- b19.setText("Username");
- b19.setTextColor(Color.BLUE);
- tr.addView(b19);
- TextView b29=new TextView(ShowData.this);
- b29.setPadding(10, 0, 0, 0);
- b29.setText("Email");
- b29.setTextColor(Color.BLUE);
- b29.setTextSize(15);
- tr.addView(b29);
- TextView b39=new TextView(ShowData.this);
- b39.setPadding(10, 0, 0, 0);
- b39.setText("Fullname");
- b39.setTextColor(Color.BLUE);
- b39.setTextSize(15);
- tr.addView(b39);
- TextView b49=new TextView(ShowData.this);
- b49.setPadding(10, 0, 0, 0);
- b49.setText("Password");
- b49.setTextColor(Color.BLUE);
- b49.setTextSize(15);
- tr.addView(b49);
- tv.addView(tr);
- final View vline = new View(ShowData.this);
- vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
- vline.setBackgroundColor(Color.BLUE);
- tv.addView(vline);
- flag=0;
- }else{
- JSONObject json_data = jArray.getJSONObject(i);
- Log.i("log_tag","ID: "+json_data.getString("id")+
- ", Username: "+json_data.getString("username")+
- ", Email: "+json_data.getString("Email")+
- ", Fullname: "+json_data.getString("fullname")+
- ", Password: "+json_data.getString("password"));
- TextView b=new TextView(ShowData.this);
- String stime=json_data.getString("id");
- b.setText(stime);
- b.setTextColor(Color.RED);
- b.setTextSize(15);
- tr.addView(b);
- TextView b1=new TextView(ShowData.this);
- b1.setPadding(10, 0, 0, 0);
- b1.setTextSize(15);
- String stime1=json_data.getString("username");
- b1.setText(stime1);
- b1.setTextColor(Color.RED);
- tr.addView(b1);
- TextView b2=new TextView(ShowData.this);
- b2.setPadding(10, 0, 0, 0);
- String stime2=json_data.getString("email");
- b2.setText(stime2);
- b2.setTextColor(Color.RED);
- b2.setTextSize(15);
- tr.addView(b2);
- TextView b3=new TextView(ShowData.this);
- b3.setPadding(10, 0, 0, 0);
- b3.setTextSize(15);
- String stime3=json_data.getString("fullname");
- b3.setText(stime3);
- b3.setTextColor(Color.RED);
- tr.addView(b3);
- TextView b4=new TextView(ShowData.this);
- b4.setPadding(10, 0, 0, 0);
- String stime4=json_data.getString("password");
- b4.setText(stime4);
- b4.setTextColor(Color.RED);
- b4.setTextSize(15);
- tr.addView(b4);
- tv.addView(tr);
- final View vline1 = new View(ShowData.this);
- vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
- vline1.setBackgroundColor(Color.RED);
- tv.addView(vline1);
- }
- }
- }catch(JSONException e){
- Log.e("log_tag", "Error parsing data "+e.toString());
- Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement