koush / ion

Android Asynchronous Networking and Image Loading
Other
6.29k stars 1.04k forks source link

post json -> return null #726

Open andhikayuana opened 8 years ago

andhikayuana commented 8 years ago

hello, im trying to change post json on service with ion, but i get null on result. when im using AsyncTask normal.

this is my code,

SomeApplication.java

..
public void onCreate() {
        super.onCreate();
        singleton = this;
        Ion.getDefault(getApplicationContext()).getConscryptMiddleware().equals(false);
        packageName = getPackageName();
    }

public static Builders.Any.B createRequestWithToken(Context context, String url) {
        return Ion.with(context)
                .load(url)
                .addHeader("Authorization", "some_token"));
    }
..

SomeService.java

..
@Override
public void onLocationChanged(Location location) {

    if (location != null) {

        Log.e(TAG, "position: " + location.getLatitude() + ", " + location.getLongitude() + " accuracy: " + location.getAccuracy());

        if (location.getAccuracy() <= 100) {
            sendLocationDataToWebsite(location, true);
        }

    }
}

@Override
public void onConnected(Bundle bundle) {
    Log.d(TAG, "onConnected");

    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if ((location != null) && (location.getAccuracy() <= 100) && (location.getTime() > System.currentTimeMillis() - (5 * 60 * 1000)) ) {
        sendLocationDataToWebsite(location, true);
    }
    else {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

}

protected void sendLocationDataToWebsite(final Location location, final boolean sendLog) {

    stopLocationUpdates();

    if (sendLog == true) {

        JsonParser parser   = new JsonParser();

        JsonArray jaLog     = parser.parse(someLog).getAsJsonArray();
        JsonArray jaLogSend = parser.parse(someLogSend).getAsJsonArray();

        for (int i = 0; i < jaLog.size(); i++) {
            jaLogSend.add(jaLog.get(i));
        }

        edit.putString("some_log_send", jaLogSend.toString());
        edit.remove("some_log");
        edit.commit();

        payload.add("some_log",jaLogSend);

    }

    if (location != null){
        payload.addProperty("lat", location.getLatitude());
        payload.addProperty("lon", location.getLongitude());
        payload.addProperty("accuracy", location.getAccuracy());
    }

    payload.addProperty("device", Build.MODEL);

    LearningApplication
            .createRequestWithToken(this,
                    getResources().getString(R.string.url_update))
            .setLogging("YUANA", Log.DEBUG)
            .setJsonObjectBody(payload)
            .asJsonObject()
            .withResponse()
            .setCallback(new FutureCallback<Response<JsonObject>>() {
                @Override
                public void onCompleted(Exception e, Response<JsonObject> response) {

                    if (response != null && response.getHeaders().code() == 200) {

                        JsonObject result = response.getResult();

                        Log.d("YUANA", ""+ result +" - "+ response.getHeaders().code());

                        //todo update shared pref ::Yuana
                        //ketika sukses [200]

                        if (result != null) {
                            edit.putString("ads", result.get("ads").toString());
                            edit.putString("user", result.get("user").getAsString());
                            edit.putString("user_img", result.get("user_img").getAsString());
                            edit.putInt("getop_gaid", result.get("getop_gaid").getAsInt());
                            edit.putInt("gebook_gaid", result.get("gebook_gaid").getAsInt());

                            edit.remove("learning_log_send");

                            edit.commit();
                        }
                    }
                    else if (response != null && response.getHeaders().code() == 403) {
                        LearningApplication.getInstance().logout();
                    }
                    else {
                        //ketika tidak sukses atau response != 200
                    }

                    stopSelf();

                }
            });
    }
..

LogCat

05-27 14:37:04.241 14443-16158/net.example.mobile.some D/ION: (86 ms) http://192.168.24.36:81/example/v3/main/update: Response is not cacheable 05-27 14:37:04.241 14443-16158/net.example.mobile.some D/ION: (87 ms) http://192.168.24.36:81/example/v3/main/update: Connection successful 05-27 14:37:04.244 14443-16158/net.example.mobile.some D/ION: (90 ms) http://192.168.24.36:81/example/v3/main/update: Recycling keep-alive socket 05-27 14:37:04.253 14443-14443/net.example.mobile.some D/YUANA: null - 200

koush commented 8 years ago

Can you give me some real urls to test against?

andhikayuana commented 8 years ago

Sorry @koush the API is not for public yet, so i cant tell you the real urls.