如何从存储在服务器上的表中检索列值? [英] How do I retrieve a columns value from a table which is stored on a server?

查看:47
本文介绍了如何从存储在服务器上的表中检索列值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序。我已经在webhost上免费注册了我的域名。我的域名是www.xyz.site在webhost上,在MySQL中我有一个数据库,其中有一个名为users的表,其中包含一个名为amount的列。在我的Android应用程序的一个活动Payment1Activity.java中,我想从服务器获取该值并从中扣除一些特定的数量,这是我的一个xml文件perryroad.xml中的textview并保存新的金额到表用户的金额列。

我的活动Payment1Activity.java如下:

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 :

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();
        }
    });
   }

    }







我的perryroad.xml文件如下:




My perryroad.xml file is as follow :

<?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>





我想从这个textview4到getText()访问20,那么它的语法是什么?

有谁可以帮我这个?任何建议或建议将受到高度赞赏。谢谢。



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.

推荐答案

查看这些资源



http://stackoverflow.com/questions/8784122/how-to-access-external-mysql-database- in-android [ ^ ]



http://stackoverflow.com/questions/11324939/how-to-access-online-mysql-database-in-android [ ^ ]



http:// www .basic4ppc.com / Android /论坛/线程/康恩ect-android-to-mysql-database-tutorial.8339 / [ ^ ]



http://www.tutorialspoint.com/android/android_php_mysql.htm [ ^ ]



还有更多来自google ... https://www.google.co.uk/ ?gfe_rd = cr& ei = FkMvU8aKB4vR8ge-6YDYDA #q = access + database + on + website + android + mysql [ ^ ]
Have a look at these resources

http://stackoverflow.com/questions/8784122/how-to-access-external-mysql-database-in-android[^]

http://stackoverflow.com/questions/11324939/how-to-access-online-mysql-database-in-android[^]

http://www.basic4ppc.com/android/forum/threads/connect-android-to-mysql-database-tutorial.8339/[^]

http://www.tutorialspoint.com/android/android_php_mysql.htm[^]

and many many more from google ... https://www.google.co.uk/?gfe_rd=cr&ei=FkMvU8aKB4vR8ge-6YDYDA#q=access+database+on+website+android+mysql[^]


这篇关于如何从存储在服务器上的表中检索列值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