I am new to android development and I am trying to make an android app for a class project that lets the user order food. Once they hit the checkout button, there is a String value containing the user's order information(i.e. -Chicken Sandwich, tomatoes, lettuce, mayo, special instructions, total: $5.99) that is passed to the next activity. However, I also want this information to be sent from the app to a website, so that the person viewing the webpage can see the order information. What is the best way to go about this?
I sort of figured out how to handle user login/registration with MySQL and phpmyadmin, but I'm not sure if doing that again for this specific string is the best way to display the food order. I'm not looking to create a database specifically for one String. Instead I just want to post that order information on a webpage so that a "staff member" can view the order information and start working on the food order.
I found some info from this post: How to send a data to a web server from Android
Here is what I tried so far based on what I found from that post : Orderlist activity:
@Override
public void onClick(View v) {
Intent myIntent = null;
switch (v.getId()) {
case R.id.checkout:
if (MainActivity.amountBalance - orderPrice < 0) {
Toast.makeText(Orderlist.this, "Insufficient balance!", Toast.LENGTH_SHORT).show();
} else {
MainActivity.amountBalance = MainActivity.amountBalance - orderPrice;
myIntent = new Intent(Orderlist.this, OrderPlaced.class);
Toast.makeText(getApplicationContext(),"Your order was placed!",Toast.LENGTH_LONG).show();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mywebsitename.com/mypage.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("orderinfo", "Chicken sandwich: tomatoes, onions, cheese.\nTotal: $5.99."));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
startActivity(i);
}
break;
case R.id.cancel:
i = new Intent(Orderlist.this, MainActivity.class);
Toast.makeText(Orderlist.this, "All items have been removed from cart.", Toast.LENGTH_LONG).show();
startActivity(i);
break;
}
}
mypage.php
<?php
$filename="datatest.html";
file_put_contents($filename,$_POST["orderinfo"]."<br />",FILE_APPEND);
$msg=file_get_contents($filename);
echo $msg; ?>
Unfortunately this causes my code to crash and doesn't post anything to mypage.php. How do I fix this?
Error:
FATAL EXCEPTION: main
Process: com.example.kusha.finalproject, PID: 5291
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
at java.net.InetAddress.getAllByName(InetAddress.java:752)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:366)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
at com.example.kusha.finalproject.Orderlist.onClick(Orderlist.java:192)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
-- And the error appears to be coming from Orderlist.java:192 which is this piece of code here:
nameValuePairs.add(new BasicNameValuePair("orderinfo", "Chicken sandwich:
tomatoes, onions, cheese.\nTotal: $5.99."));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost); //ERROR FROM HERE
Thank You!