I want when I press the button, as the result I see the received text from server using a Toast class. My php
code is correct, because when I use content.setText(text);
instead the Toast.makeText(getApplication(),text,Toast.LENGTH_LONG);
, when I press the button I can see the received text from php
code. But I do not want use the TextView
class to display the received text. I want to display the received text using the Toast
class.
public class MainActivity extends Activity {
TextView content;
EditText fname;
String Name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
content = (TextView)findViewById( R.id.textView );
fname = (EditText)findViewById(R.id.editText);
Button saveme=(Button)findViewById(R.id.button);
saveme.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
try{
// CALL GetText method to make post method call
GetText();
}
catch(Exception ex)
{
content.setText(" url exeption! " );
}
}
});}
// Create GetText Metod
public void GetText() throws UnsupportedEncodingException
{
// Get user defined values
Name = fname.getText().toString();
// Create data variable for sent values to server
String data = URLEncoder.encode("name", "UTF-8")
+ "=" + URLEncoder.encode(Name, "UTF-8");
String text = "";
BufferedReader reader=null;
// Send data
try
{
// Defined URL where to send data
URL url = new URL("http://127.0.0.1:8080/apps/register.php");
// Send POST data request
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + "\n");
}
text = sb.toString();
}
catch(Exception ex)
{
}
finally
{
try
{
reader.close();
}
catch(Exception ex) {}
}
// Show response on activity
Toast.makeText(getApplication(),text,Toast.LENGTH_LONG);
}