【发布时间】:2014-03-23 20:15:39
【问题描述】:
我有一个安卓应用程序。我在虚拟主机上免费注册了我的域名。我的域名是“www.xyz.site” 在虚拟主机上,在 MySQL 中,我有一个数据库,其中有一个名为“users”的表,其中包含一个名为“amount”的列。在我的 android 应用程序的一项活动“Payment1Activity.java”中,我想从服务器获取该值并从中扣除一些特定金额,这是我的一个 xml 文件“perryroad.xml”中的一个文本视图,并将新金额保存回来到“用户”表的“金额”列。 我的活动 Payment1Activity.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();
}
});
}
}
我的 perryroad.xml 文件如下:
<?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:background="@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>
我想通过 getText() 从这个 textview4 访问“20”,那么它的语法是什么? 谁能帮我这个 ?任何建议或意见将不胜感激。谢谢。
【问题讨论】:
标签: java android mysql database