I am sending data from my android application to remote server. I want to receive a response from server during data insertion; for example: project id. I want to do it because I want to store data in my Local File Server(SQLite) and Server at the same time and the project id is the primary key that is being generated automatically. So I want to update my local database table to synchronize data with server.
Here is my AddActivity.
private void doRegister() throws JSONException {
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("user_request", createJSON());
System.out.println("params..." + params.toString());
client.post(AddJson.this, "http://.../.../index.php/json/add_task", params,
new AsyncHttpResponseHandler() {
@Override
public void onFinish() { // TODO Auto-generated method stub
super.onFinish();
}
@Override
public void onStart() { // called before request is started
mProgress = new ProgressDialog(
AddJson.this);
mProgress.setMessage("Posting...");
mProgress.show();
}
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] response) { // called when response HTTP
// status is "200 OK"
mProgress.dismiss();
Toast.makeText(getApplicationContext(), "Success !"+response.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] errorResponse, Throwable e) {
// called when response HTTP status is "4XX" (eg. 401,
// // 403, 404)
mProgress.dismiss();
Toast.makeText(AddJson.this, "Failed", Toast.LENGTH_SHORT).show();
// System.out.println(new String(errorResponse));
}
@Override
public void onRetry(int retryNo) {
// called when request is retried
}
});
}
public String createJSON() {
JSONObject jsonObject = new JSONObject();
JSONObject jsondata = new JSONObject();
try {
jsonObject.put("task_title", etName.getText().toString());
jsonObject.put("task_details", etCountry.getText().toString());
jsonObject.put("ma_priority_id", "3");
jsonObject.put("project_id", "6");
jsonObject.put("category_id", "251");
jsonObject.put("type_id", "6");
jsonObject.put("assigned_to", "36");
jsonObject.put("assigned_by", "36");
jsonObject.put("effort_hour", "8");
jsonObject.put("from_date", "2015-06-22");
jsonObject.put("to_date", "2015-06-22");
jsonObject.put("start_time", "09:00:00");
jsonObject.put("end_time", "16:00:00");
jsondata.put("task", jsonObject);
send_jsonstring = jsondata.toString();
System.out.println("Send JSON: " + send_jsonstring);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("ee=" + e.toString());
}
return send_jsonstring;
}
public boolean isConnected(){
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
else
return false;
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.saveButton:
if(!validate())
Toast.makeText(getBaseContext(), "Enter some data!", Toast.LENGTH_LONG).show();
else {
try {
doRegister();
db.createTask(new Task(etName.getText().toString(), etCountry.getText().toString(),"off"));
Toast.makeText(getApplication(), "Task Created", Toast.LENGTH_SHORT)
.show();
Intent intent = new Intent(AddJson.this, MainActivity.class);
startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
}
break;
}
}
private boolean validate(){
if(etName.getText().toString().trim().equals(""))
return false;
else if(etCountry.getText().toString().trim().equals(""))
return false;
else
return true;
}
Here is the added data
{"id":"715","project_name":"Mini Goal","author":"xxxxx mandal","start_date":"2015-06-22","end_date":"2015-06-22","task_type_id":"Bug Fix","priority_name":"High","estimated_hr":"8","requirment_title":"JSON for Mobile App version","requirement_description":"21-05-2014\nCurrently we need 3 json file:\n1. Task list for a particular employee.\n2. Effort list for a particular employee.\n3. Log in check and return whether employee\/admin\/developer."}
I want to retrieve "id":"715" this one during data insertion.
Can anyone tell me how can I get the Id from server during data insertion?
Aucun commentaire:
Enregistrer un commentaire