试图将值发送到我的数据库它不起作用?

时间:2013-11-17 11:30:18

标签: android

我正在尝试将一些值发送到我的数据库,但它不起作用。当我单击提交按钮时,它显示一个进度对话框,最后显示相同的页面。我已经为android 2.3.3编写了它 java代码是::

package donor.android.blooddonor;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class RegistrationActivity extends Activity{
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText  et1;
EditText  et2;
EditText  et3;
EditText  et4;
EditText  et5;
EditText  et6;

// url to create new product
private static String url_create_donor = "http://192.168.28.13/pro_connect/create_donor.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registration);


        // Edit Text
        et1 = (EditText) findViewById (R.id.et1);
        et2 = (EditText) findViewById (R.id.et2);
        et3 = (EditText) findViewById (R.id.et3);
        et4 = (EditText) findViewById (R.id.et4);
        et5 = (EditText) findViewById (R.id.et5);
        et6 = (EditText) findViewById (R.id.et6);
        // Create button
        Button b3 = (Button) findViewById (R.id.b3);




        // button click event
        b3.setOnClickListener(new View.OnClickListener()
        {
       public void onClick(View view) {
                // creating new donor in background thread
                new CreateDonor1().execute();
            }
        });
    }

     class CreateDonor1 extends AsyncTask<String, String, String> {

        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(RegistrationActivity.this);
            pDialog.setMessage("Creating DONOR..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        protected String doInBackground(String... args) {
            String NAME = et1.getText().toString();
            String ADDRESS = et2.getText().toString();
            String PHONENUMBER = et3.getText().toString();
            String EMAIL = et4.getText().toString();
            String CITY = et5.getText().toString();
            String BLOODGROUP = et6.getText().toString();
            // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("NAME", NAME));
            params.add(new BasicNameValuePair("ADDRESS", ADDRESS));
        params.add(new BasicNameValuePair("PHONENUMBER", PHONENUMBER));
            params.add(new BasicNameValuePair("EMAIL", EMAIL));
            params.add(new BasicNameValuePair("CITY", CITY));
        params.add(new BasicNameValuePair("BLOODGROUP", BLOODGROUP));
            // getting JSON Object
            // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_create_donor,
                    "POST", params);


            Log.d("Create Response", json.toString());

            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // successfully created donor
Intent i = new Intent(getApplicationContext(), SearchDirectly1Activity.class);
                    startActivity(i);

                    // closing this screen
                    finish();
                } else {
                    // failed to create product
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        }

    }
}

和xml代码是::

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/t2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="@string/name" />

<EditText
    android:id="@+id/et1"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:layout_alignBaseline="@+id/t2"
    android:layout_alignBottom="@+id/t2"
    android:layout_alignParentRight="true"
    android:hint="@string/type" />

<TextView
    android:id="@+id/t3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="80dp"
    android:text="@string/address" />

<EditText
    android:id="@+id/et2"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:layout_alignBaseline="@+id/t3"
    android:layout_alignBottom="@+id/t3"
    android:layout_alignParentRight="true"
    android:hint="@string/type" />

<TextView
    android:id="@+id/t4"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="140dp"
    android:text="@string/number" />

<EditText
    android:id="@+id/et3"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:layout_alignBaseline="@+id/t4"
    android:layout_alignBottom="@+id/t4"
    android:layout_alignParentRight="true"
    android:hint="@string/type" />

<TextView
    android:id="@+id/t5"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="200dp"
    android:text="@string/email" />

<EditText
    android:id="@+id/et4"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:layout_alignBaseline="@+id/t5"
    android:layout_alignBottom="@+id/t5"
    android:layout_alignParentRight="true"
    android:hint="@string/type" />

<EditText
    android:id="@+id/et5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/et4"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/et4"
    android:layout_marginTop="26dp"
    android:hint="@string/type" />

<TextView
    android:id="@+id/t6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/et5"
    android:layout_alignBottom="@+id/et5"
    android:layout_alignParentLeft="true"
    android:text="@string/city" />

<TextView
    android:id="@+id/t7"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/et5"
    android:layout_marginTop="25dp"
    android:text="@string/blood_group" />

<EditText
    android:id="@+id/et6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/t7"
    android:layout_alignBottom="@+id/t7"
    android:layout_alignLeft="@+id/et5"
    android:layout_alignParentRight="true"
    android:hint="@string/type" />

<Button
    android:id="@+id/b3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/et6"
    android:layout_marginLeft="75dp"
    android:layout_marginTop="28dp"
    android:layout_toRightOf="@+id/t6"
    android:text="@string/submitt" />

</RelativeLayout>

这是我的php文件:

<?php

/ *  *以下代码将创建一个新的产品行  *所有产品详细信息均来自HTTP Post Request  * /

//用于JSON响应的数组 $ response = array();

//检查必填字段 if(isset($ _ POST ['NAME'])&amp;&amp; isset($ _ POST ['ADDRESS'])&amp;&amp; isset($ _ POST ['PHONENEUMBER'])&amp;&amp; isset($ _ POST [' EMAIL'])&amp;&amp; isset($ _ POST ['CITY'])&amp;&amp; isset($ _ POST ['BLOODGROUP'])){

$name = $_POST['NAME'];
$address = $_POST['ADDRESS'];
$phonenumber = $_POST['PHONENUMBER'];
$email = $_POST['EMAIL'];
$city = $_POST['CITY'];
$bloodgroup = $_POST['BLOODGROUP'];

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// mysql inserting a new row
$result = mysql_query("INSERT INTO data5(id,name, bloodgroup, address, email, mobile, city) VALUES(0,'$name', '$bloodgroup', '$address', '$email', '$phonenumber', '$city')");

// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "successfully registered.";

    // echoing JSON response
    echo json_encode($response);
} else {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "Oops! An error occurred.";

    // echoing JSON response
    echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);

}    ?&GT;

1 个答案:

答案 0 :(得分:0)

检查您的服务器是否返回了某些内容(TAG_SUCCESS值),如果是这种情况,您的php文件可能存在问题,或者您发送了错误的参数。