简体   繁体   English

Android App因网络异常关闭

[英]Android App closes unexpectedly due to network

I am working on my first android app. 我正在开发我的第一个Android应用程序。 As of now, I have a tiny server running on my laptop, and took the "Skeleton App" sample from the examples the eclipse android kit (4.0.3). 到目前为止,我的笔记本电脑上运行着一个微型服务器,并从eclipse android kit(4.0.3)的示例中获取了“ Skeleton App”示例。 I tried to throw in a basic Socket connection, to eventually send Strings to and from. 我试图抛出一个基本的Socket连接,以最终往返于Strings。 The app USED to throw an IO premission denied exception, but once I added the line: 该应用程序曾被用来抛出IO权限被拒绝的异常,但是一旦我添加以下代码:

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

the app simply unexpectedly closes. 该应用程序意外关闭。 If that line is not there, the app does not close, but of course I get that exception. 如果该行不存在,则应用程序不会关闭,但是我当然会收到该异常。

Here is my code: 这是我的代码:

SERVER: 服务器:

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();
            }
    }

} }

CLIENT (I added and called the buildNet() method): 客户端(我添加并调用了buildNet()方法):

public class SkeletonActivity extends Activity {

static final private int BACK_ID = Menu.FIRST;
static final private int CLEAR_ID = Menu.FIRST + 1;
static private int port = 10013;
static private String address;
static Socket sock;

private EditText mEditor;

public SkeletonActivity() {
}

/** Called with the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Inflate our UI from its XML layout description.
    setContentView(R.layout.skeleton_activity);

    // Find the text editor view inside the layout, because we
    // want to do various programmatic things with it.
    mEditor = (EditText) findViewById(R.id.editor);

    // Hook up button presses to the appropriate event handler.
    ((Button) findViewById(R.id.back)).setOnClickListener(mBackListener);
    ((Button) findViewById(R.id.clear)).setOnClickListener(mClearListener);


    //
    //THIS IS WHERE BUILD NET IS CALLED. ITS RESULT IS RETURNED AND PRINTED TO THE
    //SCREEN OF THE APP
    //
    mEditor.setText(buildNet());
}

/**
 * Called when the activity is about to start interacting with the user.
 */
@Override
protected void onResume() {
    super.onResume();
}

public String buildNet(){
    FileWriter fstream= null;
    BufferedWriter out = null;

    try {
        address = "192.168.1.122";
        sock = new Socket(address, port);
        return "YES";
    } catch (UnknownHostException e) {
        return "UNKNOWN HOST";
    } catch (IOException e) {
        return e.getMessage();
    }
}

/**
 * Called when your activity's options menu needs to be created.
 */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    // We are going to create two menus. Note that we assign them
    // unique integer IDs, labels from our string resources, and
    // given them shortcuts.
    menu.add(0, BACK_ID, 0, R.string.back).setShortcut('0', 'b');
    menu.add(0, CLEAR_ID, 0, R.string.clear).setShortcut('1', 'c');

    return true;
}

/**
 * Called right before your activity's option menu is displayed.
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    // Before showing the menu, we need to decide whether the clear
    // item is enabled depending on whether there is text to clear.
    menu.findItem(CLEAR_ID).setVisible(mEditor.getText().length() > 0);

    return true;
}

/**
 * Called when a menu item is selected.
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case BACK_ID:
        finish();
        return true;
    case CLEAR_ID:
        mEditor.setText("");
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * A call-back for when the user presses the back button.
 */
OnClickListener mBackListener = new OnClickListener() {
    public void onClick(View v) {
        finish();
    }
};

/**
 * A call-back for when the user presses the clear button.
 */
OnClickListener mClearListener = new OnClickListener() {
    public void onClick(View v) {
        mEditor.setText("");
    }
};

} }

MANIFEST.XML : 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>

You are in all likelihood getting a NetworkOnMainThreadException . 您很可能会收到NetworkOnMainThreadException You should put your call to buildNet() within an AsyncTask , and in onPostExecute , return the result (a String in your case), and set it to the TextView . 您应该buildNet()调用放在AsyncTask ,并在onPostExecute ,返回结果(以您的情况为String ),并将其设置为TextView

Here's the docs/guide for using an AsyncTask . 这是使用AsyncTask 的文档/指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM