Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 7.56 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. http post data not inserted but showing successful response
  2. public class DineOutActivity extends Activity {
  3.  
  4.     private TextView mDateDisplay;
  5.     private Button mPickDate;
  6.     private int mYear;
  7.     private int mMonth;
  8.     private int mDay;
  9.    /******************time picker**************/
  10.         private TextView mTimeDisplay;
  11.            private Button mPickTime;
  12.            private int mHour;
  13.            private int mMinute;
  14.            private int mAmPm;
  15.  
  16.            static final int TIME_DIALOG_ID=1;
  17.  
  18.         static final int DATE_DIALOG_ID = 0;
  19.  
  20.     @Override
  21.     public void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.main);
  24.  
  25.         /********************spinner***********/
  26.  
  27.         Spinner food = (Spinner) findViewById(R.id.spinner1);
  28.         ArrayAdapter<CharSequence> foodadapter = ArrayAdapter.createFromResource(
  29.                     this, R.array.item_array, android.R.layout.simple_spinner_item);
  30.         foodadapter.setDropDownViewResource(R.layout.spinner_layout);
  31.         food.setAdapter(foodadapter);
  32.         /**pick date*/
  33.  
  34.         mDateDisplay = (TextView) findViewById(R.id.textView2);
  35.         mTimeDisplay = (TextView) findViewById(R.id.textView4);
  36.         mPickDate = (Button) findViewById(R.id.button2);
  37.        /**pick time**/
  38.  
  39.         mPickTime=(Button)findViewById(R.id.button3);
  40.  
  41.         // add a click listener to the button
  42.         mPickTime.setOnClickListener(new View.OnClickListener() {
  43.  
  44.                      public void onClick(View v) {
  45.  
  46.                            showDialog(TIME_DIALOG_ID);
  47.                      }
  48.               });
  49.  
  50.         // get the current time
  51.         final Calendar c=Calendar.getInstance();
  52.         mHour=c.get(Calendar.HOUR_OF_DAY);
  53.         mMinute=c.get(Calendar.MINUTE);
  54.         mAmPm = c.get(Calendar.AM_PM);
  55.  
  56.         // display the current date
  57.        upTimeDisplay();
  58.  
  59.  
  60. /*****************************pick date***********************************/
  61.         // add a click listener to the button
  62.         mPickDate.setOnClickListener(new View.OnClickListener() {
  63.             public void onClick(View v1) {
  64.                 showDialog(DATE_DIALOG_ID);
  65.             }
  66.         });
  67.  
  68.         // get the current date
  69.         final Calendar date = Calendar.getInstance();
  70.         mYear = date.get(Calendar.YEAR);
  71.         mMonth = date.get(Calendar.MONTH);
  72.         mDay = date.get(Calendar.DAY_OF_MONTH);
  73.         int mDst = date.get(Calendar.AM_PM);
  74.         int mAmPm = date.get(Calendar.DST_OFFSET);
  75.  
  76.         // display the current date (this method is below)
  77.         updateDisplay();
  78.     }
  79.  
  80.     // updates the date in the TextView
  81.  
  82.  
  83.  
  84.  
  85.    private String upTimeDisplay()
  86.    {
  87.      //  mTimeDisplay.setText(new  
  88.            //    StringBuilder().append(pad(mHour)).append(":").append(pad(mMinute)).append(pad(mAmPm)));
  89.        mTimeDisplay.setText(new  
  90.                   StringBuilder().append(mHour).append(":").append(mMinute));
  91.        mTimeDisplay.setTextColor(R.color.green);
  92.     return null;
  93.    }
  94.   /** private Object pad(int mMinute2) {
  95.  
  96.        if(mMinute2>=10)
  97.               return String.valueOf(mMinute2);
  98.        else
  99.               return "0"+String.valueOf(mMinute2);
  100. }**/
  101.  
  102.    private TimePickerDialog.OnTimeSetListener mtimeSetListener=new  
  103.            TimePickerDialog.OnTimeSetListener() {
  104.  
  105. public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  106.  
  107. mHour=hourOfDay;
  108. mMinute=minute;
  109. int ampm;
  110.  
  111. upTimeDisplay();
  112. }
  113. };
  114.  
  115.  
  116.     private String updateDisplay() {
  117.         mDateDisplay.setText(new StringBuilder()
  118.                     // Month is 0 based so add 1
  119.                     .append(mMonth + 1).append("-")
  120.                     .append(mDay).append("-")
  121.                     .append(mYear).append(" "));
  122.         mDateDisplay.setTextColor(R.color.green);
  123.                  //   .append(mHour).append("_")
  124.                   //  .append(mMinute).append("_")));
  125.         return null;
  126.     }
  127.  
  128.     // the callback received when the user "sets" the date in the dialog
  129.     private DatePickerDialog.OnDateSetListener mDateSetListener =
  130.             new DatePickerDialog.OnDateSetListener() {
  131.  
  132.                 public void onDateSet(DatePicker view, int year,
  133.                                       int monthOfYear, int dayOfMonth) {
  134.                     mYear = year;
  135.                     mMonth = monthOfYear;
  136.                     mDay = dayOfMonth;
  137.                     updateDisplay();
  138.                 }
  139.             };
  140.  
  141.     @Override
  142.     protected Dialog onCreateDialog(int id) {
  143.         switch (id) {
  144.         case DATE_DIALOG_ID:
  145.             return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
  146.                     mDay);
  147.  
  148.         case TIME_DIALOG_ID:
  149.             return new TimePickerDialog(this,mtimeSetListener,mHour,mMinute,false);
  150.         }
  151.         return null;
  152.     }
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160. public  void imageclick(View view)
  161. {
  162.     postData();
  163.     //Intent where = new Intent(this, WhereToeat.class);
  164.     //startActivity(where);
  165. }
  166.  
  167. /**************/
  168.  
  169. public void postData() {
  170.  
  171.     DineOutActivity doa = null;
  172.     HttpClient httpclient = new DefaultHttpClient();
  173.     HttpPost httppost = new HttpPost("http://192.168.1.112/andro/index.php/androctrl/test/");
  174.  
  175.     try {
  176.  
  177.         //food item
  178.      Spinner spinner = (Spinner) findViewById(R.id.spinner1);
  179.      String fooditem = spinner.getSelectedItem().toString();
  180.  
  181.      TextView mDateDisplay = (TextView)findViewById(R.id.textView2);
  182.      String Date = mDateDisplay.toString();
  183.  
  184.      TextView mTimeDisplay  = (TextView)findViewById(R.id.textView4);
  185.      String Time = mTimeDisplay.toString();
  186.  
  187.  
  188.  
  189.         // Add your data
  190.         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
  191.         nameValuePairs.add(new BasicNameValuePair("fooditem", "fooditem"));
  192.         nameValuePairs.add(new BasicNameValuePair("Date", "Date"));
  193.         nameValuePairs.add(new BasicNameValuePair("Time", "Time"));
  194.         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  195.  
  196.         HttpResponse response = httpclient.execute(httppost);
  197.         String str = inputStreamToString(response.getEntity().getContent()).toString();
  198.         Log.w("ADPORTAL", str);
  199.         HttpStatus status;
  200.  
  201.         if(str.toString().equalsIgnoreCase("false"))
  202.         {Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
  203.         TextView result = (TextView)findViewById(R.id.textView5);
  204.             result.setText(str);
  205.             Log.w("ADPORTAL", str);
  206.         }
  207.         else
  208.         {TextView result = (TextView)findViewById(R.id.textView5);
  209.             result.setText(str);
  210.             Log.w("ADPORTAL", str);
  211.             Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();}
  212.  
  213.  
  214.  
  215.     } catch (ClientProtocolException e) {
  216.         // TODO Auto-generated catch block
  217.     } catch (IOException e) {
  218.         // TODO Auto-generated catch block
  219.     }
  220. }
  221.  
  222. private StringBuilder inputStreamToString(InputStream is) {
  223.     String line = "";
  224.     StringBuilder total = new StringBuilder();
  225.     // Wrap a BufferedReader around the InputStream
  226.     BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  227.     // Read response until the end
  228.     try {
  229.         while ((line = rd.readLine()) != null) {
  230.             total.append(line);
  231.         }
  232.     } catch (IOException e) {
  233.         e.printStackTrace();
  234.     }
  235.     // Return full string
  236.     return total;
  237. }
  238.        
  239. nameValuePairs.add(new BasicNameValuePair("fooditem", "fooditem"));
  240.     nameValuePairs.add(new BasicNameValuePair("Date", "Date"));
  241.     nameValuePairs.add(new BasicNameValuePair("Time", "Time"));
  242.        
  243. nameValuePairs.add(new BasicNameValuePair("fooditem", fooditem));
  244.     nameValuePairs.add(new BasicNameValuePair("Date", Date));
  245.     nameValuePairs.add(new BasicNameValuePair("Time", Time));