Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have an android application. I have registered my domain for free on webhost. My domain name is "www.xyz.site" On webhost,in MySQL I have a database which has a table called "users", which contain a column called "amount". In one of my activity "Payment1Activity.java" of my android application I want to take that value from server and deduct some particular amount from it which is a textview in one of my xml file "perryroad.xml" and save the new amount back to my "amount" column of table "Users".
My activity Payment1Activity.java is as follow :
Java
package com.example.streetsystemparking;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RemoteViews;
//import com.example.streetsystemparking.MainActivity;


public class Payment1Activity extends Activity {
Button b,br1;
EditText et,pass;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
String status;
String uid;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_payment1);
    b = (Button)findViewById(R.id.Button01);
    et = (EditText)findViewById(R.id.accountno);
    pass= (EditText)findViewById(R.id.password);
   // tv = (TextView)findViewById(R.id.tv);

    b.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View v) {

            dialog = ProgressDialog.show(Payment1Activity.this, "",
                    "Validating user...", true);
             new Thread(new Runnable() {
                    public void run() {
                        payment();
                    }
                  }).start();
        }
    });
 }

void payment(){
try{

httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://tanushreedutta.site40.net/payment_new/check.php");
        //add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("accno",et.getText().toString().trim()));
    nameValuePairs.add(new BasicNameValuePair("bpassword",
                                 pass.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        //Execute HTTP Post Request
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
        //System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
                //tv.setText("Response from PHP : " + response);
dialog.dismiss();
            }
        });
if(response.startsWith("User Found")){
            new Thread(new Runnable() {
                public void run() {

                    update();
                }

              }).start();

    runOnUiThread(new Runnable() {
    public void run() {
        //Here I want to take that amount from mysql table and subtract the textview
          (taken from perryroad.xml) value from it and store that new value back to
          mysql table.
        Toast.makeText(Payment1Activity.this,"Payment Successful for block 1",
                      Toast.LENGTH_SHORT).show();
Toast.makeText(Payment1Activity.this,"You reserved block
                          1",Toast.LENGTH_LONG).show();

                    setResult(1);
                }
            });
startActivity(new Intent (Payment1Activity.this,VacatingCredentials.class));


        }else{
            showAlert();
        }

    }catch(Exception e){
        dialog.dismiss();
        System.out.println("Exception : " + e.getMessage());
    }
}
 public void update()
    {
        try
        {
        uid="1";
    status="2";
    HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://tanushreedutta.site40.net/map/map.php");
            nameValuePairs = new ArrayList<NameValuePair>(2);

            nameValuePairs.add(new BasicNameValuePair("uid",uid));
            nameValuePairs.add(new BasicNameValuePair("status",status));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            response = httpclient.execute(httppost);
           ResponseHandler<String> responseHandler = new BasicResponseHandler();
    final String response = httpclient.execute(httppost, responseHandler);
           // HttpEntity entity = response.getEntity();
           // is = entity.getContent();
            Log.e("pass 1", "connection success ");
        }
        catch(Exception e)
        {
            Log.e("Fail 1", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP Address",
            Toast.LENGTH_LONG).show();
        }
    }
public void showAlert(){
    Payment1Activity.this.runOnUiThread(new Runnable() {
        public void run() {
     AlertDialog.Builder builder = new AlertDialog.Builder(Payment1Activity.this);
       builder.setTitle("Payment Error.");
       builder.setMessage("User not Found.")
       .setCancelable(false)
       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
           Toast.makeText(Payment1Activity.this,"Invalid Account number or Password,Try
                         Again",Toast.LENGTH_LONG).show();
                            }
                   });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });
   }

    }



My perryroad.xml file is as follow :
Java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:removed="@drawable/p5" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="40dp"
    android:text="Address: Perry Road, Bandra"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="103dp"
    android:orientation="horizontal"
    android:text="West, Mumbai"
    android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:layout_marginTop="25dp"
    android:layout_marginLeft="10dp"
    android:orientation="horizontal"
    android:text="Parking charges: Rs."
    android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
     android:id="@+id/textView4"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="220dp"
     android:orientation="horizontal"
     android:text="20"
     android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/b1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_gravity="center"
    android:orientation="horizontal"
    android:layout_marginTop="50dp"
    android:text="Get Direction"
    android:textColor="#000000"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/b2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:text="Get Map"
    android:layout_gravity="center"
     android:textColor="#000000"
    android:textAppearance="?android:attr/textAppearanceLarge" />

  </LinearLayout>


I want to access "20" from this textview4 through getText() so what is the syntax for that ?
Can anyone help me with this ? Any suggestions or advices will be highly appreciated. Thank you.
Posted
Updated 23-Mar-14 12:01pm
v2

1 solution

 
Share this answer
 
Comments
MJR4PJCT 23-Mar-14 18:02pm    
Thank you for your quick response. I have one doubt that I have used kind of client side coding as you have described but that was for comparing account no and password which is entered by user with the one stored in database but here I want to only access only one field "amount" from table "users". So now what will be the changes in my above code ?
Member 13168378 3-May-17 5:17am    
hi sir, i am doing exactly what u are did.. i need to detect some values from android device which the amount is store in mysql database.. can u please help me
CHill60 3-May-17 5:31am    
See the links above. If you are still having problems then it is better to post your own question with your own code. And explain the problem clearly

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900