package com.prgguru.example;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;
public class MainActivity extends ActionBarActivity{
//DB Class to perform DB related operations
DBController controller = new DBController(this);
//Progress Dialog Object
ProgressDialog prgDialog;
@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(userList.size()!=0){
//Set the User Array list in ListView
ListAdapter adapter = new SimpleAdapter( MainActivity.this,userList, R.layout.view_user_entry, new String[] { "userId","userName"}, new int[] {R.id.userId, R.id.userName});
ListView myList=(ListView)findViewById(android.R.id.list);
myList.setAdapter(adapter);
//Display Sync status of SQLite DB
Toast.makeText(getApplicationContext(), controller.getSyncStatus(), Toast.LENGTH_LONG).show();
}
//Initialize Progress Dialog properties
prgDialog = new ProgressDialog(this);
prgDialog.setMessage("Synching SQLite Data with Remote MySQL DB. Please wait...");
prgDialog.setCancelable(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//When Sync action button is clicked
if (id == R.id.refresh) {
//Sync SQLite DB data to remote MySQL DB
syncSQLiteMySQLDB();
return true;
}
return super.onOptionsItemSelected(item);
}
//Add User method getting called on clicking (+) button
public void addUser(View view) {
Intent objIntent = new Intent(getApplicationContext(), NewUser.class);
startActivity(objIntent);
}
public void syncSQLiteMySQLDB(){
//Create AsycHttpClient object
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
ArrayList<HashMap<String, String>> userList = controller.getAllUsers();
if(userList.size()!=0){
if(controller.dbSyncCount() != 0){
prgDialog.show();
params.put("usersJSON", controller.composeJSONfromSQLite());
client.post("http://192.168.65.1/sqlitemysqlsync/insertuser.php",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("status"));
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 String getCharset() {
// TODO Auto-generated method stub
return super.getCharset();
}
@Override
public Header[] getRequestHeaders() {
// TODO Auto-generated method stub
return super.getRequestHeaders();
}
@Override
public URI getRequestURI() {
// TODO Auto-generated method stub
return super.getRequestURI();
}
@Override
public boolean getUseSynchronousMode() {
// TODO Auto-generated method stub
return super.getUseSynchronousMode();
}
@Override
protected void handleMessage(Message arg0) {
// TODO Auto-generated method stub
super.handleMessage(arg0);
}
@Override
protected Message obtainMessage(int responseMessageId,
Object responseMessageData) {
// TODO Auto-generated method stub
return super.obtainMessage(responseMessageId, responseMessageData);
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
super.onCancel();
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
super.onFinish();
}
@Override
public void onPostProcessResponse(
ResponseHandlerInterface instance,
HttpResponse response) {
// TODO Auto-generated method stub
super.onPostProcessResponse(instance, response);
}
@Override
public void onPreProcessResponse(
ResponseHandlerInterface instance,
HttpResponse response) {
// TODO Auto-generated method stub
super.onPreProcessResponse(instance, response);
}
@Override
public void onProgress(int bytesWritten, int totalSize) {
// TODO Auto-generated method stub
super.onProgress(bytesWritten, totalSize);
}
//@Override
public void onRetry(int retryNo) {
// TODO Auto-generated method stub
super.onRetry(retryNo);
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void postRunnable(Runnable runnable) {
// TODO Auto-generated method stub
super.postRunnable(runnable);
}
@Override
protected void sendMessage(Message msg) {
// TODO Auto-generated method stub
super.sendMessage(msg);
}
@Override
public void sendResponseMessage(HttpResponse arg0)
throws IOException {
// TODO Auto-generated method stub
super.sendResponseMessage(arg0);
}
@Override
public void setCharset(String charset) {
// TODO Auto-generated method stub
super.setCharset(charset);
}
@Override
public void setRequestHeaders(Header[] requestHeaders) {
// TODO Auto-generated method stub
super.setRequestHeaders(requestHeaders);
}
@Override
public void setRequestURI(URI requestURI) {
// TODO Auto-generated method stub
super.setRequestURI(requestURI);
}
@Override
public void setUseSynchronousMode(boolean sync) {
// TODO Auto-generated method stub
super.setUseSynchronousMode(sync);
}
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
@Override
public boolean equals(Object o) {
// TODO Auto-generated method stub
return super.equals(o);
}
@Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
super.finalize();
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return super.hashCode();
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
@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();
}
}
@Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2,
Throwable arg3) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
// TODO Auto-generated method stub
}
});
}else{
Toast.makeText(getApplicationContext(), "SQLite and Remote MySQL DBs are in Sync!", Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getApplicationContext(), "No data in SQLite DB, please do enter User name to perform Sync action", Toast.LENGTH_LONG).show();
}
}
}
编译期间的错误是:
描述资源路径位置类型onSuccess方法(String) 新类型AsyncHttpResponseHandler(){}必须覆盖或实现 超类型方法
new类型的onFailure(int,Throwable,String)方法 AsyncHttpResponseHandler(){}必须覆盖或实现超类型 方法
请帮助!!!
答案 0 :(得分:2)
恕我直言,你需要使用不同的签名来定义onSuccess和onFailure的方法。检查您正在使用的库版本,但不是这样的吗?
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
// Successfully got a response
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
// Response failed :(
}
答案 1 :(得分:1)