0
package com.login.android;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import com.login.android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class login extends Activity {
// Declare our Views, so we can access them later
private EditText etUsername;
private EditText etPassword;
private Button btnLogin;
private Button btnCancel;
private TextView lblResult;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set Activity Layout
setContentView(R.layout.main);
// Get the EditText and Button References
etUsername = (EditText)findViewById(R.id.username);
etPassword = (EditText)findViewById(R.id.password);
btnLogin = (Button)findViewById(R.id.login_button);
btnCancel = (Button)findViewById(R.id.cancel_button);
lblResult = (TextView)findViewById(R.id.result);
// Set Click Listener
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Check Login
SocketChannel sock = SocketChannel.open(new InetSocketAddress("www.abcd.com", 80));
String data = etUsername + "|" + etPassword;
sock.write(ByteBuffer.wrap(data.getBytes()));
ByteBuffer result = ByteBuffer.allocate(8); // 8 byte-large container for result
fSocket.read(result);
if (result.get(0) == 1)
{
lblResult.setText("Login successful.");
} else {
lblResult.setText("Login failed. Username and or password doesn't match.");
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Close the application
finish();
}
});
}
}
當我運行這個時,我得到fSocket
錯誤。 FSocket cannot be resolved
。請告訴我我在這裏犯了什麼錯誤。Android Web服務執行
這是一個錯誤。由於我們使用「襪子」,使用它來調用!沒有fSocket,它的sock.read(結果);我錯誤地使用了fSocket。 – 2011-03-18 05:32:26