简体   繁体   中英

no post using ion library

I am trying to post a json object through android ion library on a computer in my local network with the following code :

String url = "http://192.168.1.23/ws/svn/branches/application/public/connectMobile";
                    try {


                        JsonObject json = new JsonObject();
                        json.addProperty("email", email.getText().toString());
                        json.addProperty("psw", password.getText().toString());
                        Log.i("json envoyé",json.toString());



                            Ion.with(context)
                                    .load("POST",url)
                                    .addHeader("Content-Type", "application/json")
                                    .setLogging("ion-geny", Log.DEBUG)
                                    .setJsonObjectBody(json)
                                    .asJsonObject()
                                    .setCallback(new FutureCallback<JsonObject>() {
                                        @Override
                                        public void onCompleted(Exception e, JsonObject result) {
                                            if (result != null)
                                            {
                                                Log.i("result",result.toString());
                                            }
                                            else
                                            {
                                                Log.i("result","null");
                                            }
                                            if (e != null)
                                            {
                                                Log.i("error",e.toString());

                                            }
                                            else
                                            {
                                                Log.i("error","null");
                                            }

                                        }

                                    });



                    } catch (Exception ex) {
                        ex.printStackTrace();
                        Log.i("except", ex.toString());
                    }

The debug gives this :

09-27 12:19:43.302 3309-3309/com.bewizme.bewizmeloginsample I/email﹕ f@f.com 09-27 12:19:43.302 3309-3309/com.bewizme.bewizmeloginsample I/Password﹕ f 09-27 12:19:43.362 3309-3309/com.bewizme.bewizmeloginsample I/connected﹕ connected true 09-27 12:19:43.402 3309-3309/com.bewizme.bewizmeloginsample D/dalvikvm﹕ GC_FOR_ALLOC freed 142K, 7% free 2917K/3116K, paused 15ms, total 18ms 09-27 12:19:43.482 3309-3309/com.bewizme.bewizmeloginsample I/json envoyé﹕ {"email":"f@f.com","psw":"f"} 09-27 12:19:43.602 3309-3309/com.bewizme.bewizmeloginsample D/dalvikvm﹕ GC_FOR_ALLOC freed 154K, 6% free 3275K/3480K, paused 14ms, total 15ms 09-27 12:19:43.622 3309-3309/com.bewizme.bewizmeloginsample D/ion-geny﹕ (0 ms) http://192.168.1.23/ws/svn/branches/application/public/connectMobile : preparing request 09-27 12:19:43.632 3309-3309/com.bewizme.bewizmeloginsample I/ion-geny﹕ (0 ms) http://192.168.1.23/ws/svn/branches/application/public/connectMobile : Using loader: com.koushikdutta.ion.lo ader.HttpLoader@b20a3808 09-27 12:19:43.652 3309-3331/com.bewizme.bewizmeloginsample D/ion-geny﹕ (0 ms) http://192.168.1.23/ws/svn/branches/application/public/connectMobile : Executing request. 09-27 12:19:44.862 3309-3331/com.bewizme.bewizmeloginsample D/ion-geny﹕ (1212 ms) http://192.168.1.23/ws/svn/branches/application/public/connectMobile : Response is not cacheable 09-27 12:19:44.902 3309-3331/com.bewizme.bewizmeloginsample D/ion-geny﹕ (1249 ms) http://192.168.1.23/ws/svn/branches/application/public/connectMobile : Connection successful 09-27 12:19:45.002 3309-3331/com.bewizme.bewizmeloginsample D/dalvikvm﹕ GC_FOR_ALLOC freed 263K, 9% free 3462K/3788K, paused 61ms, total 61ms 09-27 12:19:45.052 3309-3309/com.bewizme.bewizmeloginsample I/result﹕ null 09-27 12:19:45.052 3309-3309/com.bewizme.bewizmeloginsample I/error﹕ com.google.gson.JsonParseException: unable to parse json

I suspect that the parse error is due to the fact that nothing is returned.

Moreover, on my server, I wrote a piece of code to check what's coming in, and I receive nothing on the server from my sample but when trying with RESTEASY with firefox sending the same, the post is correctly sent my server receives the post and a response is sent back.

Any idea ? I can't figure out what's wrong...

if you are getting stuck regarding json parsing error because when you get result into JsonObject formate and try to get string usually people write wrong parsing so I write it in String formate now you need not to parse it you can get your response in String so you can get String easily using .toString and there are lot of mechanism to do this problem and lot of way to write code but meaning would be same but sometime you write code same and meaning also same but compiler could not translate it. so we get error that's why I write here string now you can get your response.

  .addHeader("Content-Type", "application/json")
                                .setLogging("ion-geny", Log.DEBUG)
                                .setJsonObjectBody(jsobj.getAsJsonObject())
                            .asString()
                                .setCallback(new FutureCallback<String>() {
                                    @Override
                                    public void onCompleted(Exception e, String result) {
                                        if (result != null)
                                        {
                                            Log.i("result",result.toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM