The main bug was this: Android has something called a Context. Although I still can't find out as to why it is used, a context is basically something stored in an Activity or Application that tells the phone what's going on. Contexts are important because you need them to start Intents (which basically redirect you from class to class).
The problem was that I needed to call an Intent from a non-Activity class, and to get a Context easily, the main way would be to have an Activity class. The reason I needed to do this was because my Authentication class was a Runnable, not an Activity (Runnables are mainly used for looping, and I needed this to handle client-server interactions to check usernames).
In the end, I ended with this class, AccountVerify.java, which *should* connect to the server and await a servers' response to check usernames if they exist. I haven't written the server-side code yet. If the username exists, it will give the user a Toast message saying that the account has been created and move them over to the MultiplayerMenu (which has a list of servers they can connect to). If not, it will say the username is not available, and to try a different one. And, last but not least, if it is neither (for some odd reason), it will give them an error message, asking if they are connected to the Internet (this is if a response from the server isn't received).
AccountVerify.java
package com.example.AndroidRPGNew.multiplayer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import java.io.DataOutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.util.Scanner;
/**
* Created by fccardiff on 10/14/14.
*/
public class AccountVerify extends Activity implements Runnable,Serializable{
private Socket socket;
private String username;
private String password;
protected Context context;
public AccountVerify(Socket s, String user, String pass){
socket = s;
username = user;
password = pass;
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
run();
}
public void run(){
try{
final DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
final Scanner in = new Scanner(socket.getInputStream());
dataOutputStream.writeByte(80);
dataOutputStream.writeUTF("verify");
dataOutputStream.flush();
dataOutputStream.writeByte(81);
dataOutputStream.writeUTF(username);
dataOutputStream.flush();
dataOutputStream.writeByte(82);
dataOutputStream.writeUTF(password);
dataOutputStream.flush();
if(in.hasNext()){
String canCreate = in.nextLine();
boolean available = Boolean.parseBoolean(canCreate);
if(available == false){
userNotAvailable();
}
else if(available == true){
try{
Toast.makeText(context, "Successfully created user!", Toast.LENGTH_SHORT).show();
Intent startMultiplayer = new Intent(getApplicationContext(), MultiplayerMenu.class);
startActivity(startMultiplayer);
}
catch(Exception e){
e.printStackTrace();
}
}
else{
// TODO Send encountered error, is client connected to internet?
Toast.makeText(context, "Cannot create user! Is client connected to the internet?", Toast.LENGTH_SHORT).show();
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void userNotAvailable(){
Toast.makeText(context, "Username not available! Please try a different username!", Toast.LENGTH_SHORT).show();
}
}
A link from AccountSetup (which handles the text boxes where the user inputs their username/password) allows connection to the server and verification.
ServerConnect.connect();
final int port = 2525;
final String IP = MultiplayerMenu.getIP();
try {
Log.w("attempting socket connection", "...");
socket = new Socket(IP, port);
Log.w("Server:", "Connected to " + IP + ":" + port);
AccountVerify verify = new AccountVerify(socket, user, pass);
Thread thread = new Thread(verify);
thread.start();
}
catch(Exception e){
Log.w("", "Error connecting to Account Verification Server!");
e.printStackTrace();
}
Once again, I haven't tested this yet and it probably has a lot of bugs in it, but it's definitely a starting attempt at getting the server to verify and authenticate users.
Goals for this week:
1. Finish User Authentication
2. Setup basic server announcement commands (maybe automated restarts?)
3. Add "Connect to IP" box in client, to connect to different IPs than the server list
Goals for this week:
1. Finish User Authentication
2. Setup basic server announcement commands (maybe automated restarts?)
3. Add "Connect to IP" box in client, to connect to different IPs than the server list
This is quite cool. From the description in the documentation for Activity, it appears designed to help you focus on small chunks of user functionality (it says it is "a single, focused thing that the user can do"). That is also what I'm trying to encourage you to do with my request for "User Stories".
ReplyDeleteI'm intrigued by the notion of an abstract class to hold "Intents". Any specific examples of how these are used would be most appreciated.
Lastly, it is really time to setup your github repository. Make that the first thing you do next week. The top part of this post should be left just the way it is. The long code source listing should be by way of a link to your repo.