E / AndroidRuntime:致命异常:将文件复制到其他PC后出现AsyncTask#1错误

时间:2019-06-24 04:34:24

标签: java android android-asynctask

我只是使用TortoiseSVN将项目从家用笔记本电脑复制到办公室PC。但是该项目在我的办公室PC上运行不正常。

在这里,我的代码被卡在这里(我认为)。

public class SyncDatabase extends AsyncTask<String, Void, String> {

    Handler mHandler = null;

    public void setHandler(Handler handler) {
        mHandler = handler;
    }

    private static final String TAG = "SyncDatabase";
    Context mContext;
    AlertDialog mAlertDialog;

    public SyncDatabase(Context context) {
        mContext = context;
    }

    String post_url = "http://192.168.56.1/php_poedi/progress_business/admin/process/location-Backup.php";

    @Override
    protected String doInBackground(String... params) {
            try{
                String txtLatitude = params[0];
                String txtLongitude = params[1];
                String editUserID = params[2];
                String editEmailPHP = params[3];
                String tempRadio = params[4];
                String editDate = params[5];
                String nationality = params[6];

                URL url = new URL(post_url);
                Log.d(TAG, "checkpost_url: "+ post_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                Log.d(TAG, "txtLatitude " + txtLatitude + "txtLongitude "+ txtLongitude);
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("latitude", "UTF-8")+"="+URLEncoder.encode(txtLatitude,"UTF-8")
                        +"&"+URLEncoder.encode("longitude", "UTF-8")+"="+URLEncoder.encode(txtLongitude, "UTF-8")
                        +"&"+URLEncoder.encode("username", "UTF-8")+"="+URLEncoder.encode(editUserID, "UTF-8")
                        +"&"+URLEncoder.encode("email", "UTF-8")+"="+URLEncoder.encode(editEmailPHP, "UTF-8")
                        +"&"+URLEncoder.encode("gender", "UTF-8")+"="+URLEncoder.encode(tempRadio, "UTF-8")
                        +"&"+URLEncoder.encode("birthday", "UTF-8")+"="+URLEncoder.encode(editDate, "UTF-8")
                        +"&"+URLEncoder.encode("nationality", "UTF-8")+"="+URLEncoder.encode(nationality, "UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                String result = "";
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    result += line;
                }
                Log.d(TAG, "result: "+ result);
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e){
                e.printStackTrace();
            } catch (IOException e){
                e.printStackTrace();
            }

        return null;
    }

    @Override
    protected void onPreExecute() {
        mAlertDialog = new AlertDialog.Builder(mContext).create();
        mAlertDialog.setTitle("Login Status");
    }

    @Override
    protected void onPostExecute(String s) {
        mAlertDialog.setMessage(s);
        Log.d(TAG, "onPostExecute: "+ s);

        if(mHandler!=null){
            Message message = new Message();
            message.obj = s;
            mHandler.sendMessage(message);
            Log.d(TAG, "Message " + message);
        }
    }
}

这是我的错误。

D/MainActivity: onClick: starts
    Date10/5/2009
    checkradiobutton Male
D/SyncDatabase: checkpost_url: http://192.168.56.1/php_poedi/progress_business/admin/process/location-Backup.php
D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
W/ResourceType: Failure getting entry for 0x7f100000 (t=15 e=0) (error -75)
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: org.example.register, PID: 6130
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:318)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:761)
     Caused by: java.lang.RuntimeException: Failed to parse XML configuration from network_security_config
        at android.security.net.config.XmlConfigSource.ensureInitialized(XmlConfigSource.java:98)
        at android.security.net.config.XmlConfigSource.getPerDomainConfigs(XmlConfigSource.java:65)
        at android.security.net.config.ManifestConfigSource.getPerDomainConfigs(ManifestConfigSource.java:49)
        at android.security.net.config.ApplicationConfig.ensureInitialized(ApplicationConfig.java:169)
        at android.security.net.config.ApplicationConfig.getTrustManager(ApplicationConfig.java:120)
        at android.security.net.config.RootTrustManagerFactorySpi.engineGetTrustManagers(RootTrustManagerFactorySpi.java:65)
        at javax.net.ssl.TrustManagerFactory.getTrustManagers(TrustManagerFactory.java:285)
        at com.android.org.conscrypt.SSLParametersImpl.createDefaultX509TrustManager(SSLParametersImpl.java:933)
        at com.android.org.conscrypt.SSLParametersImpl.getDefaultX509TrustManager(SSLParametersImpl.java:922)
        at com.android.org.conscrypt.SSLParametersImpl.<init>(SSLParametersImpl.java:156)
        at com.android.org.conscrypt.OpenSSLContextImpl.engineInit(OpenSSLContextImpl.java:100)
        at javax.net.ssl.SSLContext.init(SSLContext.java:316)
        at com.android.okhttp.OkHttpClient.getDefaultSSLSocketFactory(OkHttpClient.java:671)
        at com.android.okhttp.OkHttpClient.copyWithDefaults(OkHttpClient.java:630)
        at com.android.okhttp.OkUrlFactory.open(OkUrlFactory.java:59)
        at com.android.okhttp.OkUrlFactory.open(OkUrlFactory.java:54)
        at com.android.okhttp.HttpHandler.openConnection(HttpHandler.java:44)
        at java.net.URL.openConnection(URL.java:991)
        at org.example.register.SyncDatabase.doInBackground(SyncDatabase.java:66)
        at org.example.register.SyncDatabase.doInBackground(SyncDatabase.java:34)
        at android.os.AsyncTask$2.call(AsyncTask.java:304)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
        at java.lang.Thread.run(Thread.java:761) 
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f100000
        at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:190)
        at android.content.res.Resources.loadXmlResourceParser(Resources.java:2094)
        at android.content.res.Resources.getXml(Resources.java:1160)
        at android.security.net.config.XmlConfigSource.ensureInitialized(XmlConfigSource.java:92)
        at android.security.net.config.XmlConfigSource.getPerDomainConfigs(XmlConfigSource.java:65) 
        at android.security.net.config.ManifestConfigSource.getPerDomainConfigs(ManifestConfigSource.java:49) 
        at android.security.net.config.ApplicationConfig.ensureInitialized(ApplicationConfig.java:169) 
        at android.security.net.config.ApplicationConfig.getTrustManager(ApplicationConfig.java:120) 
        at android.security.net.config.RootTrustManagerFactorySpi.engineGetTrustManagers(RootTrustManagerFactorySpi.java:65) 
        at javax.net.ssl.TrustManagerFactory.getTrustManagers(TrustManagerFactory.java:285) 
        at com.android.org.conscrypt.SSLParametersImpl.createDefaultX509TrustManager(SSLParametersImpl.java:933) 
        at com.android.org.conscrypt.SSLParametersImpl.getDefaultX509TrustManager(SSLParametersImpl.java:922) 
        at com.android.org.conscrypt.SSLParametersImpl.<init>(SSLParametersImpl.java:156) 
        at com.android.org.conscrypt.OpenSSLContextImpl.engineInit(OpenSSLContextImpl.java:100) 
        at javax.net.ssl.SSLContext.init(SSLContext.java:316) 
        at com.android.okhttp.OkHttpClient.getDefaultSSLSocketFactory(OkHttpClient.java:671) 
        at com.android.okhttp.OkHttpClient.copyWithDefaults(OkHttpClient.java:630) 
        at com.android.okhttp.OkUrlFactory.open(OkUrlFactory.java:59) 
        at com.android.okhttp.OkUrlFactory.open(OkUrlFactory.java:54) 
        at com.android.okhttp.HttpHandler.openConnection(HttpHandler.java:44) 
        at java.net.URL.openConnection(URL.java:991) 
        at org.example.register.SyncDatabase.doInBackground(SyncDatabase.java:66) 
        at org.example.register.SyncDatabase.doInBackground(SyncDatabase.java:34) 
        at android.os.AsyncTask$2.call(AsyncTask.java:304) 
        at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
        at java.lang.Thread.run(Thread.java:761) 
