2012-07-09 57 views
0

我目前正在创建一个项目,需要有一个简单的异步任务来照顾幕后运行的线程。用户需要登录。我使用另一个名为PVAndroid Client的类,它提供了有用的方法,并为我提供了XML序列化表单数据包。我对于使用线程或者对服务器做任何事情都是全新的,所以这可能是完全错误的或者有点正确。有关在Android客户端上使用异步的问题

我得到用户输入的数据:IP地址和端口,他们的用户名(我将它分成姓和名),他们选择的区域。我加密他们的密码,并尝试使用ip地址和端口号连接到tcp。我试图在异步任务中工作,但对我应该做什么感到困惑。任何人都可以在正确的方向引导我并帮助我吗?

谢谢我真的很感激。

private TcpClient myTcpClient = null; 
    private UdpClient udpClient; 
    private static final String USERNAME_SHARED_PREFS = "username"; 
    private static final String PASSWORD_SHARED_PREFS = "password"; 
    private static final String IP_ADDRESS_SHARED_PREFS = "ipAddressPref"; 
    private static final String PORT_SHARED_PREFS = "portNumberPref"; 
    private String encryptedNameLoginActivity, encryptPassLoginActivity; 
    private EditText userText, passText; 
    private String getIpAddressSharedPrefs, getPortNumberPrefs; 
    private String getUserNameValue; 
    private String getPasswordValue; 
    private String fName, lName; 
    private SharedPreferences settings; 
    private Editor myEditor; 
    private boolean getCheckedRemember; 
    private boolean resultCheck = false; 
    private int portNum; 
    private Button submitButton; 
    private String userMACVARIABLE = ""; 
    private String regionSelected, gridSelected; 
    private Spinner regSpinner, gridSpinner; 
    PVDCAndroidClient client; 
    private int userNum; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     client = new PVDCAndroidClient(); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 

     // Take care of getting user's login information: 
     submitButton = (Button) findViewById(R.id.submitButton); 
     userText = (EditText) findViewById(R.id.nameTextBox); 
     passText = (EditText) findViewById(R.id.passwordTextBox); 
     regSpinner = (Spinner) findViewById(R.id.regionSpinner); 

     // grid selected as well? sometime? 
     regSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View v, 
        int position, long rowId) { 
       regionSelected = regSpinner.getItemAtPosition(position) 
         .toString(); 

      } 

      @Override 
      public void onNothingSelected(AdapterView<?> arg0) { 
       // TODO Auto-generated method stub 
      } 
     }); 
     submitButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       settings = PreferenceManager 
         .getDefaultSharedPreferences(AndroidClientCompnt.this); 

       getIpAddressSharedPrefs = settings.getString(
         IP_ADDRESS_SHARED_PREFS, ""); 
       portNum = Integer.parseInt(settings.getString(
         PORT_SHARED_PREFS, "")); 

       if (getIpAddressSharedPrefs.length() != 0 && portNum != 0) { 
        if (userText.length() != 0 && passText.length() != 0) { 
         try { 

          try { 
           // encrypting the user's password. 
           encryptPassLoginActivity = Secure.encrypt(passText 
             .toString()); 
          } catch (Exception e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 

          // first connect attempt. 
          myTcpClient = new TcpClient(); 
          myTcpClient.connect(getIpAddressSharedPrefs, 
            portNum); 
          // here is where I want to call Async to do login 
          // or do whatever else. 

          UploadTask task = new UploadTask(); 
          task.execute(); 


         } catch (Exception e) { 
          Toast.makeText(getApplicationContext(), 
            "Could not connect.", Toast.LENGTH_LONG) 
            .show(); 
          e.printStackTrace(); 
         } 
        } 

       } 
      } 
     }); 
    } 

    private class UploadTask extends AsyncTask<String, Integer, Void> 
    { 
     @Override 
     protected void onPreExecute() { 
      Toast.makeText(getApplicationContext(), "Loading...", 
        Toast.LENGTH_LONG).show(); 
          } 

     @Override 
     protected Void doInBackground(String... names) { 

        resultCheck = myTcpClient.connect(getIpAddressSharedPrefs, 
          portNum); 


        if (resultCheck == true) { 
         while (myTcpClient.getUserNum() < 0) { 
          // num set? session? with proxy server? 
         } 

         String[] firstAndLast; 

         String spcDelmt = " "; 

         firstAndLast = userText.toString().split(spcDelmt); 

         fName = firstAndLast[0]; 
         lName = firstAndLast[1]; 

         // set up the tcp client to sent the information to the 
         // server. 
         client.login(fName, lName, encryptPassLoginActivity,regionSelected, 128, 128, 20); 

        } else { 
         Toast.makeText(getApplicationContext(), 
           "Connection not successful", Toast.LENGTH_LONG) 
           .show(); 
        } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      Toast.makeText(getApplicationContext(), "Connected", 
        Toast.LENGTH_LONG).show(); 
     } 
    } 

} 

回答

1

首先

@Override 
protected Void doInBackground(String...params) { 
    new Thread (new Runnable() { 
     // ... 
    } 
} 

永远不会再这样做。没有必要在中创建新的Thread doInBackground方法其实际运行在背景Thread。所以删除它。

的给你的建议是棘手的,因为你需要阅读有关Threads,与Connection等工作,所以给你最好的建议是阅读一些tutorials,基本应用实例和阅读参考。所以,你可以从这里开始:

Android TCP Client and Server Communication Programming–Illustrated with Example

+0

我在阅读您的建议后编辑了一些代码,并拿出了额外的可运行代码,并向我的异步任务添加了一个调用。这关闭了吗?我又是新的,所以在这一点上任何事情都会非常有帮助。这是我编辑的代码:在下午4:09左右更改。 – Tastybrownies 2012-07-09 20:09:16

0

我看不出来,你在哪里yoursing你的任务,但我看到你正在做的东西在里面doInBackground() weired!绝对没有理由在它内部创建自己的Thread。 删除,你可以只用你的任务是这样的:

UploadTask task = new UploadTask(); 
task.execute("someString", "anotherString", "addAsManyStringsYouNeed"); 

AsyncTask的文档是非常有益的,太。