1

现在这段代码不起作用

public class MainActivity extends Activity {


String X = "Music"; 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);


 new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... voids) {
            Socket socket = null;
             DataOutputStream dataOutputStream = null;
             DataInputStream dataInputStream = null;

             try {
              socket = new Socket("172.16.82.131", 8888);
              dataOutputStream = new DataOutputStream(socket.getOutputStream());
              dataInputStream = new DataInputStream(socket.getInputStream());
              dataOutputStream.writeUTF(X);
              //textIn.setText(dataInputStream.readUTF());
             } catch (UnknownHostException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             }
             finally{
              if (socket != null){
               try {
                socket.close();
               } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
               }
              }

              if (dataOutputStream != null){
               try {
                dataOutputStream.close();
               } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
               }
              }

              if (dataInputStream != null){
               try {
                dataInputStream.close();
               } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
               }
              }
             }


            return null;
        }
    }.execute();




   }


    }

在我较早的帖子中,我发布了一个代码,其中在单击按钮时正在执行一项功能,但现在我已将我的代码更改为这个。我在启动应用程序时尝试了这种在后台运行我的代码的方法。但没有任何反应。我无法找到我出错的地方..请帮助:(

4

6 回答 6

1

尝试这个

public class AndroidClient extends Activity {  

        String x = "Music";
             /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         textIn = (TextView)findViewById(R.id.textin);
      doThis();

    }                
       public void doThis() {
     // TODO Auto-generated method stub
     Socket socket = null;
     DataOutputStream dataOutputStream = null;
     DataInputStream dataInputStream = null;

     try {
     socket = new Socket("112.13.835.187", 8183);
     dataOutputStream = new DataOutputStream(socket.getOutputStream());
     dataInputStream = new DataInputStream(socket.getInputStream());
     dataOutputStream.writeUTF(x);
     textIn.setText(dataInputStream.readUTF());
     } catch (UnknownHostException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     }
     finally{
     if (socket != null){
     try {
     socket.close();
     } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
      }
     }

     if (dataOutputStream != null){
     try {
     dataOutputStream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
     e.printStackTrace();
     }
     }

     if (dataInputStream != null){
      try {
       dataInputStream.close();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }
       }
       } 
       }};
    }
于 2013-04-03T12:01:01.543 回答
1

在 onCreate 中编写代码,或者您也可以将Asynctask用于后台进程

于 2013-04-03T12:01:55.167 回答
1

您必须在应用程序的onCreate()函数中编写onClick()函数的代码。并且不需要实现按钮的onClickListener函数。

于 2013-04-03T12:31:04.763 回答
1

使用 AsyncTask 处理DataOutputStream 和 Socket

请参见下面的示例代码

从 onCreate() 方法调用以下行

 new SignInService().execute();

然后像下面这样创建类并在onPostExecute 方法中写入这一行 textIn.setText(dataInputStream.readUTF());

private class SignInService extends AsyncTask<Void, Void, Void> {
      Socket socket = null;
      DataOutputStream dataOutputStream = null;
      DataInputStream dataInputStream = null;

     @Override
    protected void onPreExecute() {
        super.onPreExecute();
        try {
            progressDilaog = ProgressDialog.show(MainActivity.this, "",
                    "Loading", true, false);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected Void doInBackground(Void... params) {

     try {
        socket = new Socket("112.13.835.187", 8183);
        dataOutputStream = new DataOutputStream(socket.getOutputStream());
          dataInputStream = new DataInputStream(socket.getInputStream());
          dataOutputStream.writeUTF(x);

      } catch (UnknownHostException e) {

        e.printStackTrace();
      } catch (IOException e) {
           // TODO Auto-generated catch block
         e.printStackTrace();
      }
      finally{
         if (socket != null){
         try {
            socket.close();
          } catch (IOException e) {
            // TODO Auto-generated catch block
             e.printStackTrace();
           }
          }

        if (dataOutputStream != null){
          try {
            dataOutputStream.close();
          } catch (IOException e) {
           // TODO Auto-generated catch block
                 e.printStackTrace();
          }
        }

        if (dataInputStream != null){
           try {
               dataInputStream.close();
            } catch (IOException e) {
              e.printStackTrace();
             }
        }
      }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        progressDilaog.dismiss();

        textIn.setText(dataInputStream.readUTF());
    }
}
于 2013-04-03T12:04:16.087 回答
1

将 onClick() 重命名为其他名称,删除按钮和单击侦听器。然后它将失败,因为您正在 UI 线程上进行网络调用。您将需要使用 AsyncTask() (或类似的。)

如果你只是想让它运行而不关心答案......

public void onCreate(Bundle savedInstanceState) {
    // ...
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... voids) {
            try {
                // your code here
            } catch (Exception e) {
                // catch any errors
            }
            return null;
        }
    }.execute();
    // ...
}
于 2013-04-03T11:59:41.407 回答
1

试试这段代码...这里不需要实现按钮的onclick监听

public class AndroidClient extends Activity {

String x = "Music";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 Button buttonSend = (Button)findViewById(R.id.send);
 textIn = (TextView)findViewById(R.id.textin);
 startonCreate(); 
 }
 public void startonCreate(){
 Socket socket = null;
 DataOutputStream dataOutputStream = null;
 DataInputStream dataInputStream = null;

 try {
  socket = new Socket("112.13.835.187", 8183);
  dataOutputStream = new DataOutputStream(socket.getOutputStream());
  dataInputStream = new DataInputStream(socket.getInputStream());
 dataOutputStream.writeUTF(x);
 textIn.setText(dataInputStream.readUTF());
 } catch (UnknownHostException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (IOException e) {
 // TODO Auto-generated catch block
  e.printStackTrace();
  }
  finally{
  if (socket != null){
 try {
 socket.close();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }

}

if (dataOutputStream != null){
 try {
 dataOutputStream.close();
 } catch (IOException e) {
  // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }

 if (dataInputStream != null){
 try {
    dataInputStream.close();
   } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   }
 }
 } 

 }
}
于 2013-04-03T12:49:13.307 回答