Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android应用程序因网络问题意外关闭_Java_Android_Sockets_Connect - Fatal编程技术网

Java Android应用程序因网络问题意外关闭

Java Android应用程序因网络问题意外关闭,java,android,sockets,connect,Java,Android,Sockets,Connect,我正在开发我的第一个android应用程序。到目前为止,我的笔记本电脑上运行着一个微型服务器,并从EclipseAndroid工具包(4.0.3)的示例中获取了“骨架应用”示例。我尝试加入一个基本的套接字连接,最终将字符串发送到和从中发送。该应用程序曾抛出IO premission denied异常,但我添加了一行: 应用程序只是意外关闭。如果该行不存在,则应用程序不会关闭,但我当然会遇到例外情况 这是我的密码: 服务器: public class net { public stati

我正在开发我的第一个android应用程序。到目前为止,我的笔记本电脑上运行着一个微型服务器,并从EclipseAndroid工具包(4.0.3)的示例中获取了“骨架应用”示例。我尝试加入一个基本的套接字连接,最终将字符串发送到和从中发送。该应用程序曾抛出IO premission denied异常,但我添加了一行:

应用程序只是意外关闭。如果该行不存在,则应用程序不会关闭,但我当然会遇到例外情况

这是我的密码:

服务器:

public class net {
    public static void main(String [] args){
            ServerSocket ss;
            try {
                    ss = new ServerSocket(10017);
                    System.out.println("waiting");
                    Socket newSock = ss.accept();
                    System.out.println("success!");
            } catch (IOException e) {
                    System.out.println("FAIL");
                    e.printStackTrace();
            }
    }
}

客户端(我添加并调用了buildNet()方法):

}

MANIFEST.XML:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<!-- This file describes the code in the SkeletonApp package, which is
     used by the system to determine how to start your application and
     integrate it with the rest of the system.  -->

<!-- Declare the contents of this Android application.  The namespace
     attribute brings in the Android platform namespace, and the package
     supplies a unique name for the application.  When writing your
     own application, the package name must be changed from "com.example.*"
     to come from a domain that you own or have control over. -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.skeletonapp">

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

<uses-sdk android:targetSdkVersion="11"/>
<!-- This package contains an application...  The 'label' is the name
     to display to the user for the overall application, and provides
     a default label for all following components.  The syntax here is a
     reference to one of our string resources.-->
<application android:label="@string/skeleton_app">

    <!-- An Activity in the application - this is something the user
         can launch and interact with.  The "name" attribute is the
         name of the class within your package that implements this
         activity. -->
    <activity android:name="SkeletonActivity">

        <!-- An IntentFilter tells the system when it should use your
             activity.  This allows the user to get to your activity
             without someone having to explicitly know to launch your
             class "com.examplel.android.skeletonapp.SkeletonActivity". -->
        <intent-filter>
            <!-- The MAIN action describes a main entry point into an
                 activity, without any associated data. -->
            <action android:name="android.intent.action.MAIN" />

            <!-- This places this activity into the main app list. -->
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

</application> </manifest>

您很可能会遇到
NetworkOnMainThreadException
。您应该在
AsyncTask
中调用
buildNet()
,然后在
onPostExecute
中返回结果(在您的情况下是
字符串
),并将其设置为
TextView


要使用
AsyncTask

请尝试将权限放入应用程序标记中,Logcat在哪里?
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<!-- This file describes the code in the SkeletonApp package, which is
     used by the system to determine how to start your application and
     integrate it with the rest of the system.  -->

<!-- Declare the contents of this Android application.  The namespace
     attribute brings in the Android platform namespace, and the package
     supplies a unique name for the application.  When writing your
     own application, the package name must be changed from "com.example.*"
     to come from a domain that you own or have control over. -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.skeletonapp">

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

<uses-sdk android:targetSdkVersion="11"/>
<!-- This package contains an application...  The 'label' is the name
     to display to the user for the overall application, and provides
     a default label for all following components.  The syntax here is a
     reference to one of our string resources.-->
<application android:label="@string/skeleton_app">

    <!-- An Activity in the application - this is something the user
         can launch and interact with.  The "name" attribute is the
         name of the class within your package that implements this
         activity. -->
    <activity android:name="SkeletonActivity">

        <!-- An IntentFilter tells the system when it should use your
             activity.  This allows the user to get to your activity
             without someone having to explicitly know to launch your
             class "com.examplel.android.skeletonapp.SkeletonActivity". -->
        <intent-filter>
            <!-- The MAIN action describes a main entry point into an
                 activity, without any associated data. -->
            <action android:name="android.intent.action.MAIN" />

            <!-- This places this activity into the main app list. -->
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

</application> </manifest>