Android的koush / lib中离子不能发布和接收JSON响应 [英] Android koush/ion lib unable to post and receive JSON response

查看:226
本文介绍了Android的koush / lib中离子不能发布和接收JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ION能为REST / POST,并获得图像输入的ListView
但我无法使用POST方法和接收响应作为JSON
我现在用的ION版本是1.3.7。测试的模拟器,也真实设备4.1.1,4.2.2和4.4

ION链接在GitHub上

这code被称为上的一个按钮点击张贴到服务器后的验证是成功的。这里面支持片段V4:20 gradle这个定义

 编译com.android.support:support-v4:20.+///////////////////Ion.with(getActivity())
    .load(URLStr)
    .addQuery(行动,dummyAction)
    .addHeader(内容类型,应用/ JSON)
    .setLogging(ION_VERBOSE_LOGGING,Log.VERBOSE)
    .setJsonObjectBody(jsonStr)
    .asJsonObject()
    .setCallback(新FutureCallback<&JSONObject的GT;(){
    @覆盖
    公共无效onCompleted(例外五,结果的JSONObject){
        如果(NULL!= E){
            如果(NULL!=结果){                Log.d(Constants.LOG_TAG,JSON结果的用户基本信息:+结果);
            }其他{
                Log.e(Constants.LOG_TAG,在用户发布信息的基本错误);
            }
        }其他{
            Log.e(Constants.LOG_TAG,在用户发布信息的基本错误,E);
        }
    }
    });

我在结果得到空指针。
请求JSON也是有效的,而且我在使用GSON来构建它。
响应JSON是有效的,因为我用邮差Chrome应用程式,检查了。

不过,下面code工作完全正常,但它depricated ION.with(getActivity(),URL)
所以我不知道,我在哪里犯错。

  Ion.with(getActivity(),URLStr)
            .addQuery(行动,dummyAction)
            .asJsonObject()
            .setCallback(新FutureCallback<&JSONObject的GT;(){
                @覆盖
                公共无效onCompleted(例外五,结果的JSONObject){
                    如果(E!= NULL){                        e.printStackTrace();
                        Toast.makeText(getActivity(),错误加载用户数据,Toast.LENGTH_LONG).show();
                        返回;
                    }
                    Log.d(Constants.LOG_TAG,DATA:+ result.toString());
                    字符串数据= result.get(状态),符getAsString()。
                    Log.d(Constants.LOG_TAG,DATA:+数据);
                }
            });

