How can i connect android Button to a web page button? - android

Suppose I have some text fields and a button. Can I link that button to pass the text field values to the webpage button on button click in my app?

Yes.Check this example: How To Post Data From An Android App To a Website
MainActivity:
public class HelloWorldActivity extends Activity {
Button sendButton;
EditText msgTextField;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// load the layout
setContentView(R.layout.main);
// make message text field object
msgTextField = (EditText) findViewById(R.id.msgTextField);
// make send button object
sendButton = (Button) findViewById(R.id.sendButton);
}
// this is the function that gets called when you click the button
public void send(View v)
{
// get the message from the message text box
String msg = msgTextField.getText().toString();
// make sure the fields are not empty
if (msg.length()>0)
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://yourwebsite.com/yourPhpScript.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("message", msg));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
msgTextField.setText(""); // clear text box
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
else
{
// display message if text fields are empty
Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
}
}
}
Server Side Codes:
<?php
// get the "message" variable from the post request
// this is the data coming from the Android app
$message=$_POST["message"];
// specify the file where we will save the contents of the variable message
$filename="androidmessages.html";
// write (append) the data to the file
file_put_contents($filename,$message."<br />",FILE_APPEND);
// load the contents of the file to a variable
$androidmessages=file_get_contents($filename);
// display the contents of the variable (which has the contents of the file)
echo $androidmessages;
?>

Related

Submit The Form Via POST Method

While sending the message I got application error and the app stops. I wanted to submit the form using POST method. Please help me to correct the code as i am new to android.
I have taken code reference from http://www.onlymobilepro.com/2013/03/16/submitting-android-form-data-via-post-method/
public class MainActivity extends Activity {
EditText msgTextField;
Button sendButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
//make message text field object
msgTextField = (EditText) findViewById(R.id.msgTextField);
//make button object
sendButton = (Button) findViewById(R.id.sendButton);
}
public void send(View v)
{
//get message from message box
String msg = msgTextField.getText().toString();
//check whether the msg empty or not
if(msg.length()>0) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yourdomain.com/serverside-script.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "01"));
nameValuePairs.add(new BasicNameValuePair("message", msg));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
msgTextField.setText(""); //reset the message text field
Toast.makeText(getBaseContext(),"Sent",Toast.LENGTH_SHORT).show();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
//display message if text field is empty
Toast.makeText(getBaseContext(),"All fields are required",Toast.LENGTH_SHORT).show();
}
}
You are doing network operation on Main Thread , it needs to be done in seperate Thread .
Do something like this:
To know how to use AsyncTask and set its parameters, see this :
What arguments are passed into AsyncTask<arg1, arg2, arg3>?
EditText msgTextField;
Button sendButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
//make message text field object
msgTextField = (EditText) findViewById(R.id.msgTextField);
//make button object
sendButton = (Button) findViewById(R.id.sendButton);
}
public void send(View v) {
//get message from message box
String msg = msgTextField.getText().toString();
if (!msg.isEmpty()) {
new PostData().execute(msg);
} else {
//display message if text field is empty
Toast.makeText(getBaseContext(), "All fields are required", Toast.LENGTH_SHORT).show();
}
}
public class PostData extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yourdomain.com/serverside-script.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "01"));
nameValuePairs.add(new BasicNameValuePair("message", params[0]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
try {
HttpResponse response = httpclient.execute(httppost);
String op = EntityUtils.toString(response.getEntity(), "UTF-8");//The response you get from your script
return op;
} catch (IOException e) {
e.printStackTrace();
}
//reset the message text field
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
msgTextField.setText("");
Toast.makeText(getBaseContext(), "Sent", Toast.LENGTH_SHORT).show();
}
}

START INPUT: android.widget.EditText

