什么是android.os.NetworkOnMainThreadException

时间:2012-10-16 11:35:10

标签: android

我是新手 我想从我的Android手机发送数据到我的电脑 如果我在我的Android手机上运行它,它说android.os.NetworkOnMainThreadException 有人能帮助我吗?

public void OnClick1(View v) {

    try{
        Socket s=new Socket("localhost",4445);

        DataOutputStream dout =new DataOutputStream(s.getOutputStream());

        dout.writeUTF("hello");
        dout.flush();

        dout.close();
        s.close();

    } catch (Exception e){
        System.out.println(e);
    } 
}

5 个答案:

答案 0 :(得分:3)

可能这可以帮助您解决问题

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);       
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

答案 1 :(得分:3)

NetworkOnMainThreadException:当应用程序尝试在其主线程上执行网络操作时引发的异常。

您可以按如下方式创建AsyncTask课程:

private class MyAsyncTask extends AsyncTask<Void, Void, Void>
{

    @Override
    protected void onPostExecute(Void result) {
        //Task you want to do on UIThread after completing Network operation
        //onPostExecute is called after doInBackground finishes its task.
    }

    @Override
    protected Void doInBackground(Void... params) {
       //Do your network operation here
        return null;
    }
}

另外,不要忘记调用doInBackground()函数,可以使用MyAsyncTask.execute()轻松调用该函数。 希望这会有所帮助..

答案 2 :(得分:2)

这项工作适合我。

请尝试在localhost内部进行IP地址

Android权限:

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

班级RetrieveFeedTask代码:

class RetreiveFeedTask extends AsyncTask<String, Void, Void> {

  private Exception exception;

  protected Void doInBackground(String... datas) {
     try {
        Socket s = new Socket("10.10.10.46", 8888);

        DataOutputStream dout = new DataOutputStream(s.getOutputStream());
        for (String string : datas) {
           dout.writeUTF(string);
        }

        dout.flush();
        dout.close();
        s.close();
     } catch (Exception e) {
        this.exception = e;
     }
     return null;
  }

  protected void onPostExecute(Void void1) {
     // do nothing;
  }

  public RetreiveFeedTask() {
  }

  public Exception getException() {
     return exception;
  }

}


 @Override
 public void onClick(View v) {
     switch (v.getId()) {
     case R.id.btn:

        RetreiveFeedTask feedTask = new RetreiveFeedTask();
        feedTask.execute(new String[] { "hello", "hello" });
        if (feedTask.getException() != null) {
           Log.d(TAG, feedTask.getException().toString());
        }
  }

答案 3 :(得分:1)

始终使用Threads,最好是AsyncTask,以执行网络或任何其他时间进行操作。阅读this

P.S。在Google上进行一些搜索可以为您提供数百个结果。

答案 4 :(得分:1)

试试这个AsyncTask tutorial。有关最佳实践,请使用异步方法。

只需在onCreate方法中尝试此代码

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().penaltyDeath().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().penaltyDeath().build());

但有时很难记住在开发过程中在应用程序中制作所有正确的东西。那是StrictMode进来的。它允许在您的应用程序中设置策略,以避免做错误的事情。例如,如果应用程序违反了某些Android策略,则以下设置会使您的应用程序崩溃。 StrictMode只能在开发期间使用,而不能在您的实时应用程序中使用。

最好避免使用StrictMode,请使用AsyncTask方法。