I want to add data from a json string on android to a MySQL table. I am using a PHP API for this.
This is my android code, and I'm certain that the string is passed to the PHP file, and the in the MySQL table, it inserts empty rows only. What am I doing wrong?
public class Donate extends Activity{
private static final String TAG = "paymentExample";
/**
* - Set to PaymentActivity.ENVIRONMENT_PRODUCTION to move real money.
*
* - Set to PaymentActivity.ENVIRONMENT_SANDBOX to use your test credentials
* from https://developer.paypal.com
*
* - Set to PayPalConfiguration.ENVIRONMENT_NO_NETWORK to kick the tires
* without communicating to PayPal's servers.
*/
//private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_NO_NETWORK;
private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX;
// note that these credentials will differ between live & sandbox environments.
private static final String CONFIG_CLIENT_ID = "AT0wBHbotSFAajdTsYi70iUEznfeMD6iouo3CQ1RoxpiTKNyjsQ4QOUmMczO9y8VeMrW4nbfs_1OpXCu";
private static final int REQUEST_CODE_PAYMENT = 1;
private static final int REQUEST_CODE_FUTURE_PAYMENT = 2;
private static PayPalConfiguration config = new PayPalConfiguration()
.environment(CONFIG_ENVIRONMENT)
.clientId(CONFIG_CLIENT_ID)
// The following are only used in PayPalFuturePaymentActivity.
.merchantName("Hipster Store")
.merchantPrivacyPolicyUri(Uri.parse("https://www.example.com/privacy"))
.merchantUserAgreementUri(Uri.parse("https://www.example.com/legal"));
PayPalPayment thingToBuy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.donate);
Intent intent = new Intent(this, PayPalService.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
startService(intent);
}
public void onBuyPressed(View pressed) {
// PAYMENT_INTENT_SALE will cause the payment to complete immediately.
// Change PAYMENT_INTENT_SALE to PAYMENT_INTENT_AUTHORIZE to only authorize payment and
// capture funds later.
EditText etdonate = (EditText) findViewById(R.id.etDonate);
//Button donate = (Button) findViewById(R.id.btnDonate);
BigDecimal amount;
amount = new BigDecimal (etdonate.getText().toString());
//thingToBuy = new PayPalPayment(amount,"MYR", "Test", PayPalPayment.PAYMENT_INTENT_SALE);
Intent get = getIntent();
String eid = get.getStringExtra("eid");
String ename = get.getStringExtra("ename");
if(pressed.getId() == R.id.btnDonate2){
thingToBuy = new PayPalPayment(amount, "MYR", "Donation for " + ename, PayPalPayment.PAYMENT_INTENT_SALE);
}
Intent intent = new Intent(Donate.this, PaymentActivity.class);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
startActivityForResult(intent, REQUEST_CODE_PAYMENT);
}
public void onFuturePaymentPressed(View pressed) {
Intent intent = new Intent(Donate.this, PayPalFuturePaymentActivity.class);
startActivityForResult(intent, REQUEST_CODE_FUTURE_PAYMENT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PAYMENT) {
if (resultCode == Activity.RESULT_OK) {
PaymentConfirmation confirm =
data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null) {
Log.i(TAG, confirm.toJSONObject().toString());
Log.i(TAG, confirm.getPayment().toJSONObject().toString());
Log.i(TAG, confirm.getProofOfPayment().toJSONObject().toString());
String js = confirm.getProofOfPayment().toJSONObject().toString();
new sendData().execute(js);
Toast.makeText(
getApplicationContext(),
"PaymentConfirmation info received from PayPal", Toast.LENGTH_LONG)
.show();
Intent conf = new Intent(getApplicationContext(), PayConfirm.class);
startActivity(conf);
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i(TAG, "The user canceled.");
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
Log.i(TAG,"An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
}
}
}
private void sendAuthorizationToServer(PayPalAuthorization authorization) {
}
//from here
public class sendData extends AsyncTask<String, String, Void> {
@Override
protected Void doInBackground(String... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/shareity/paypal.php/");
try {
String js = params[0];
HttpEntity httpEntity = new StringEntity(js);
httpPost.setEntity(httpEntity);
httpClient.execute(httpPost);
//String t = httpPost.toString();
Log.i(TAG, "Testiing");
Log.i(TAG, js);
}
catch (IOException e) {
}
return null;
}
}
@Override
public void onDestroy() {
// Stop service when done
stopService(new Intent(this, PayPalService.class));
super.onDestroy();
}
}
I'm assuming the problem is somewhere here, in the PHP code:
<?php
//Create Database connection
//$file = 'paypaltext.txt';
$db = mysql_connect("localhost","root","");
if (!$db) {
die('Could not connect to db: ' . mysql_error());
}
//Select the Database
mysql_select_db("shareity",$db) or die('Could not select DB' . mysqli_connect_error());
$json = file_get_contents("php://input");
$data = json_decode($json);
$create_time = $data[0]->create_time;
$id = $data[0]->id;
$intent = $data[0]->intent;
$state = $data[0]->state;
//$con = mysql_connect("","","") or die(mysqli_connect_error());
//$db = mysql_select_db("shareity",$db) or die(mysqli_connect_error());
//file_put_contents ( string $file , $data);
mysql_query("INSERT INTO donations(create_time,id,intent,state)VALUES('$create_time','$id','$intent','$state')");
?>
0 comments:
Post a Comment