kongkea
kongkea

Reputation: 9978

Json can not be convert to int

I follow this tutorial http://vikaskanani.wordpress.com/2011/08/03/android-proper-way-to-cancel-asynctask/

I try to make app for login. when I follow this tutorial, it has errors. I think it error from onProgressDialog at int success = jResponse.getInt("user"); but I don't know how to solve it.

url for login

http://192.168.10.111/user/auth/?login_id=kongkea&password=kongkea&app_id=103574020240693

(In Browser) when login success, return json

`{
    "user": {
        "user_id": "15",
        "firstname": "kea",
        "lastname": "kong",
        "gender": null,
        "email": null,
        "picture": null,
        "total_friends": 0,
        "total_cards": 0,
        "friend_status": "SELF"
    },
    "token": "NCTak4hOqzLUMCOYkTOG1wFCqjsxI0yKM4YGGw9pa2oz8QNCNiXeWEAOBhvTOHJKAWOvXo9zy2y5Jp9MK1PuSpiT2OALXq94acWsQ1fq0axi2UCD1DENd5Kzf54JL"
}`

DashboardActivity

public class DashboardActivity extends BaseActivity {

private EditText etUsername;
private EditText etPassword;
private static String appId = "103574020240693";
private ProgressDialog progressDialog;
private static final int PROGESSDIALOG_ID = 0;
private static final int SERVER_ERROR = 1;
private static final int NETWORK_ERROR = 2;
private static final int CANCELED = 3;
private static final int SUCCESS = 4; 
private String ServerResponse;
private LoginTask loginTask;

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

    etUsername = (EditText) findViewById(R.id.loginEmail);
    etPassword = (EditText) findViewById(R.id.loginPassword);

    Button login_button = (Button) findViewById(R.id.btnLogin);
    login_button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if(etUsername.getText().toString().length() == 0
                    || etPassword.getText().toString().length() == 0
                    || appId.length() == 0){
                Toast.makeText(getApplicationContext(), "Please enter username and password", Toast.LENGTH_SHORT).show();
            } else {
                showDialog(PROGESSDIALOG_ID);
            }
        }
    });

    isNetworkAvailable();
}

protected Dialog onCreateDialog(int id) {
    switch(id){
    case PROGESSDIALOG_ID:
        removeDialog(PROGESSDIALOG_ID);
        progressDialog = ProgressDialog.show(DashboardActivity.this, "Authenticating", "Please wait...",true,true, new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                if(loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED)
                    loginTask.cancel(true);
            }
        });
        break;
        default: progressDialog = null;
    }
    return progressDialog;
}



@Override
protected void onPrepareDialog(int id, Dialog dialog){
    switch(id){
    case PROGESSDIALOG_ID:
        if(loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED)
            loginTask.cancel(true);
        loginTask = new LoginTask();
        loginTask.execute();
    }
}

class LoginTask extends AsyncTask<Void, Integer, Void>{

    @Override
    protected Void doInBackground(Void... unused) {
         try {
            ServerResponse = null;
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpGet httpGet = new HttpGet(getString(R.string.webserverurl)
                    +URLEncoder.encode(etUsername.getText().toString(),"UTF-8")
                    +"&password="+ URLEncoder.encode(etPassword.getText().toString(),"UTF-8")+"&app_id="+appId);
            httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Some user agent string");

            if(isCancelled()){
                publishProgress(CANCELED);
                return (null);
            }

            HttpResponse httpResponse = httpClient.execute(httpGet,localContext);
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(),"UTF-8"));
            ServerResponse = reader.readLine();
            publishProgress(SUCCESS);
        } catch (UnknownHostException e) {
            removeDialog(PROGESSDIALOG_ID);
            e.printStackTrace();
            publishProgress(NETWORK_ERROR);
        } catch (Exception e){
            removeDialog(PROGESSDIALOG_ID);
            e.printStackTrace();
            publishProgress(SERVER_ERROR);
        }
        return null;
    }


    @Override
    protected void onProgressUpdate(Integer...errorCode){
        switch(errorCode[0]){
        case CANCELED:
            removeDialog(PROGESSDIALOG_ID);
            Toast.makeText(getApplicationContext(), "Cancel by user", Toast.LENGTH_LONG).show();
            break;
        case NETWORK_ERROR:
            removeDialog(PROGESSDIALOG_ID);
            Toast.makeText(getApplicationContext(), "Network connection error", Toast.LENGTH_LONG).show();
            break;
        case SERVER_ERROR:
            removeDialog(PROGESSDIALOG_ID);
            Toast.makeText(getApplicationContext(), "Server Error", Toast.LENGTH_LONG).show();
            break;
        case SUCCESS:
            removeDialog(PROGESSDIALOG_ID);
            try {
                if (ServerResponse != null) {
                    JSONObject jResponse = new JSONObject(ServerResponse);
                    String sMessage = jResponse.getString("user");
                    int success = jResponse.getInt("user");
                    if(success == 1){
                        DashboardActivity.this.finish();
                    } else {
                        Toast.makeText(getApplicationContext(), sMessage, Toast.LENGTH_LONG).show();
                    }
                }

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Server Error", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
            break;
        }
    }

    @Override
    protected void onPostExecute(Void unused){

    }
}

 @Override
 protected void onDestroy(){
     //you may call the cancel() method but if it is not handled in doInBackground() method
     if (loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED)
         loginTask.cancel(true);
     super.onDestroy();
 }

}