i want to sava data from android app to local host in my pc.
i write a edittext to get my text:
<EditText
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
and write a method to send data to localhost:
public void send(View v3)
{
String msg = edittext2.getText().toString();
// make sure the fields are not empty
if (msg.length()>0)
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://localhost/datalog.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("message", msg));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
edittext2.setText(""); // clear text box
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
// Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
}
}
else
{
// display message if text fields are empty
Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
}
}
and call method by sendbutton:
sendButton = (Button) findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v3) {
// TODO Auto-generated method stub
// send datasend=new send();
//datasend.execute();
send(v3);
}
});
but get a massage that:
08-10 01:14:26.171: V/InputMethodManager(20557): START INPUT: android.widget.EditText{41812b10 VFED..CL .F....ID 0,672-225,731 #7f080013 app:id/text2} ic=com.android.internal.widget.EditableInputConnection#418662b0 tba=android.view.inputmethod.EditorInfo#41866268 controlFlags=#100
i write a php code in server side ti get post variable.
I hope you do the Http Request in a thread outside of the main gui thread.
Can you try this code:
MainActivity:
public class MainActivity extends Activity {
private final static String TAG = MainActivity.class.getSimpleName();
EditText edittext2;
Button sendButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext2 = (EditText) findViewById(R.id.text2);
sendButton = (Button) findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
send();
}
});
}
public void send() {
String msg = edittext2.getText().toString();
// make sure the fields are not empty
if (!msg.equals(""))
{
Log.d(TAG, "send: " + msg);
// HttpClient httpclient = new DefaultHttpClient();
// HttpPost httppost = new HttpPost("https://localhost/datalog.php");
//
// try {
// List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// nameValuePairs.add(new BasicNameValuePair("id", "12345"));
// nameValuePairs.add(new BasicNameValuePair("message", msg));
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// httpclient.execute(httppost);
// edittext2.setText(""); // clear text box
// } catch (ClientProtocolException e) {
// // TODO Auto-generated catch block
// } catch (IOException e) {
// // TODO Auto-generated catch block
// // Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
// }
}
else
{
// display message if text fields are empty
Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
}
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<EditText
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/sendButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="send"
android:layout_below="#id/text2"/>
</RelativeLayout>
This is working well for me, i am interested if its same for you
Edit after get more information:
I tested it now with the simple app and another php file at my computer.
The app is connectiong to the php file, which creates a message file. This message file can be accessed eg via Browser.
The Http part have to run in an task which i did this way:
MainActivity:
public class MainActivity extends Activity {
private final static String TAG = MainActivity.class.getSimpleName();
EditText edittext2;
Button sendButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext2 = (EditText) findViewById(R.id.text2);
sendButton = (Button) findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MyHttpTask mTask = new MyHttpTask();
mTask.execute("");
}
});
}
class MyHttpTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
send();
return "";
}
#Override
protected void onPostExecute(String result) {
Log.d(TAG, "onPostExecute" + result);
edittext2.setText(result);
}
}
public void send() {
String msg = edittext2.getText().toString();
// make sure the fields are not empty
if (!msg.equals(""))
{
Log.d(TAG, "send: " + msg);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.178.60/datalog.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("message", msg));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
//edittext2.setText(""); // clear text box
} catch (ClientProtocolException e) {
Log.d(TAG, e.toString());
} catch (IOException e) {
Log.d(TAG, e.toString());
}
}
else
{
// display message if text fields are empty
Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
}
}
}
Make sure its NOT localhost do to this is nor possible to connect except theAndroid device is running the http server with php.
php file datalog.php:
<?php
$message=$_POST['message'];
$filename="androidmessages.html";
file_put_contents($filename,$message."<br />",FILE_APPEND);
$androidmessages=file_get_contents($filename);
echo $androidmessages;
?>
I also get a file called "androidmessages.html" with the text i key in into edittext
For me this is working well without any error
The sense of this job would be to have a web page which is logging the messages from the android device. So it would be possible to view the messages from any browser via network connection

android input validation not working as expected

