Trying to connect to web database

Corrornalcose

New Member
It's me again. Posting another question here, same as last. How do enable user input to be sent to a remote php server? I've searched through stack and some other resources and found alot of good hints. But I'm still having trouble. Since my last post I have updated my code to include an HTTP POST request imbedded into my Submit button. When I launch I get my form screen, I'm able to input info into the text fields, but when I click submit the the app stops working. For my Database I'm using Xeround. If anyone can tell what it is I'm missing that would be awesome. \[code\]package com.androidbook.john;import android.app.Activity;import android.os.Bundle;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.URL;import java.util.Calendar;import android.app.DatePickerDialog;import android.app.Dialog;import android.content.Context;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.DatePicker;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class DataBaseTest extends Activity { private TextView tvDisplayDate; private DatePicker dpResult; private Button btnChangeDate; private Button btnSubmit; private TextView FirstName, MiddleName, LastName; private int year, month, day; private EditText editFirst, editMiddle, editLast; static final int DATE_DIALOG_ID = 999; final Context context = this; protected Object savedInstanceState; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setCurrentDateOnView(); addListenerOnButton(); submitButton_handler(); editText_handler(); //Adding editText input to Submit Button editFirst = (EditText) findViewById(R.id.editFirst); editMiddle = (EditText) findViewById(R.id.editMiddle); editLast = (EditText) findViewById(R.id.editLast); btnSubmit = (Button) findViewById(R.id.btnSubmit); btnSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FirstName.setText(editFirst.getText().toString()); MiddleName.setText(editMiddle.getText().toString()); LastName.setText(editLast.getText().toString()); trybtnSubmit(editFirst, editMiddle, editLast); } }); } protected void trybtnSubmit(EditText editFirst, EditText editMiddle, EditText editLast) { // Connection after Submit button Clicked //HTTP POST HttpURLConnection connection; OutputStreamWriter request = null; URL url = null; String response = null; String parameters = "editFirst="+editFirst.getText().toString()+"editMiddle="+editMiddle.getText().toString()+"editLast="+editLast.getText().toString()+"dpResult="+dpResult; try { url = new URL("instance12008.db.xeround.com:8158"); connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); connection.setRequestMethod("POST"); //OutStream request = new OutputStreamWriter(connection.getOutputStream()); request.write(parameters); request.flush(); request.close(); String line = ""; InputStreamReader isr = new InputStreamReader(connection.getInputStream()); BufferedReader reader = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); while ((line = reader.readLine()) !=null) { sb.append(line + "\n"); } //Response from WebHost response = sb.toString(); Toast.makeText(this, "Message from Server: \n"+ response, 0).show(); isr.close(); reader.close(); } // Catch error exception catch(IOException e) { } } private void editText_handler() { // TODO Auto-generated method stub } public void submitButton_handler() { btnSubmit = (Button) findViewById(R.id.btnSubmit); btnSubmit.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub } }); } // display current date public void setCurrentDateOnView() { tvDisplayDate = (TextView) findViewById(R.id.tvDate); dpResult = (DatePicker) findViewById(R.id.dpResult); final Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); // set current date into textview tvDisplayDate.setText(new StringBuilder() // Month is 0 based, just add 1 .append(month + 1).append("-").append(day).append("-") .append(year).append(" ")); // set current date into datepicker dpResult.init(year, month, day, null); } public void addListenerOnButton() { btnChangeDate = (Button) findViewById(R.id.btnChangeDate); btnChangeDate.setOnClickListener(new OnClickListener() { @SuppressWarnings("deprecation") @Override public void onClick(View v) { showDialog(DATE_DIALOG_ID); System.out.println("ChangeDate btn clicked"); } }); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: // set date picker as current date return new DatePickerDialog(this, datePickerListener, year, month,day); } return null; } private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { // when dialog box is closed, below method will be called. public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { year = selectedYear; month = selectedMonth; day = selectedDay; // set selected date into textview tvDisplayDate.setText(new StringBuilder().append(month + 1) .append("-").append(day).append("-").append(year) .append(" ")); // set selected date into datepicker also dpResult.init(year, month, day, null); } };}\[/code\]Additionally to help you out, here is the xml layout:\[code\]<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="1" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/FirstName" /> <EditText android:id="@+id/editFirst" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="text" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/MiddleName" /> <EditText android:id="@+id/editMiddle" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="text" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/LastName" /> <EditText android:id="@+id/editLast" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="text" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/DOB" /> <Button android:id="@+id/btnChangeDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btnChangeDate" /> <TextView android:id="@+id/tvDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textAppearance="?android:attr/textAppearanceLarge" /> <DatePicker android:id="@+id/dpResult" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/Send" /> <Button android:id="@+id/btnSubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btnSubmit" /> </TableLayout></ScrollView>\[/code\]
 
Back
Top