D/EGL_emulation: eglMakeCurrent: 0xb25840c0: ver 2 0 (tinfo 0xb25835d0)

家用笔记本电脑或办公室PC上的项目都没有进行编辑。 我认为问题出在Android Studio设置中,但在哪一部分呢?我没有任何线索!

这是我的network_security_config.xml

<network-security-config>
    <base-config cleartextTrafficPermitted="true">

        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

还有AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.example.register">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:networkSecurityConfig="@xml/network_security_config"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">    
        </activity>
        <activity
            android:name=".Activity2"
            android:label="@string/location_activity"
            android:parentActivityName=".MainActivity" />

        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="org.example.register.MainActivity" />

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

1 个答案:

答案 0 :(得分:0)

据我从您的logcat所看到的,您正在尝试连接到本地服务器以进行某些位置备份,因此您正在向具有以下IP地址{{1 }}。

在家里,您可能正在运行一个服务器应用程序,该服务器应用程序只能在本地网络中通信。您需要为服务器计算机提供一个公共IP地址,以访问服务器在本地网络(例如您的办公室)之外提供的API。

从Logcat中可以看到,您正在尝试连接到以下主机。

192.168.56.1

您的办公室网络无法访问此地址,因为此IP地址是您家中一台计算机的IP地址(即本地IP地址)。因此,调用此API时找不到资源,我的猜测是,当您从服务器未获得任何信息时,您将不处理响应。

因此,我有两个建议。

  • 还要复制服务器应用程序,然后在办公室的计算机上运行它。获取机器的IP地址,然后放置IP地址而不是http://192.168.56.1/php_poedi/progress_business/admin/process/locationBackup.php 。您可能还会考虑,将您的家用计算机设置为具有公共IP地址,但是,我认为这将需要很长时间才能由您自己执行。
  • 妥善处理从API调用收到的响应。也就是说,当没有响应时,您的代码也应该处理该响应。通常,我们可以使用192.168.56.1块来处理这种情况。如果无法解析服务器的响应,则可以在catch块中获取该响应,并生成try/catch或错误消息。

希望有帮助!