I want to create a "game server"- the server will be able to manage multiple games (clients), receive/send data from/to client apps and store it in a DB. Since I'm new to Android- I'm having a problem creating this basic server program.
What I already did: An android app for the game (client), the client has a registration page that sends the user details to the server (which I don't have yet) in JSON. My questions:
- I couldn't find any example of a server using Restful Webservice- all I found are client side's examples and not server's. Can you give me any code example of a server which receives and sends data using JSON from/to multiple clients? Or at least a good direction to start and build such server?
- Should I use Android Studio to build the server?
P.S. notice that my questions are about the server side and the connection between it to the clients (not about the connection between server to db).
Thanks.
Client:
public class JsonToServer {
String name, email, password;
Context registerActivityContext;
public JsonToServer(String nameString, String emailString, String passwordString, Context context) {
this.name = nameString;
this.email = emailString;
this.password = passwordString;
this.registerActivityContext = context;
new SendPostRequest().execute();
}
public class SendPostRequest extends AsyncTask<String, Void, String> {
protected void onPreExecute(){}
protected String doInBackground(String... arg0) {
try {
Log.d("params123","before connecting");
URL url = new URL(""); // SERVER path
Log.d("params123","after connecting");
JSONObject postDataParams = new JSONObject();
Log.d("params123","json created");
postDataParams.put("name", name);
postDataParams.put("password", password);
postDataParams.put("email", email);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
Log.d("params123","httpOK");
BufferedReader in=new BufferedReader(new
InputStreamReader(
conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
}
else {
return new String("false : "+responseCode);
}
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(registerActivityContext, result,
Toast.LENGTH_LONG).show();
}
}
public String getPostDataString(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while(itr.hasNext()){
String key= itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
}