无法使用php将数据从android插入mysql

时间:2014-03-22 08:33:08

标签: java php android mysql

我试图使用php将数据从android插入mysql。没有错误,但数据没有插入到我的mysql表中,它显示"插入数据失败!"。

这是我的java课程

package com.ta.nutrifact;



import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;


import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class DaftarAkun extends Activity {
    TextView tekserror;
    private EditText txtNama, txtPass2, txtPass, txtBerat, txtTinggi, txtAF, txtUsia, txtGula, txtTekanan, txtKolestrol, txtAsamUrat, txtJK ;
    EditText email;
    private String url = "http://10.0.2.2/TA/adduser.php";

    protected void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);
        setContentView(R.layout.daftar_akun);
        tekserror = (TextView) findViewById(R.id.showError);
        txtPass = (EditText) findViewById(R.id.inputPassword);
        txtPass2 = (EditText) findViewById(R.id.inputPasswordAgain);
        txtPass2.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                if (!s.toString().equals(txtPass.getText().toString()) ) {
                    tekserror.setVisibility(View.VISIBLE);
                    tekserror.setText("Password Tidak Cocok");
                } else {
                    tekserror.setVisibility(View.INVISIBLE);
                }

            }
        });


        txtNama = (EditText) findViewById(R.id.inputUsername);
        txtTinggi = (EditText) findViewById(R.id.inputTinggi);
        txtUsia = (EditText) findViewById(R.id.inputUsia);


        Button btnSimpan = (Button) findViewById(R.id.btnSimpan);
        // daftarkan even onClick pada btnSimpan
            btnSimpan.setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View v) {

                    try {
                        // setiap parameter yang akan dikirim melalui http
                    // harus encode agar
                        // dapat terbaca dengan baik oleh server
                        String nama = URLEncoder.encode(txtNama.getText()
                                .toString(), "utf-8");
                        String pass = URLEncoder.encode(txtPass.getText()
                               .toString(), "utf-8");
                        String tinggi = URLEncoder.encode(txtTinggi.getText()
                                .toString(), "utf-8");
                        String usia = URLEncoder.encode(txtUsia.getText()
                               .toString(), "utf-8");

                        url += "?nama=" + nama + "&pass=" + pass + "&tinggi=" + tinggi + "&usia=" + usia ;
                        getRequest(url);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

            }
            });

    }
    public void getRequest(String Url) {
        Toast.makeText(this, "Insert Data " + Url + " ", Toast.LENGTH_SHORT)
                .show();
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        try {
            HttpResponse response = client.execute(request);
            Toast.makeText(this, "Insert Data" + request(response) + " ",
                    Toast.LENGTH_SHORT).show();
        } catch (Exception ex) {
            Toast.makeText(this, "Insert Data Failed!", Toast.LENGTH_SHORT)
                    .show();
        }

    }

public static String request(HttpResponse response) {
        String result = "";

    try {
            InputStream in = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in));
        StringBuilder str = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                str.append(line + "\n");
            }
            in.close();
            result = str.toString();
        } catch (Exception ex) {
            result = "Error";
        }
        return result;
    }
    }

adduser.php

<?php
$nama = $_GET['nama'];
$pass = $_GET['pass'];
$tinggi = $_GET['tinggi'];
$usia = $_GET['usia'];


$con = mysql_connect("localhost","root","");

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db('tugas_akhir', $con);

mysql_query("INSERT INTO login (username, password) VALUES ('$nama', '$pass')");


