I'm doing some assignment, and I need to enable sync of SQLite DB data to MySQL DB on localhost server. On button click, data from SQLite needs to be "gathered", converted to JSON and sent to localhost MySQL DB and inserted there. I made Activity that handles that job, made some PHP according to academy example, and have wamp server running where I made database in which data needs to be stored. On my localhost database is named employes_db , and table within is named employes. Here is the code from android studio which I made:
package com.EmDatabase;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DataSyncManager extends Activity {
Database db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sync_options);
db = new Database(this);
db.getWritableDatabase();
}
public void syncToServer(View v) throws JSONException {
List<Employe> employes = db.selectAll();
JSONObject objEmployes = new JSONObject();
JSONArray employesData = new JSONArray();
for (Employe employe : employes) {
int id = employe.getId();
String name = employe.getName();
String surname = employe.getSurname();
int age = employe.getAge();
String company = employe.getCompany();
String wtype = employe.getWorktype();
JSONObject empData = new JSONObject();
empData.put("id", id);
empData.put("name", name);
empData.put("surname", surname);
empData.put("age", age);
empData.put("company", company);
empData.put("worktype", wtype);
employesData.put(empData);
}
objEmployes.put("all_employes", employesData);
String result = objEmployes.toString();
System.out.println(result);
UploadJsonStringTask task = new UploadJsonStringTask();
task.execute(
new String[] { "http://10.0.2.2/employes_db/register.php", result}
);
}
public void syncFromServer(View v) {
}
private class UploadJsonStringTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String response = "";
Map<String,String> queries = new HashMap<String, String>(1);
queries.put("all_employes", params[1]);
try {
response += postHttpContent(params[0],queries);
} catch (IOException e) {
Log.e("error", e.toString());
}
return response;
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
public String postHttpContent(String urlString, Map<String, String> queries) throws IOException {
String response = "";
URL url = new URL(urlString);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setDoInput(true);
httpConnection.setDoOutput(true);
httpConnection.setUseCaches(false);
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String postData = "";
for (String key : queries.keySet()) {
postData += "&" + key + "=" + queries.get(key);
}
postData = postData.substring(1);
DataOutputStream postOut = new DataOutputStream(httpConnection.getOutputStream());
postOut.writeBytes(postData);
postOut.flush();
postOut.close();
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
}
} else {
response = "";
throw new IOException();
}
return response + " *** Uploaded!";
}
}
public void goBack(View v) {
Intent in= new Intent(DataSyncManager.this,MainActivity.class);
startActivity(in);
}
}
And here is the PHP file I made and inserted into wampserver/www/employes_db (register.php):
<?php
$id = 0;
$name = "";
$surname = "";
$age = 0;
$company = "";
$worktype = "";
$conn = new mysqli("localhost","root","","employes_db");
$conn->query("insert into employes values (null,'".$_POST['id']."','".$_POST['name']."','".$_POST['surname']."','".$_POST['age']."','".$_POST['company']."','".$_POST['worktype']."')");
if(!$conn->error) echo "{status:0}"; else echo "{status:-1}";
?>
When I launch app, insert one row in SQLite database, than hit sync button, and I open "localhost/employes_db/register.php" I get "errors" -> Notice: Undefined index: id in C:\wamp64\www\employes_db\register.php on line 9 <- And same error for rest of columns(name,surname,age,company,wtype). May someone help, where is my mistake?