Error

12-04 14:43:35.258: W/System.err(1039): org.json.JSONException: Value {"picture":null,"total_cards":0,"friend_status":"SELF","email":null,"gender":null,"lastname":"kong","user_id":"15","firstname":"kea","total_friends":0} at user of type org.json.JSONObject cannot be converted to int
12-04 14:43:35.258: W/System.err(1039):     at org.json.JSON.typeMismatch(JSON.java:100)
12-04 14:43:35.258: W/System.err(1039):     at org.json.JSONObject.getInt(JSONObject.java:446)
12-04 14:43:35.268: W/System.err(1039):     at com.example.androidlogin.DashboardActivity$LoginTask.onProgressUpdate(DashboardActivity.java:157)
12-04 14:43:35.268: W/System.err(1039):     at com.example.androidlogin.DashboardActivity$LoginTask.onProgressUpdate(DashboardActivity.java:1)
12-04 14:43:35.268: W/System.err(1039):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:618)
12-04 14:43:35.268: W/System.err(1039):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 14:43:35.268: W/System.err(1039):     at android.os.Looper.loop(Looper.java:137)
12-04 14:43:35.268: W/System.err(1039):     at android.app.ActivityThread.main(ActivityThread.java:4424)
12-04 14:43:35.268: W/System.err(1039):     at java.lang.reflect.Method.invokeNative(Native Method)
12-04 14:43:35.268: W/System.err(1039):     at java.lang.reflect.Method.invoke(Method.java:511)
12-04 14:43:35.268: W/System.err(1039):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-04 14:43:35.268: W/System.err(1039):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-04 14:43:35.280: W/System.err(1039):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 849

Answers (3)

Blackbelt
Blackbelt

Reputation: 157457

user is a JSONObject not an int, so you should retrieve the inner JSONObject. Repleace

int success = jResponse.getInt("user");

with

JSONObject userJsonObj  = jResponse.getJSONObject("user")
int success = userJsonObj.getInt("user_id");

you have to use the userJsonObj object to retrive the content you need

Upvotes: 2

Snicolas
Snicolas

Reputation: 38168

You misuse AyncTasks : first you should do the parsing inside doInBackground. Use progress only for a progress bar or ui progress notification.onPostExecute should be the place where you update the ui in case of success or failure.

But globally, using AsyncTask for networking is not a good idea. The javadoc states that they are designed for short running tasks. And networking is not a short running task.

You should consider using an android service for your network requests. There is a library that can help you for that : RoboSpice. It will help you creating a service and provides out of the box REST support if you use the spring android module or the google http java client module.


If you really want to go on using AsyncTask then the answer of @Maxim Shoustin is what you are looking for.

Upvotes: 0

Maxim Shoustin
Maxim Shoustin

Reputation: 77910

I think you have issue in row:

String sMessage = jResponse.getString("user");
int success = jResponse.getInt("user");

Suppose it should be: int success = jResponse.getInt("user_id");

Upvotes: 3

Related Questions