下面是第一次调用输出的详细

  08-12 14:18:06.010 27928-27928 / com.abc.xyz:D / ION_VERBOSE_LOGGING:(0毫秒)HTTP://urlHidden/service.php行动= dummyAction:preparing要求
    08-12 14:18:06.010 27928-27928 / com.abc.xyz:I / ION_VERBOSE_LOGGING:(0毫秒)HTTP://urlHidden/service.php行动= dummyAction:使用装载机:com.koushikdutta.ion.loader .HttpLoader @ 42d0e6c8
    08-12 14:18:06.025 27928-28187 / com.abc.xyz:D / ION_VERBOSE_LOGGING:(0毫秒)HTTP://urlHidden/service.php行动= dummyAction:执行请求。
    08-12 14:18:06.040 27928-28187 / com.abc.xyz:I / ION_VERBOSE_LOGGING:(14毫秒)的http://urlHidden/service.php行动= dummyAction:响应可以从有条件的缓存来满足
    08-12 14:18:06.040 27928-28187 / com.abc.xyz:V / ION_VERBOSE_LOGGING:(16毫秒)的http://urlHidden/service.php行动= dummyAction:解决域名并连接到所有可用地址
    08-12 14:18:06.195 27928-28187 / com.abc.xyz:V / ION_VERBOSE_LOGGING:(167毫秒)的http://urlHidden/service.php行动= dummyAction:插座连接
    08-12 14:18:06.195 27928-28187 / com.abc.xyz:V / ION_VERBOSE_LOGGING:(169毫秒)的http://urlHidden/service.php行动= dummyAction:
        GET /service.php?post=stepOne HTTP / 1.1
        主持人:188.226.224.99
        用户代理:的Dalvik / 1.6.0(Linux的; U; Android的4.4.4; GT-N7000构建/ KTU84P)
        接受编码:gzip,紧缩
        连接:保持活动
        接受:* / *
        内容类型:应用程序/ JSON
        如果-Modified-Since的:星期二,2014年8月12日6点42分34秒UTC
    08-12 14:18:06.200 27928-28187 / com.abc.xyz:V / ION_VERBOSE_LOGGING:(170毫秒)的http://urlHidden/service.php行动= dummyAction:请求已完成
    08-12 14:18:06.355 27928-27928 / com.abc.xyz:D / MatchBot:数据:{地位:挂起}
    08-12 14:18:06.370 27928-28187 / com.abc.xyz:V / ION_VERBOSE_LOGGING:(347毫秒)的http://urlHidden/service.php行动= dummyAction:接收的头:
        HTTP / 1.1 200 OK
        日期:星期二,2014年8月12日8时48分08秒GMT
        服务器:Apache / 2.2.22(Ubuntu的)
        的X已启动方式:PHP / 5.3.10-1ubuntu3.11
        有所不同:接受编码
        内容编码:gzip
        内容长度:64
        保持活动:超时= 5,最大值= 100
        连接:保持活动
        内容类型:text / html的
    08-12 14:18:06.380 27928-28187 / com.abc.xyz:D / ION_VERBOSE_LOGGING:(355毫秒)的http://urlHidden/service.php行动= dummyAction:缓存响应
    08-12 14:18:06.380 27928-28187 / com.abc.xyz:V / ION_VERBOSE_LOGGING:(356毫秒)的http://urlHidden/service.php行动= dummyAction:决赛(后缓存响应)标题:
        HTTP / 1.1 200 OK
        日期:星期二,2014年8月12日8时48分08秒GMT
        服务器:Apache / 2.2.22(Ubuntu的)
        的X已启动方式:PHP / 5.3.10-1ubuntu3.11
        有所不同:接受编码
        内容编码:gzip
        内容长度:64
        保持活动:超时= 5,最大值= 100
        连接:保持活动
        内容类型:text / html的
    08-12 14:18:06.380 27928-28187 / com.abc.xyz:D / ION_VERBOSE_LOGGING:(356毫秒)的http://urlHidden/service.php行动= dummyAction:连接成功
    08-12 14:18:06.385 27928-27928 / com.abc.xyz:E / MatchBot:错误发布用户基本信息
    08-12 14:18:06.390 27928-28187 / com.abc.xyz:D / ION_VERBOSE_LOGGING:(364毫秒)的http://urlHidden/service.php行动= dummyAction:回收保持活动插座

/////////////////////////////////////////////// /////////////////

 进口com.google.gson.annotations.SerializedName;的Bean;
进口java.util.Date;/ **
 *创建者riinav上17/6/14。
 * /
公共类用户实现Serializable {    @SerializedName(facebook_id)
    私人字符串userFBId;    @SerializedName(身份)
    私人字符串注册状态;    私人短暂的日期registeredOnDate;    公共用户(){
    }    公共用户(字符串userFBId,字符串注册状态){
    this.userFBId = userFBId;
    this.registrationStatus =注册状态;
    }    / *
     *干将,setter和overidden平等和哈希code方法
     * /
}////////////////////////////////////////////////// /////////////////////////进口com.google.gson.annotations.SerializedName;/ **
 *创建者riinav上27/6/14。
 * /
公共类UserBasicInfo扩展用户{//实现Serializable {    @SerializedName(名称)
    私人字符串用户名;    @SerializedName(电子邮件)
    私人字符串EMAILID;    @SerializedName(性别)
    私人字符串性别;    @SerializedName(BDAY)
    私人字符串DOB;    @SerializedName(国家)
    私人字符串的国家;    @SerializedName(城市)
    私人字符串的城市;    @SerializedName(self_image)
    私人字符串profilePicUrl;    @SerializedName(学校)
    私人字符串学校;    @SerializedName(大学)
    私人字符串学院;    @SerializedName(度)
    私人字符串资格;    @SerializedName(sorientation)
    私人字符串性preference;    @SerializedName(prefered_age)
    私人字符串preferedAge;    @SerializedName(prefered_city)
    私人字符串preferedCity;    公共UserBasicInfo(){
    超();
    }    / *
     *干将,setter和overidden平等和哈希code方法
     * /
}///////////////////////////////////////用户模型JSON发布到服务器网友:{
    大学:孟买大学,
    BDAY:1980年1月1日,
    电子邮件:dummyEmail@gmail.com
    性别:男,
    度:大专学历,
    学校:ABC中学,
    名:真棒名,
    状态:过程,
    facebook_id:0000000000000000//测试fbId
} /////////////////服务器预期的响应 的onSuccess:
 {地位:成功} 的OnError:
 {地位:错误消息}