I am trying to perform simple form validation whereby I use EditText.setError() to notify the user of the wrong input or blank field. Unfortunately, when I do that it only shows error when I click on the field again after incomplete form submission. This is weird because I want it to show as soon as I click button and form incomplete.
I believe it has something to do with the placement of the code that does the validation? Following is my code:
public class AddDiscountActivity extends Activity implements OnItemSelectedListener{
String shopCategory;
Spinner spinner;
String shopName;
String shopCity;
String shopLocation;
String discountRate;
String discountDuration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adddiscount_activity);
spinner = (Spinner) findViewById(R.id.categoriesSpinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.categoriesArray, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
public void SubmitData(View view)
{
new PostDataAsyncTask().execute();
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
// TODO Auto-generated method stub
shopCategory = spinner.getItemAtPosition(pos).toString();
Log.v("SHOP CATEGORY***********: ", shopCategory);
}
public class PostDataAsyncTask extends AsyncTask<String, String, String>
{
ProgressDialog progressDialog;
#Override
protected void onPreExecute()
{
progressDialog= ProgressDialog.show(AddDiscountActivity.this, "Please Wait","Update Ads listings", true);
//do initialization of required objects objects here
};
#Override
protected String doInBackground(String... strings) {
// TODO Auto-generated method stub
try {
postAdData();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String lenghtOfFile) {
// do stuff after posting data
super.onPostExecute(lenghtOfFile);
progressDialog.dismiss();
//Intent intent = new Intent(MainActivity.this, ThankYouAcitivty.class);
// startActivity(intent);
}
}
private void postAdData() throws JSONException{
try{
// url where the data will be posted
String postReceiverUrl = "http://hye.com/displaypost.php";
// HttpClient
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
// add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//All user input
EditText shopNameEditText = (EditText)findViewById(R.id.shopName);
EditText shopLocationEditText = (EditText)findViewById(R.id.shopLocation);
EditText shopCityEditText = (EditText)findViewById(R.id.shopCity);
EditText discountRateEditText = (EditText)findViewById(R.id.shopDiscount);
EditText discountDurationEditText = (EditText)findViewById(R.id.shopDiscountDuration);
shopNameEditText.getText().toString();
shopLocationEditText.getText().toString();
shopCityEditText.getText().toString();
discountRateEditText.getText().toString();
discountDurationEditText.getText().toString();
/*******Fields Validation*********/
if(shopNameEditText.getText().toString().length() == 0)
shopNameEditText.setError("يجب ادخال اسم المحل");
if(shopLocationEditText.getText().toString().length() == 0)
shopLocationEditText.setError("يجب ادخال العنوان");
if(shopCityEditText.getText().toString().length() == 0)
shopCityEditText.setError("يجب ادخال المدينة");
if(discountRateEditText.getText().toString().length() == 0)
discountRateEditText.setError("يجب ادخال نسبة التخفيض");
/*********************************/
nameValuePairs.add(new BasicNameValuePair("name", shopName));
nameValuePairs.add(new BasicNameValuePair("location", shopLocation));
nameValuePairs.add(new BasicNameValuePair("city", shopCity));
nameValuePairs.add(new BasicNameValuePair("rate", discountRate));
nameValuePairs.add(new BasicNameValuePair("duration", discountDuration));
nameValuePairs.add(new BasicNameValuePair("category", shopCategory));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v("", "Response: " + responseStr);
// you can add an if statement here and do other actions based on the response
}
} catch (IOException e) {
e.printStackTrace();
}
}
try by putting // All user input // // Fields Validation // inside public void SubmitData(View view) and use if{} else{}
you will Also get null pointer Exception because you are not assigning any value to
String shopName;
String shopCity;
String shopLocation;
String discountRate;
String discountDuration;
so your public void SubmitData(View view) should be like :
public void SubmitData(View view)
{
//All user input
EditText shopNameEditText = (EditText)findViewById(R.id.shopName);
EditText shopLocationEditText = (EditText)findViewById(R.id.shopLocation);
EditText shopCityEditText = (EditText)findViewById(R.id.shopCity);
EditText discountRateEditText = (EditText)findViewById(R.id.shopDiscount);
EditText discountDurationEditText = (EditText)findViewById(R.id.shopDiscountDuration);
if(shopNameEditText.getText().toString().length() == 0)
shopNameEditText.setError("يجب ادخال اسم المحل");
else if(shopLocationEditText.getText().toString().length() == 0)
shopLocationEditText.setError("يجب ادخال العنوان");
else if(shopCityEditText.getText().toString().length() == 0)
shopCityEditText.setError("يجب ادخال المدينة");
else if(discountRateEditText.getText().toString().length() == 0)
discountRateEditText.setError("يجب ادخال نسبة التخفيض");
else
{
shopName = shopNameEditText.getText().toString();
shopLocation = shopLocationEditText.getText().toString();
shopCity = shopCityEditText.getText().toString();
discountRate = discountRateEditText.getText().toString();
discountDuration = discountDurationEditText.getText().toString();
new PostDataAsyncTask().execute();
}
}
What I would recommend doing is using TextWatcher. If you do it this way, I believe these steps will help:
First, implement android.text.TextWatcher
Second, implement the necessary methods, most importantly, afterTextChanged(Editable)
Third, add textlisteners for your EditText's
For example...
EditText shopNameEditText = (EditText)findViewById(R.id.shopName);
shopNameEditText.addTextChangedListener(this);
#Override
public void afterTextChanged(Editable s) {
//check validation
if(shopNameEditText.getText().toString().length() == 0){
...
}
}
This is the expected behavior. Note that EditText extends the TextView class. And, the method that you are using: setError(CharSequence) is inherited by EditText from TextView.
Here is what it is designed to do:
Sets the right-hand compound drawable of the TextView to the "error"
icon and sets an error message that will be displayed in a popup when
the TextView has focus. The icon and error message will be reset to
null when any key events cause changes to the TextView's text. If the
error is null, the error message and icon will be cleared.
When the click is encountered, the EditText loses focus and waits until it regains focus to post the error.
To show the user that an error has occured, instead of calling setError(CharSequence), you can set warning text inside the EditText using myEditText.setText("Required"). You can also call requestFocus() on the EditText to show the error immediately after setError(CharSequence), but I am not sure how this would behave in case of 2 or more errors.

How to make Toast to display LogCat text?

I have an HttpPost which sends data to a server to be stored on a database. When that data is successfully stored I get a response in my LogCat that says "message has been saved successfully" (this response was defined in my PHP code). I am happy with that, but I am trying to get that same response to be displayed in a Toast. Here is my code:
String myBreadfromr, myBreadtor;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle myBasket = getIntent().getExtras();
myBreadfromr = myBasket.getString("keyfromcellr");
myBreadtor = myBasket.getString("keytocellr");
new SendData().execute("");
}
public class SendData extends AsyncTask<String, Integer, Void> {
protected void onPreExecute(String f) {
// called before doInBackground has started
f = "f";
}
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
// Create a new HTTP client
HttpClient client = new DefaultHttpClient();
// Create a new HTTP Post
HttpPost post = new HttpPost("http://192.xxx.xxx.xxx/androidp2p/process.php");
try {
// Add the data
List<NameValuePair> pairs = new ArrayList<NameValuePair>(3);
pairs.add(new BasicNameValuePair("from", myBreadfromr));
pairs.add(new BasicNameValuePair("to", myBreadtor));
pairs.add(new BasicNameValuePair("message", "What is your location?"));
// Encode Post data into valid URL format
post.setEntity(new UrlEncodedFormEntity(pairs));
// Go back to the first page
Intent back2start = new Intent(RequestLocation.this, StartApp.class);
startActivity(back2start);
// Make the HTTP Post Request
HttpResponse response = client.execute(post);
// Convert the response into a String
final HttpEntity resEntity = response.getEntity();
// Write the response to a log file
if (resEntity != null) {
Log.i("RESPONSE", EntityUtils.toString(resEntity));
}
runOnUiThread(new Runnable(){
public void run() {
Toast.makeText(RequestLocation.this, resEntity.toString(), Toast.LENGTH_LONG).show();
}
});
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
} catch (ClientProtocolException cpe) {
cpe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
// called when the background task has made any progress
}
protected void onPostExecute() {
// called after doInBackground has finished
}
}
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
What I see in the Toast instead is: "org.apache.http.conn.BasicManagedEntity#41284b48".
Thanking you in advance for any help in resolving this matter.
Use EntityUtils.toString(resEntity) in the Toast to get the same text.
Also no need to call runOnUiThread, doInBackground must return something, not null, and that something will be available onPostExecute which already is made to run on the UI thread.
AsyncTask

