mercredi 23 mars 2016

Error with Async_Http_Response_Handler

I am a beginner in Android development. I am developing an application which receives MySql data and then saves it in SQLite.

I am using Json for sync status so that i can show the the number unsync data as number of pending data to be synced.

The AsyncHttpResponseHandler is showing error in code as " Class 'Anonymous class derived from AsyncHttpResponseHandler method' onFailure(int, Header[], byte[],Throwable)' in 'AsyncHttpResponseHandler' ".

Now i am stuck with a problem in the code which i can't solve.

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class MainActivity extends AppCompatActivity {
// DB Class to perform DB related operations
DBController controller = new DBController(this);
// Progress Dialog Object
ProgressDialog prgDialog;
HashMap<String, String> queryValues;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Get User records from SQLite DB
    ArrayList<HashMap<String, String>> userList = controller.getAllUsers();
    // If users exists in SQLite DB
    if (userList.size() != 0) {
        // Set the User Array list in ListView
        ListAdapter adapter = new SimpleAdapter(MainActivity.this, userList, R.layout.view_logtable_entry, new String[] {
                "id", "time","logtitle","log" }, new int[] { R.id.id, R.id.time, R.id.logtitle, R.id.log });
        ListView myList = (ListView) findViewById(android.R.id.list);
        myList.setAdapter(adapter);
    }
    // Initialize Progress Dialog properties
    prgDialog = new ProgressDialog(this);
    prgDialog.setMessage("Transferring Data. Please wait...");
    prgDialog.setCancelable(false);
    // BroadCase Receiver Intent Object
    Intent alarmIntent = new Intent(getApplicationContext(), SampleBC.class);
    // Pending Intent Object
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    // Alarm Manager Object
    AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    // Alarm Manager calls BroadCast for every Ten seconds (10 * 1000), BroadCase further calls service to check if new records are inserted in
    // Remote MySQL DB
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + 5000, 10 * 1000, pendingIntent);
}

// When Options Menu is selected
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here.
    int id = item.getItemId();
    // When Sync action button is clicked
    if (id == R.id.refresh) {
        // Transfer data from remote MySQL DB to SQLite on Android and perform Sync
        syncSQLiteMySQLDB();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

// Method to Sync MySQL to SQLite DB
public void syncSQLiteMySQLDB() {
    // Create AsycHttpClient object
    AsyncHttpClient client = new AsyncHttpClient();
    // Http Request Params Object
    RequestParams params = new RequestParams();
    // Show ProgressBar
    prgDialog.show();
    // Make Http call to getusers.php
    client.post("http://ift.tt/1Rko3Sx", params, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            // Hide ProgressBar
            prgDialog.hide();
            // Update SQLite DB with response sent by getusers.php
            updateSQLite(response);
        }
        // When error occured
        @Override
        public void onFailure(int statusCode, Throwable error, String content) {
            // TODO Auto-generated method stub
            // Hide ProgressBar
            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();
            }
        }
    });
}

public void updateSQLite(String response){
    ArrayList<HashMap<String, String>> usersynclist;
    usersynclist = new ArrayList<HashMap<String, String>>();
    // Create GSON object
    Gson gson = new GsonBuilder().create();
    try {
        // Extract JSON array from the response
        JSONArray arr = new JSONArray(response);
        System.out.println(arr.length());
        // If no of array elements is not zero
        if(arr.length() != 0){
            // Loop through each array element, get JSON object which has userid and username
            for (int i = 0; i < arr.length(); i++) {
                // Get JSON object
                JSONObject obj = (JSONObject) arr.get(i);
                System.out.println(obj.get("id"));
                System.out.println(obj.get("time"));
                System.out.println(obj.get("logtitle"));
                System.out.println(obj.get("log"));
                // DB QueryValues Object to insert into SQLite
                queryValues = new HashMap<String, String>();
                // Add userID extracted from Object
                queryValues.put("id", obj.get("id").toString());
                // Add userName extracted from Object
                queryValues.put("time", obj.get("time").toString());
                queryValues.put("logtitle", obj.get("logtitle").toString());
                queryValues.put("log", obj.get("log").toString());
                // Insert User into SQLite DB
                controller.insertUser(queryValues);
                HashMap<String, String> map = new HashMap<String, String>();
                // Add status for each User in Hashmap
                map.put("id", obj.get("id").toString());
                map.put("status", "1");
                usersynclist.add(map);
            }
            // Inform Remote MySQL DB about the completion of Sync activity by passing Sync status of Users
            updateMySQLSyncSts(gson.toJson(usersynclist));
            // Reload the Main Activity
            reloadActivity();
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

// Method to inform remote MySQL DB about completion of Sync activity
public void updateMySQLSyncSts(String json) {
    System.out.println(json);
    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams params = new RequestParams();
    params.put("syncstatus", json);
    // Make Http call to updatesyncsts.php with JSON parameter which has Sync statuses of Users
    client.post("http://ift.tt/1VDJmUl", params, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            Toast.makeText(getApplicationContext(), "MySQL DB has been informed about Sync activity", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onFailure(int statusCode, Throwable error, String content) {
            Toast.makeText(getApplicationContext(), "Error Occured", Toast.LENGTH_LONG).show();
        }
    });
}

// Reload MainActivity
public void reloadActivity() {
    Intent objIntent = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(objIntent);
}
}

Aucun commentaire:

Enregistrer un commentaire