Android Web Service Implememnation
I am new to android development. I am quite learning things faster.
I have few queries to Post here:
I need code for Calling web service for my Login Screen in Android. i.e. when user enters his username and password in the login screen, call web service and check valid user or not from my server and di开发者_Python百科spaly error.
I also have problem with webview in Android. Always i get the webview with "PAGE NOT FOUND" error.
Kindly help me in solving my queries.
1.) I assume that this question does not refer to the WebView question. From within your app, you can create connections to your server using java.net.Socket
or java.nio.channels.SocketChannel
. I prefer SocketChannel as it has some features that the basic Socket does not have (e.g. non-blocking mode).
With this class it's fairly easy to communicate with your server, for example:
// this code has not been tested (!)
// Username and Password will be sent in a single string to the server, namely "username|password"
String username = "foo";
String password = "bar";
// Open Socket connection to the Server example.com at Port 12345
SocketChannel sock = SocketChannel.open(new InetSocketAddress("example.com", 12345));
// send user credentials to server
String data = username + "|" + password;
sock.write(ByteBuffer.wrap(data.getBytes()));
// await response from server
ByteBuffer result = ByteBuffer.allocate(8); // 8 byte-large container for result
fSocket.read(result);
// The first byte of the response decides wether login failed or succeeded (just as an example!)
if (result.get(0) == 1) {
// login succeeded
} else {
// login failed
}
However, if you use the blocking mode, I would suggest to move the connection handling in its own thread. For mor information on Socket communication, see the Socket and SocketChannel documentation.
2.) If the Page is not found, maybe the URL is in a wrong format? Some details would help here.
精彩评论