解决方案

在POJO对象直接传递。离子会自动将其序列化到JSON你。

I am using ION to GET/POST for REST and also to get Images into ListView but I am unable to use POST method and receive response as a JSON The ION version that I am using is 1.3.7. Tested on emulator and also on real devices 4.1.1, 4.2.2 and 4.4

ION Link on gitHub

This code is called on a button click to post to the server after the validations are successful. This is inside support fragment v4:20 defined in gradle

compile 'com.android.support:support-v4:20.+'

///////////////////

Ion.with(getActivity())
    .load(URLStr)
    .addQuery("action", "dummyAction")
    .addHeader("Content-Type", "application/json")
    .setLogging("ION_VERBOSE_LOGGING", Log.VERBOSE)
    .setJsonObjectBody(jsonStr)
    .asJsonObject()
    .setCallback(new FutureCallback<JsonObject>() {
    @Override
    public void onCompleted(Exception e, JsonObject result) {
        if (null != e) {
            if (null != result) {

                Log.d(Constants.LOG_TAG, "JSON Result User basic Info: " + result);
            } else {
                Log.e(Constants.LOG_TAG, "error in posting User basic Info");
            }
        } else {
            Log.e(Constants.LOG_TAG, "error in posting User basic Info", e);
        }
    }
    });

I am getting NullPointer at result. The request JSON is also valid, and I am using GSON to construct it. The response JSON is valid as I checked it using POSTMAN chrome app.

However, below code works perfectly fine, but it's depricated ION.with(getActivity(), URL) So I dont know, where am I making mistake.

    Ion.with(getActivity(), URLStr)
            .addQuery("action", "dummyAction")
            .asJsonObject()
            .setCallback(new FutureCallback<JsonObject>() {
                @Override
                public void onCompleted(Exception e, JsonObject result) {
                    if (e != null) {

                        e.printStackTrace();
                        Toast.makeText(getActivity(), "Error loading user data", Toast.LENGTH_LONG).show();
                        return;
                    }
                    Log.d(Constants.LOG_TAG, "DATA: " + result.toString());
                    String data = result.get("status").getAsString();
                    Log.d(Constants.LOG_TAG, "DATA: " + data);
                }
            });

