Android koush/ion lib 无法发布和接收 JSON 响应

标签 android android-networking android-ion ion-koush

我正在使用 ION 获取/发布 REST 并将图像放入 ListView 但我无法使用 POST 方法并以 JSON 格式接收响应 我使用的 ION 版本是 1.3.7。在模拟器和真实设备 4.1.1、4.2.2 和 4.4 上测试

ION Link on gitHub

此代码在验证成功后单击按钮以发布到服务器时调用。这是在 gradle 中定义的内部支持 fragment v4:20

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);
        }
    }
    });

我得到的结果是 NullPointer。 请求JSON也是有效的,我是用GSON构造的。 响应 JSON 有效,因为我使用 POSTMAN chrome 应用检查了它。

然而,下面的代码工作得很好,但它被 depricated ION.with(getActivity(), URL) 所以我不知道,我在哪里犯了错误。

    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);
                }
            });

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

    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”}

最佳答案

直接传入POJO对象。 Ion 会自动为您将其序列化为 JSON。

关于Android koush/ion lib 无法发布和接收 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25153340/

相关文章:

iOS "calculationMode"等效的 Android 动画

android - 将值从 Activity 传递到广播接收器并从广播接收器启动服务

android - 在 Android 创建的 aac 文件上以流模式使用 ffmpeg 时出现问题

java.lang.IllegalStateException : Cannot access request header fields after connection is set 错误

java - 使用网络资源进行货币转换器

java - 如何从 ion 返回全局变量?

java - ionic 库中的 future 如何处理?

android actionbar 在超大屏幕的标签下方

打瞌睡模式下的 Android VOIP 应用程序行为

java - 当我使用 ion 库加载图像时 RecyclerView 元素重复