我尝试插入MySQL数据库,它可以工作,但警告对话框没有显示,我没有得到任何来自我的PHP的回复,我也有错误,我不知道是什么原因导致我在Android上新的
以下是完整的代码:
public class TransaksiBaru extends AppCompatActivity {
EditText et_trans, et_total, et_anggota;
Button tambah;
String transaksi, total, anggota;
String url_trans = "http://10.0.2.2/test/new.php";
AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transaksi_baru);
et_trans = (EditText) findViewById(R.id.et_transaksi);
et_total = (EditText) findViewById(R.id.et_total);
et_anggota = (EditText) findViewById(R.id.et_user);
tambah = (Button) findViewById(R.id.btn_tambah);
builder = new AlertDialog.Builder(TransaksiBaru.this);
tambah.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
transaksi = et_trans.getText().toString();
total = et_total.getText().toString();
anggota = et_anggota.getText().toString();
if (transaksi.equals("") || total.equals("") || anggota.equals("")) {
builder.setMessage("Data Belum Lengkap....");
displayAlert("input_error");
} else {
StringRequest stringRequest = new StringRequest(Request.Method.POST, url_trans,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String code = jsonObject.getString("code");
String message = jsonObject.getString("message");
builder.setMessage(message);
displayAlert(code);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("transaksi", transaksi);
params.put("nilai", total);
params.put("id_anggota1", anggota);
return params;
}
};
AppController.getInstance(TransaksiBaru.this).addToRequestQueue(stringRequest);
}
}
});
}
public void displayAlert(final String str) {
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (str.equals("input_error")) {
}
else if (str.equals("input_sukses"))
{
finish();
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
这是php代码:
include "init.php";
$transaksi = $_POST['transaksi'];
$nilai = $_POST['nilai'];
$id_anggota1 = $_POST['id_anggota1'];
$sql = "INSERT INTO transaksi (id_transaksi,transaksi,total,id_anggota) VALUES('','".$transaksi."','".$nilai."','".$id_anggota1."')";
$result = mysqli_query($con,$sql);
$response = array();
$code = "input_sukses";
$message = "Data Berhasil Ditambahkan....";
array_push($response, array("code"=>$code,"message" => $message));
echo json_encode($response);
mysqli_close($con);
警报对话框适用于input_error,但不适用于input_sukses
我也遇到了这个错误:
W/System.err: org.json.JSONException: Value <br><h3>Connection of type java.lang.String cannot be converted to JSONArray
W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
W/System.err: at org.json.JSONArray.<init>(JSONArray.java:96)
W/System.err: at org.json.JSONArray.<init>(JSONArray.java:108)
W/System.err: at com.rzproject.koperasi.TransaksiBaru$1$1.onResponse(TransaksiBaru.java:67)
W/System.err: at com.rzproject.koperasi.TransaksiBaru$1$1.onResponse(TransaksiBaru.java:62)
W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
出了什么问题?