执行doInBackground()时发生错误

时间:2019-11-13 06:46:54

标签: android

我已将实时服务器与android应用程序连接,但是当我单击“提交”按钮创建帐户时,应用程序停止工作。然后跟随exceptions

如何解决此问题...

这是我使用的代码

public void InsertData(final String name, final String email, final String password, final String con_password) {

        class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... params) {

                String NameHolder = name;
                String EmailHolder = email;
                String PasswordHolder = password;
                String ConPasswordHolder = con_password;

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                nameValuePairs.add(new BasicNameValuePair("name", NameHolder));
                nameValuePairs.add(new BasicNameValuePair("email", EmailHolder));
                nameValuePairs.add(new BasicNameValuePair("password", PasswordHolder));
                nameValuePairs.add(new BasicNameValuePair("con_password", ConPasswordHolder));


                try {
                    HttpClient httpClient = new DefaultHttpClient();

                    HttpPost httpPost = new HttpPost("198.96.95.58");


                    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(Create_account.this, "Data Submit Successfully", Toast.LENGTH_LONG).show();

            }
        }

        SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();

        sendPostReqAsyncTask.execute(name, email, password, con_password);
    }

错误:这些异常是在我单击“创建帐户”按钮时出现的。当我调试我的应用程序时,在单击“创建帐户”按钮后出现黑屏。

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
    Process: com.example.myapplication1, PID: 15651
    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.myapplication1.Create_account$1SendPostReqAsyncTask.doInBackground(Create_account.java:80)
        at com.example.myapplication1.Create_account$1SendPostReqAsyncTask.doInBackground(Create_account.java:69)
        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.myapplication1-dD2mzqj0Giijo0j___OG8A==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.myapplication1-dD2mzqj0Giijo0j___OG8A==/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.myapplication1.Create_account$1SendPostReqAsyncTask.doInBackground(Create_account.java:80) 
        at com.example.myapplication1.Create_account$1SendPostReqAsyncTask.doInBackground(Create_account.java:69) 
        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) 

1 个答案:

答案 0 :(得分:0)

要继续使用Apache HTTP client,面向Android 9及更高版本的应用可以将application tag内的以下行添加到AndroidManifest.xml

<uses-library android:name="org.apache.http.legacy" android:required="false"/>
  

注意:android:required =“ false”属性对于SDK最低为23或更低的应用程序是必需的,因为在API级别低于24的设备上,org.apache.http.legacy库不可用。 (在这些设备上,可以在bootclasspath上使用Apache HTTP类。)

查看有关https://developer.android.com/about/versions/pie/android-9.0-changes-28的官方文档