mercredi 28 octobre 2015

Connecting mysql and sqlite using android

I followed the tutorial, http://ift.tt/ZYxP8Q and created an app.I am trying to connect a hosted web.Code is working but gives the Error"Unexpected Error Occured" in onFailure method. I have put the internet permission as well.

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.ProgressDialog;

public class AddNewUsr extends Activity {
    DBHelper controller = new DBHelper(this);
    ProgressDialog prgDialog;
    Button btnRegister;
    EditText txtName,txtLastName,txtPhone,txtMail,txtMobile,txtPassword,txtConPassword;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newregister);

    txtName = (EditText) findViewById(R.id.firstName);
    txtLastName = (EditText) findViewById(R.id.lastName);
    txtMail= (EditText) findViewById(R.id.mail);
    txtMobile= (EditText) findViewById(R.id.mobile);
    txtPassword = (EditText) findViewById(R.id.password);
    txtConPassword = (EditText) findViewById(R.id.confPassword);
    btnRegister=(Button)findViewById(R.id.btnRegister);

    prgDialog = new ProgressDialog(AddNewUsr.this);
    prgDialog.setMessage("Synching SQLite Data with Remote MySQL DB. Please wait...");
    prgDialog.setCancelable(false);

    btnRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
            DBHelper dbHandler=new DBHelper(AddNewUsr.this);
            User user=new User();
            user.setTxtName(txtName.getText().toString());
            user.setTxtLastName(txtLastName.getText().toString());
            user.setTxtMail(txtMail.getText().toString());
            user.setTxtMobile(txtMobile.getText().toString());
            user.setTxtPassword(txtPassword.getText().toString());

            dbHandler.addNewUser(user);
                syncSQLiteMySQLDB();
            }
            catch (Exception e){

            }
        }
    });
}


public void syncSQLiteMySQLDB(){
    //Create AsycHttpClient object
    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams params = new RequestParams();

    params.put("usersJSON", controller.composeJSONforUser());
    prgDialog.show();
    client.post("http://ift.tt/1ikzDS9",params ,new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            System.out.println(response);
            prgDialog.hide();
            try {
                JSONArray arr = new JSONArray(response);
                System.out.println(arr.length());
                for(int i=0; i<arr.length();i++){
                    JSONObject obj = (JSONObject)arr.get(i);
                    System.out.println(obj.get("id"));
                    System.out.println(obj.get("mysql_id"));
                    //controller.updateSyncStatus(obj.get("id").toString(), obj.get("status").toString());
                }
                Toast.makeText(getApplicationContext(), "DB Sync completed!", Toast.LENGTH_LONG).show();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(int statusCode, Throwable error,
                              String content) {
            // TODO Auto-generated method stub
            prgDialog.hide();
            if(statusCode == 404){
                Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
            }else if(statusCode == 500){
                Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]", Toast.LENGTH_LONG).show();
            }
        }


    });


}

}

Please help...

Aucun commentaire:

Enregistrer un commentaire