我想使用json将数据插入数据库,但是按提交时应用崩溃 我不知道如何解决这个问题,您能帮我吗? ................................................... ................................................... .......................
我已将此网站用作指导 https://androidjson.com/android-php-send-data-mysql-database/
activity_laporan.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="Laporan"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Perkakasan"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_title">
<EditText
android:id="@+id/editText_nama"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:ems="15"
android:hint="Nama Penuh"
android:inputType="textPersonName"
android:textAlignment="center"
android:textSize="14sp" />
<EditText
android:id="@+id/editText_nokp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:ems="15"
android:hint="No Kad Pengenalan"
android:inputType="textPersonName"
android:textAlignment="center"
android:textSize="14sp" />
<EditText
android:id="@+id/editText_notel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:ems="15"
android:hint="No Tel"
android:inputType="textPersonName"
android:textAlignment="center"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3">
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<CheckBox
android:id="@+id/checkBox_komputer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Komputer" />
<CheckBox
android:id="@+id/checkBox_monitor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Monitor" />
<CheckBox
android:id="@+id/checkBox_keyboard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Keyboard" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<CheckBox
android:id="@+id/checkBox_mouse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mouse" />
<CheckBox
android:id="@+id/checkBox_printer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Printer" />
<CheckBox
android:id="@+id/checkBox_scanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Scanner" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<CheckBox
android:id="@+id/checkBox_lcdprojector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="LCD Projector" />
<CheckBox
android:id="@+id/checkBox_ups"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="UPS" />
<CheckBox
android:id="@+id/checkBox_rangkaian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Peralatan Rangkaian" />
</LinearLayout>
</LinearLayout>
<EditText
android:id="@+id/editText_ulasan"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="25dp"
android:layout_marginTop="335dp"
android:ems="10"
android:gravity="start|top"
android:hint="Ulasan"
android:inputType="textMultiLine"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout4" />
<Button
android:id="@+id/button_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Submit"
app:layout_constraintEnd_toStartOf="@+id/button_res"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText_ulasan" />
<Button
android:id="@+id/button_res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Reset"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button_sub"
app:layout_constraintTop_toBottomOf="@+id/editText_ulasan" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
Laporan.java
package com.example.e_aduanjabatanict;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.os.AsyncTask;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Laporan extends AppCompatActivity {
String ServerURL = "http://10.0.2.2/jabatanict/get_data.php";
CheckBox komp, moni, keyb, mous, prin, scan, lcdp, ups, rang;
EditText nama, nokp, notel, ulasan;
Button reset, submit;
String TempNokp, TempNama, TempNotel, TempUlasan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_laporan);
nokp = (EditText) findViewById(R.id.editText_nokp);
nama = (EditText) findViewById(R.id.editText_nama);
notel = (EditText) findViewById(R.id.editText_notel);
ulasan = (EditText) findViewById(R.id.editText_ulasan);
//reset Button
reset = (Button) findViewById(R.id.button_res);
submit = (Button) findViewById(R.id.button_sub);
komp = (CheckBox) findViewById(R.id.checkBox_komputer);
moni = (CheckBox) findViewById(R.id.checkBox_monitor);
keyb = (CheckBox) findViewById(R.id.checkBox_keyboard);
mous = (CheckBox) findViewById(R.id.checkBox_mouse);
prin = (CheckBox) findViewById(R.id.checkBox_printer);
scan = (CheckBox) findViewById(R.id.checkBox_scanner);
lcdp = (CheckBox) findViewById(R.id.checkBox_lcdprojector);
ups = (CheckBox) findViewById(R.id.checkBox_ups);
rang = (CheckBox) findViewById(R.id.checkBox_rangkaian);
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nama.setText("");
nokp.setText("");
notel.setText("");
ulasan.setText("");
if (komp.isChecked()) {
komp.setChecked(false);
}
if (moni.isChecked()) {
moni.setChecked(false);
}
if (keyb.isChecked()) {
keyb.setChecked(false);
}
if (mous.isChecked()) {
mous.setChecked(false);
}
if (prin.isChecked()) {
prin.setChecked(false);
}
if (scan.isChecked()) {
scan.setChecked(false);
}
if (lcdp.isChecked()) {
lcdp.setChecked(false);
}
if (ups.isChecked()) {
ups.setChecked(false);
}
if (rang.isChecked()) {
rang.setChecked(false);
}
}
});
//submit
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GetData();
InsertData(TempNokp, TempNama, TempNotel, TempUlasan);
}
});
}
public void GetData(){
TempNokp = nama.getText().toString();
TempNama = nokp.getText().toString();
TempNotel = notel.getText().toString();
TempUlasan = ulasan.getText().toString();
}
public void InsertData(final String nokp, final String nama, final String notel, final String ulasan){
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String NokpHolder = nokp ;
String NamaHolder = nama ;
String NotelHolder = notel ;
String UlasanHolder = ulasan ;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("nokp", NokpHolder));
nameValuePairs.add(new BasicNameValuePair("nama", NamaHolder));
nameValuePairs.add(new BasicNameValuePair("notel", NotelHolder));
nameValuePairs.add(new BasicNameValuePair("ulasan", UlasanHolder));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(ServerURL);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return "Data Inserted Successfully";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(Laporan.this, "Data Submit Successfully", Toast.LENGTH_LONG).show();
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(nokp, nama, notel, ulasan);
}
}
按提交后此错误显示
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
Process: com.example.e_aduanjabatanict, PID: 8913
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:354)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/message/BasicNameValuePair;
at com.example.e_aduanjabatanict.Laporan$1SendPostReqAsyncTask.doInBackground(Laporan.java:136)
at com.example.e_aduanjabatanict.Laporan$1SendPostReqAsyncTask.doInBackground(Laporan.java:125)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.message.BasicNameValuePair" on path: DexPathList[[zip file "/data/app/com.example.e_aduanjabatanict-rCqKaOCDBhpZ9c3N8IEDkg==/base.apk", zip file "/data/app/com.example.e_aduanjabatanict-rCqKaOCDBhpZ9c3N8IEDkg==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.e_aduanjabatanict-rCqKaOCDBhpZ9c3N8IEDkg==/split_lib_resources_apk.apk", zip file "/data/app/com.example.e_aduanjabatanict-rCqKaOCDBhpZ9c3N8IEDkg==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.e_aduanjabatanict-rCqKaOCDBhpZ9c3N8IEDkg==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.e_aduanjabatanict-rCqKaOCDBhpZ9c3N8IEDkg==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.e_aduanjabatanict-rCqKaOCDBhpZ9c3N8IEDkg==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.e_aduanjabatanict-rCqKaOCDBhpZ9c3N8IEDkg==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.e_aduanjabatanict-rCqKaOCDBhpZ9c3N8IEDkg==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.e_aduanjabatanict-rCqKaOCDBhpZ9c3N8IEDkg==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.e_aduanjabatanict-rCqKaOCDBhpZ9c3N8IEDkg==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.e_aduanjabatanict-rCqKaOCDBhpZ9c3N8IEDkg==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.e_aduanjabatanict-rCqKaOCDBhpZ9c3N8IEDkg==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.e_aduanjabatanict-rCqKaOCDBhpZ9c3N8IEDkg==/lib/x86, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.example.e_aduanjabatanict.Laporan$1SendPostReqAsyncTask.doInBackground(Laporan.java:136)
at com.example.e_aduanjabatanict.Laporan$1SendPostReqAsyncTask.doInBackground(Laporan.java:125)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Suppressed: java.io.IOException: No original dex files found for dex location /data/app/com.example.e_aduanjabatanict-rCqKaOCDBhpZ9c3N8IEDkg==/split_lib_resources_apk.apk
at dalvik.system.DexFile.openDexFileNative(Native Method)
at dalvik.system.DexFile.openDexFile(DexFile.java:354)
at dalvik.system.DexFile.<init>(DexFile.java:101)
at dalvik.system.DexFile.<init>(DexFile.java:75)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:394)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:354)
at dalvik.system.DexPathList.<init>(DexPathList.java:164)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:74)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:65)
at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:73)
at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:88)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:74)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:40)
at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:727)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:810)
at android.app.LoadedApk.getResources(LoadedApk.java:1032)
at android.app.ContextImpl.createAppContext(ContextImpl.java:2345)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5749)
at android.app.ActivityThread.access$1100(ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 8913 SIG: 9
Application terminated.