I'm trying to send some data to my website. When the button is clicked, data need to be sent to the web site
but I got some errors when I am running the program
when I clicked the button this message appears "unfortunately app has stopped" then it exit my application.
public class testInput extends Activity {
Button Setbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test_input_page);
Setbutton=(Button)findViewById(R.id.setbtn);
Setbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
testInput ti=new testInput();
//Create the intent
ti.postData("Sent Data");
});
}
public void postData(String toPost) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mysite.com/index.php");
//This is the data to send
String MyName = toPost; //any data to send
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("action", MyName));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httppost, responseHandler);
//This is the response from a php application
String reverseString = response;
Toast.makeText(this, "response" + reverseString, Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
} catch (IOException e) {
Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
}
}//end postData()
This is my PHP code.
<?php
//code to reverse the string
$reversed = strrev($_POST["action"]);
echo $reversed;
?>
I got permission also for use internet in my app.
Android uses the action ACTION_SEND to send data from one activity to another, even across process boundaries. You need to specify the data and its type. The system automatically identifies the compatible activities that can receive the data and displays them to the user.
Use AsyncTask in android and access the url of your JSON to do the POST or GET with your data, In the web, simply do the same, write a page that reads the json data that you return from the page you first created. Android Example that reads data from a json file: private void parseJsonFeed(JSONObject response) {
Stay organized with collections Save and categorize content based on your preferences. Just as an app can send data to other apps, it can also receive data from other apps as well. Think about how users interact with your application and what data types you want to receive from other applications.
Since Android 3.x you can't perform network operations in main thread. You need to use AsyncTask or separate thread where you can call your postData method.
http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
Use the following code
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://yoursite.com/index.php");
String MyName = toPost;
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("action", MyName));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
InputStream is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
result = br.readLine();
//This is the response from a php application
String reverseString = result;
Toast.makeText(this, "result" + reverseString, Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
Toast.makeText(this, ""+e, Toast.LENGTH_LONG).show();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With