php - 无法使用 WAMP 将数据从 android Activity 插入 MySQL 到 php 数据库

标签 php android mysql wamp

我使用 PHP 在 MySQL 中创建了数据库。我有 registration.php 文件。从 Android 应用程序插入数据时会引发错误。

这是我的 registration.php:

<?php

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['fname']) && isset($_POST['lname']) && sisset($_POST['email']) && isset($_POST['phone']) && isset($_POST['password'])) 
    {

    $fname = $_GET['fname'];
    $lname = $_GET['lname'];
    $email = $_GET['email'];
    $phone = $_GET['phone'];
    $password = $_GET['password'];


    // include db connect class
    require_once __DIR__ . '/db_connect.php';
    //include("db_connect.php");
    // connecting to db
    $db = new DB_CONNECT();

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO user_master(fname, lname, email, phone, password,) VALUES ('$fname','$lname', '$email','$phone','$password',)");

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

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

?>

这是我的 Registration.java 文件:

public class Registartion extends Activity implements OnClickListener {

    private ProgressDialog pDialog;
    private JsonClass jsonParser;
    private EditText edt1,edt2,edt3,edt4,edt5,edt6;
    private Button btnReg;
    private List<NameValuePair> params = new ArrayList<NameValuePair>();

    // url to create new product
    private static String url_create_product = "http://10.0.2.2:80/MyAPI/registration.php";

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        setContentView(R.layout.activity_registartion);

        btnReg=(Button)findViewById(R.id.button1);
        edt1=(EditText)findViewById(R.id.editText1);
        edt2=(EditText)findViewById(R.id.editText2);
        edt3=(EditText)findViewById(R.id.editText3);
        edt4=(EditText)findViewById(R.id.editText4);
        edt5=(EditText)findViewById(R.id.editText5);
        edt6=(EditText)findViewById(R.id.editText6);

        btnReg.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if(v.equals(btnReg))
        {

            String firstname=edt1.getText().toString();
            String lastname=edt2.getText().toString();
            String email=edt3.getText().toString();
            String phone=edt4.getText().toString();
            String password=edt5.getText().toString();
            String confPassword=edt6.getText().toString();
            if(password.equals(confPassword))    
            {

                params.add(new BasicNameValuePair("fname", firstname));
                params.add(new BasicNameValuePair("lname", lastname));
                params.add(new BasicNameValuePair("email", email));
                params.add(new BasicNameValuePair("phone", phone));
                params.add(new BasicNameValuePair("password", confPassword));

                InsertDetials in=new InsertDetials();
                in.execute();
            }
            else
            {
                Toast.makeText(getApplicationContext(),
                        "Place re-Enter the password", Toast.LENGTH_LONG)
                        .show();
                edt5.setText("");
                edt6.setText("");

            }



        }
    }


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

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Registartion.this);
            pDialog.setMessage("Wait..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }



        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub


            jsonParser=new JsonClass();

            JSONObject json = jsonParser.makeHttpRequest(url_create_product,"POST", params);

            return null;    
            }

        protected void onPostExecute(String file_url) {
                // dismiss the dialog once done
                pDialog.dismiss();
                super.onPostExecute(file_url);
            }

        }
}

这是我的 JsonClass.java 文件:

public class JsonClass {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JsonClass() {

    }

    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST") {
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            } else if(method == "GET") {
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();

            Log.i("Message Here:", "["+json+"]");

        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {

            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

还有日志猫:

04-15 11:16:36.420: D/dalvikvm(1638): GC_FOR_ALLOC freed 62K, 5% free 2732K/2868K, paused 77ms, total 79ms
04-15 11:16:36.750: I/dalvikvm-heap(1638): Grow heap (frag case) to 3.777MB for 1093136-byte allocation
04-15 11:16:36.960: D/dalvikvm(1638): GC_FOR_ALLOC freed <1K, 4% free 3799K/3936K, paused 209ms, total 209ms
04-15 11:16:37.640: I/Choreographer(1638): Skipped 54 frames!  The application may be doing too much work on its main thread.
04-15 11:16:37.880: D/gralloc_goldfish(1638): Emulator without GPU emulation detected.
04-15 11:16:42.060: I/Choreographer(1638): Skipped 50 frames!  The application may be doing too much work on its main thread.
04-15 11:16:44.230: I/Choreographer(1638): Skipped 75 frames!  The application may be doing too much work on its main thread.
04-15 11:16:45.550: I/Choreographer(1638): Skipped 798 frames!  The application may be doing too much work on its main thread.
04-15 11:16:46.870: I/Choreographer(1638): Skipped 442 frames!  The application may be doing too much work on its main thread.
04-15 11:16:55.000: D/InputEventConsistencyVerifier(1638): KeyEvent: ACTION_UP but key was not down.
04-15 11:16:55.000: D/InputEventConsistencyVerifier(1638):   in android.widget.EditText{b2f162b0 VFED..CL .F....I. 0,80-292,128 #7f0a0007 app:id/editText2}
04-15 11:16:55.000: D/InputEventConsistencyVerifier(1638):   0: sent at 11751115000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=11751115, downTime=11750965, deviceId=0, source=0x101 }
04-15 11:16:55.770: D/dalvikvm(1638): GC_FOR_ALLOC freed 61K, 3% free 4252K/4384K, paused 37ms, total 39ms
04-15 11:16:55.990: D/InputEventConsistencyVerifier(1638): KeyEvent: ACTION_UP but key was not down.
04-15 11:16:55.990: D/InputEventConsistencyVerifier(1638):   in android.widget.EditText{b2f17840 VFED..CL .F....I. 0,144-292,192 #7f0a0009 app:id/editText3}
04-15 11:16:55.990: D/InputEventConsistencyVerifier(1638):   0: sent at 11752132000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=11752132, downTime=11751941, deviceId=0, source=0x101 }
04-15 11:16:56.940: D/InputEventConsistencyVerifier(1638): KeyEvent: ACTION_UP but key was not down.
04-15 11:16:56.940: D/InputEventConsistencyVerifier(1638):   in android.widget.EditText{b2f18dc8 VFED..CL .F....I. 0,208-292,256 #7f0a0010 app:id/editText4}
04-15 11:16:56.940: D/InputEventConsistencyVerifier(1638):   0: sent at 11753078000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=11753078, downTime=11752927, deviceId=0, source=0x101 }
04-15 11:16:58.450: I/Choreographer(1638): Skipped 79 frames!  The application may be doing too much work on its main thread.
04-15 11:16:59.870: I/Choreographer(1638): Skipped 37 frames!  The application may be doing too much work on its main thread.
04-15 11:17:00.010: D/InputEventConsistencyVerifier(1638): KeyEvent: ACTION_UP but key was not down.
04-15 11:17:00.010: D/InputEventConsistencyVerifier(1638):   in android.widget.EditText{b2f1a3b8 VFED..CL .F....I. 0,272-292,320 #7f0a0011 app:id/editText5}
04-15 11:17:00.010: D/InputEventConsistencyVerifier(1638):   0: sent at 11756204000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=11756204, downTime=11756049, deviceId=0, source=0x101 }
04-15 11:17:03.180: D/InputEventConsistencyVerifier(1638): KeyEvent: ACTION_UP but key was not down.
04-15 11:17:03.180: D/InputEventConsistencyVerifier(1638):   in android.widget.EditText{b2f1c308 VFED..CL .F....I. 0,336-292,384 #7f0a0012 app:id/editText6}
04-15 11:17:03.180: D/InputEventConsistencyVerifier(1638):   0: sent at 11759186000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=11759186, downTime=11759101, deviceId=0, source=0x101 }
04-15 11:17:04.160: I/Choreographer(1638): Skipped 50 frames!  The application may be doing too much work on its main thread.
04-15 11:17:10.050: D/InputEventConsistencyVerifier(1638): KeyEvent: ACTION_UP but key was not down.
04-15 11:17:10.050: D/InputEventConsistencyVerifier(1638):   in android.widget.Button{b2f1de40 VFED..C. .F....I. 94,400-194,450 #7f0a0005 app:id/button1}
04-15 11:17:10.050: D/InputEventConsistencyVerifier(1638):   0: sent at 11766215000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=11766215, downTime=11766098, deviceId=0, source=0x101 }
04-15 11:17:12.910: I/Choreographer(1638): Skipped 107 frames!  The application may be doing too much work on its main thread.
04-15 11:17:13.160: I/Choreographer(1638): Skipped 62 frames!  The application may be doing too much work on its main thread.
04-15 11:17:13.770: I/Choreographer(1638): Skipped 50 frames!  The application may be doing too much work on its main thread.
04-15 11:17:15.200: I/Choreographer(1638): Skipped 30 frames!  The application may be doing too much work on its main thread.
04-15 11:17:16.120: I/Choreographer(1638): Skipped 34 frames!  The application may be doing too much work on its main thread.
04-15 11:17:16.270: I/Choreographer(1638): Skipped 34 frames!  The application may be doing too much work on its main thread.
04-15 11:17:17.320: I/Choreographer(1638): Skipped 34 frames!  The application may be doing too much work on its main thread.
04-15 11:17:17.750: I/Choreographer(1638): Skipped 39 frames!  The application may be doing too much work on its main thread.
04-15 11:17:18.390: D/dalvikvm(1638): GC_FOR_ALLOC freed 221K, 7% free 4541K/4836K, paused 42ms, total 43ms
04-15 11:17:19.270: I/Choreographer(1638): Skipped 50 frames!  The application may be doing too much work on its main thread.
04-15 11:17:19.500: I/Message Here:(1638): [<br />
04-15 11:17:19.500: I/Message Here:(1638): <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
04-15 11:17:19.500: I/Message Here:(1638): <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: fname in C:\wamp\www\MyAPI\registration.php on line <i>15</i></th></tr>
04-15 11:17:19.500: I/Message Here:(1638): <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
04-15 11:17:19.500: I/Message Here:(1638): <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
04-15 11:17:19.500: I/Message Here:(1638): <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.2613</td><td bgcolor='#eeeeec' align='right'>246304</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\MyAPI\registration.php' bgcolor='#eeeeec'>..\registration.php<b>:</b>0</td></tr>
04-15 11:17:19.500: I/Message Here:(1638): </table></font>
04-15 11:17:19.500: I/Message Here:(1638): <br />
04-15 11:17:19.500: I/Message Here:(1638): <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
04-15 11:17:19.500: I/Message Here:(1638): <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: lname in C:\wamp\www\MyAPI\registration.php on line <i>16</i></th></tr>
04-15 11:17:19.500: I/Message Here:(1638): <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
04-15 11:17:19.500: I/Message Here:(1638): <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
04-15 11:17:19.500: I/Message Here:(1638): <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.2613</td><td bgcolor='#eeeeec' align='right'>246304</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\MyAPI\registration.php' bgcolor='#eeeeec'>..\registration.php<b>:</b>0</td></tr>
04-15 11:17:19.500: I/Message Here:(1638): </table></font>
04-15 11:17:19.500: I/Message Here:(1638): <br />
04-15 11:17:19.500: I/Message Here:(1638): <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
04-15 11:17:19.500: I/Message Here:(1638): <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: email in C:\wamp\www\MyAPI\registration.php on line <i>17</i></th></tr>
04-15 11:17:19.500: I/Message Here:(1638): <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
04-15 11:17:19.500: I/Message Here:(1638): <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
04-15 11:17:19.500: I/Message Here:(1638): <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.2613</td><td bgcolor='#eeeeec' align='right'>246304</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\MyAPI\registration.php' bgcolor='#eeeeec'>..\registration.php<b>:</b>0</td></tr>
04-15 11:17:19.500: I/Message Here:(1638): </table></font>
04-15 11:17:19.500: I/Message Here:(1638): <br />
04-15 11:17:19.500: I/Message Here:(1638): <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
04-15 11:17:19.500: I/Message Here:(1638): <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: phone in C:\wamp\www\MyAPI\registration.php on line <i>18</i></th></tr>
04-15 11:17:19.500: I/Message Here:(1638): <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
04-15 11:17:19.500: I/Message Here:(1638): <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
04-15 11:17:19.500: I/Message Here:(1638): <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.2613</td><td bgcolor='#eeeeec' align='right'>246304</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\MyAPI\registration.php' bgcolor='#eeeeec'>..\registration.php<b>:</b>0</td></tr>
04-15 11:17:19.500: I/Message Here:(1638): </table></font>
04-15 11:17:19.500: I/Message Here:(1638): <br />
04-15 11:17:19.500: I/Message Here:(1638): <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
04-15 11:17:19.500: I/Message Here:(1638): <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color:
04-15 11:17:19.630: I/Choreographer(1638): Skipped 55 frames!  The application may be doing too much work on its main thread.
04-15 11:17:19.710: E/JSON Parser(1638): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
04-15 11:17:20.040: I/Choreographer(1638): Skipped 105 frames!  The application may be doing too much work on its main thread.

最佳答案

04-15 11:17:19.710: E/JSON Parser(1638): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

您没有正确构建 JSON 结构。您确实需要使用像 Gson 这样的 JSON 库来将您的数据序列化/反序列化为 JSON。

关于php - 无法使用 WAMP 将数据从 android Activity 插入 MySQL 到 php 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29654462/

相关文章:

php - 在 PHP 中获取 cURL 流

java - ORMLite 数据库更新

java - Android - 图像至上

python - 使用 python 将数据从 MySQL 迁移到 SAP HANA

mysql - 如何设置mysql查询的最大执行时间?

php - 使用 preg_match 查找字符串是否包含脚本标签

javascript - AJAX POST 不起作用

php - MySQL:从表中选择在另一个表的字段中指定名称的行

android - ImageViews 在多个屏幕中的准确位置

mysql - 在类似mysql的查询中包含点运算符