mysql_query("INSERT INTO data_user (username, tinggi_badan,usia)
VALUES ('$nama','$tinggi' , '$usia')");


mysql_close($con);
?>

daftar_akun.xml

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#99cc00"
    android:scrollbarSize="1dp" >

   <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="28dp"
        android:layout_marginRight="15.9dp"
        android:layout_marginTop="17dp"
        android:background="@drawable/registrasi_utama_hdpi"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/showError"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="6dp"
            android:layout_marginTop="57dp"
            android:background="@drawable/registrasi_hdpi_error"
            android:paddingLeft="43dp"
            android:paddingRight="9dp"
            android:singleLine="true"
            android:textColorHint="#ffffff"
            android:textSize="14dp"
            android:visibility="invisible" />

        <EditText
            android:id="@+id/inputUsername"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="26.5dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="15.41dp"
            android:background="@drawable/registrasi_hdpi_username"
            android:hint="Masukkan Username"
            android:inputType="textPersonName"
            android:maxLength="8"
            android:paddingLeft="43dp"
            android:paddingRight="9dp"
            android:singleLine="true"
            android:textColorHint="#ffffff"
            android:textSize="14dp" />

        <EditText
            android:id="@+id/inputPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="26.5dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="9dp"
            android:background="@drawable/registrasi_hdpi_password"
            android:hint="Masukkan Password"
            android:inputType="textPassword"
            android:maxLength="8"
            android:paddingLeft="43dp"
            android:paddingRight="9dp"
            android:singleLine="true"
            android:textColorHint="#ffffff"
            android:textSize="14dp" />

        <EditText
            android:id="@+id/inputPasswordAgain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="26.5dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="9dp"
            android:background="@drawable/registrasi_hdpi_password"
            android:hint="Ulangi Password  "
            android:inputType="textPassword"
            android:maxLength="8"
            android:paddingLeft="43dp"
            android:paddingRight="9dp"
            android:singleLine="true"
            android:textColorHint="#ffffff"
            android:textSize="14dp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="26.5dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="9dp"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/showTanggal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/registrasi_hdpi_calendar"
                android:hint="Tanggal Lahir"
                android:paddingLeft="43dp"
                android:paddingRight="9dp"
                android:paddingTop="5dp"
                android:singleLine="true"
                android:textColorHint="#ffffff"
                android:textSize="14dp" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#ffffffff"
                android:src="@drawable/registrasi_hdpi_calendar_edit" />
        </LinearLayout>

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="26.5dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="9dp" >

            <ImageButton
                android:id="@+id/buttonAddHealth"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#ffffffff"
                android:src="@drawable/registrasi_hdpi_health" />

            <TextView
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Klik untuk Riwayat Penyakit "
                android:paddingLeft="43dp"
                android:paddingRight="9dp"
                android:singleLine="true"
                android:textColorHint="#ffffff"
                android:textSize="14dp" />
        </FrameLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="26.5dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="15dp"
            android:background="@drawable/registrasi_hdpi_22"
            android:hint="Isi Jika Sudah Pernah Check "
            android:maxLength="8"
            android:paddingLeft="43dp"
            android:paddingRight="9dp"
            android:singleLine="true"
            android:textColorHint="#ffffff"
            android:textSize="14dp" />


        <EditText
            android:id="@+id/inputTinggi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="26.5dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="11dp"
            android:background="@drawable/registrasi_hdpi_24"
            android:hint="Tinggi Badan"
            android:inputType="number"
            android:maxLength="8"
            android:paddingLeft="43dp"
            android:paddingRight="9dp"
            android:singleLine="true"
            android:textColorHint="#ffffff"
            android:textSize="14dp" />

        <EditText
            android:id="@+id/inputUsia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="26.5dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="11dp"
            android:background="@drawable/registrasi_hdpi_24"
            android:hint="Usia"
            android:inputType="number"
            android:maxLength="8"
            android:paddingLeft="43dp"
            android:paddingRight="9dp"
            android:singleLine="true"
            android:textColorHint="#ffffff"
            android:textSize="14dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="30dp" />

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/teksNama"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="9dp"
                android:layout_marginLeft="40dp"
                android:layout_marginRight="30dp" />
        </FrameLayout>

        <Button android:text="Sign Up" android:id="@+id/btnSimpan"
        android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>

    </LinearLayout>



</ScrollView>

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

对不起。也许我的回答是错的。但是当我使用URL

时,我遇到了同样的问题
private String url = "http://10.0.2.2/TA/adduser.php";

然后我改变了像

这样的格式
private static String url  = "http://192.168.137.1:81/TA/adduser.php";

然后我工作正常。我再次说我不知道​​我的答案是对还是错。但试试这个。