Below is the verbose output from the first call

    08-12 14:18:06.010  27928-27928/com.abc.xyz: D/ION_VERBOSE_LOGGING﹕ (0 ms) http://urlHidden/service.php?action=dummyAction: preparing request
    08-12 14:18:06.010  27928-27928/com.abc.xyz: I/ION_VERBOSE_LOGGING﹕ (0 ms) http://urlHidden/service.php?action=dummyAction: Using loader: com.koushikdutta.ion.loader.HttpLoader@42d0e6c8
    08-12 14:18:06.025  27928-28187/com.abc.xyz: D/ION_VERBOSE_LOGGING﹕ (0 ms) http://urlHidden/service.php?action=dummyAction: Executing request.
    08-12 14:18:06.040  27928-28187/com.abc.xyz: I/ION_VERBOSE_LOGGING﹕ (14 ms) http://urlHidden/service.php?action=dummyAction: Response may be served from conditional cache
    08-12 14:18:06.040  27928-28187/com.abc.xyz: V/ION_VERBOSE_LOGGING﹕ (16 ms) http://urlHidden/service.php?action=dummyAction: Resolving domain and connecting to all available addresses
    08-12 14:18:06.195  27928-28187/com.abc.xyz: V/ION_VERBOSE_LOGGING﹕ (167 ms) http://urlHidden/service.php?action=dummyAction: socket connected
    08-12 14:18:06.195  27928-28187/com.abc.xyz: V/ION_VERBOSE_LOGGING﹕ (169 ms) http://urlHidden/service.php?action=dummyAction:
        GET /service.php?post=stepOne HTTP/1.1
        Host: 188.226.224.99
        User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.4; GT-N7000 Build/KTU84P)
        Accept-Encoding: gzip, deflate
        Connection: keep-alive
        Accept: */*
        Content-Type: application/json
        If-Modified-Since: Tue, 12 Aug 2014 06:42:34 UTC
    08-12 14:18:06.200  27928-28187/com.abc.xyz: V/ION_VERBOSE_LOGGING﹕ (170 ms) http://urlHidden/service.php?action=dummyAction: request completed
    08-12 14:18:06.355  27928-27928/com.abc.xyz: D/MatchBot﹕ DATA: {"status":"pending"}
    08-12 14:18:06.370  27928-28187/com.abc.xyz: V/ION_VERBOSE_LOGGING﹕ (347 ms) http://urlHidden/service.php?action=dummyAction: Received headers:
        HTTP/1.1 200 OK
        Date: Tue, 12 Aug 2014 08:48:08 GMT
        Server: Apache/2.2.22 (Ubuntu)
        X-Powered-By: PHP/5.3.10-1ubuntu3.11
        Vary: Accept-Encoding
        Content-Encoding: gzip
        Content-Length: 64
        Keep-Alive: timeout=5, max=100
        Connection: Keep-Alive
        Content-Type: text/html
    08-12 14:18:06.380  27928-28187/com.abc.xyz: D/ION_VERBOSE_LOGGING﹕ (355 ms) http://urlHidden/service.php?action=dummyAction: Caching response
    08-12 14:18:06.380  27928-28187/com.abc.xyz: V/ION_VERBOSE_LOGGING﹕ (356 ms) http://urlHidden/service.php?action=dummyAction: Final (post cache response) headers:
        HTTP/1.1 200 OK
        Date: Tue, 12 Aug 2014 08:48:08 GMT
        Server: Apache/2.2.22 (Ubuntu)
        X-Powered-By: PHP/5.3.10-1ubuntu3.11
        Vary: Accept-Encoding
        Content-Encoding: gzip
        Content-Length: 64
        Keep-Alive: timeout=5, max=100
        Connection: Keep-Alive
        Content-Type: text/html
    08-12 14:18:06.380  27928-28187/com.abc.xyz: D/ION_VERBOSE_LOGGING﹕ (356 ms) http://urlHidden/service.php?action=dummyAction: Connection successful
    08-12 14:18:06.385  27928-27928/com.abc.xyz: E/MatchBot﹕ error in posting User basic Info
    08-12 14:18:06.390  27928-28187/com.abc.xyz: D/ION_VERBOSE_LOGGING﹕ (364 ms) http://urlHidden/service.php?action=dummyAction: Recycling keep-alive socket

////////////////////////////////////////////////////////////////

import com.google.gson.annotations.SerializedName;

import java.io.Serializable;
import java.util.Date;

/**
 * Created by riinav on 17/6/14.
 */
public class User implements Serializable {

    @SerializedName("facebook_id")
    private String userFBId;

    @SerializedName("status")
    private String registrationStatus;

    private transient Date registeredOnDate;

    public User() {
    }

    public User(String userFBId, String registrationStatus) {
    this.userFBId = userFBId;
    this.registrationStatus = registrationStatus;
    }

    /* 
     * getters, setters and overidden equals and hashcode methods
     */
}

///////////////////////////////////////////////////////////////////////////

import com.google.gson.annotations.SerializedName;

/**
 * Created by riinav on 27/6/14.
 */
public class UserBasicInfo extends User {//implements Serializable {

    @SerializedName("name")
    private String userName;

    @SerializedName("email")
    private String emailId;

    @SerializedName("gender")
    private String gender;

    @SerializedName("bday")
    private String dob;

    @SerializedName("country")
    private String country;

    @SerializedName("city")
    private String city;

    @SerializedName("self_image")
    private String profilePicUrl;

    @SerializedName("school")
    private String school;

    @SerializedName("college")
    private String college;

    @SerializedName("degree")
    private String qualification;

    @SerializedName("sorientation")
    private String sexualPreference;

    @SerializedName("prefered_age")
    private String preferedAge;

    @SerializedName("prefered_city")
    private String preferedCity;

    public UserBasicInfo() {
    super();
    }

    /* 
     * getters, setters and overidden equals and hashcode methods
     */
}

/////////////////////////////////////// User Model json posted to server

User: {
    "college": "University of Mumbai,",
    "bday": "01/01/1980",
    "email": "dummyEmail@gmail.com",
    "gender": "Male",
    "degree": "college degree",
    "school": "abc High School,",
    "name": "awesome name",
    "status": "PROCESS",
    "facebook_id": "000000000000000" // test fbId
}

 ///////////////// Server expected response 

 onSuccess:
 {"status":"success"}

 OnError:
 {"status":"error MESSAGE"}

解决方案

Pass in the POJO object directly. Ion will automatically serialize it into JSON for you.

这篇关于Android的koush / lib中离子不能发布和接收JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