Fail to display server's respond

Here I am trying to retrieve the response from the server and display it, but I am failed to do so, the response text does not appear in my text view, insetead the default value of the string does, may I ask how can I achieve my goal. And why my code cannot finish the task.
Here is my android program:
public class Chico extends Activity {
GrabURL grab;
TextView mRespond;
String line;
#Override
public void onCreate(Bundle savedInstanceState) {
//create the activity
super.onCreate(savedInstanceState);
//set up the layout
setContentView(R.layout.activity_chico);
mRespond = (TextView) findViewById(R.id.Respond);
mRespond.setVisibility(View.GONE);
grab = new GrabURL();
line = "line";
//set up the button for check in
Button btnin = (Button) findViewById(R.id.inbutton);
btnin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//set the values
grab.onPreExecute("TechID", Build.SERIAL);
grab.onPreExecute("Type", "Checkin");
//set the destination and send the request
grab.execute(new String[]{"http://192.168.1.150/Me/testing.php"});
//close the activity
mRespond.setVisibility(View.VISIBLE);
mRespond.setText(line);
//finish();
}
});
}
private class GrabURL extends AsyncTask<String, Void, Void>{
//ArrayList object for storing the string pairs
ArrayList<NameValuePair> nameValuePairs;
public GrabURL() {
//constructor of the class
nameValuePairs = new ArrayList<NameValuePair>();
}
protected void onPreExecute(String key, String value) {
//store the pair of values into the ArrayList
nameValuePairs.add(new BasicNameValuePair(key,value));
}
#Override
protected Void doInBackground(String... urls) {
// TODO Auto-generated method stub
//Operation being executed in another thread
try{
//set up the type of HTTPClient
HttpClient client = new DefaultHttpClient();
//set up the location of the server
HttpPost post = new HttpPost(urls[0]);
//translate form of pairs to UrlEncodedFormEntity
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8);
//set up the entity being sent by post method
post.setEntity(ent);
//execute the url and post the values
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
line = EntityUtils.toString(resEntity);
} catch (Exception e) {
//catch the exception
line = "Can't connect to server";
}
return null;
}
protected void onPostExecute(Void unused) {
Toast.makeText(getApplicationContext(), "Value updated", Toast.LENGTH_SHORT).show();
}
}
}
And here is the php file, it just prints a line:
<?php
print "testing";
?>
Move this code to your AsyncTask's onPostExecute():
...
mRespond.setVisibility(View.VISIBLE);
mRespond.setText(line);

Categories

Resources