Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 改装API响应中的空键_Android_Json_Api_Retrofit_Retrofit2 - Fatal编程技术网

Android 改装API响应中的空键

Android 改装API响应中的空键,android,json,api,retrofit,retrofit2,Android,Json,Api,Retrofit,Retrofit2,我已经使用改型进行API调用一段时间了。我已经用过很多次了,但我已经有好几天遇到问题了。发生的情况是,我的API调用成功,但是一个JSON键包含一个列表。我使用GSON和pojo类来解析和处理数据,以备将来使用。 这是API响应: { “IsBlockUser”:“0”, “BlockedByUserId”:空, “BlockedMessage”:空, “聊天记录”:[ { “用户名”:“Kunaal Satija”, “用户ID”:“3”, “用户图像”:“380E16D23-6a72-46a

我已经使用改型进行API调用一段时间了。我已经用过很多次了,但我已经有好几天遇到问题了。发生的情况是,我的API调用成功,但是一个JSON键包含一个列表。我使用GSON和pojo类来解析和处理数据,以备将来使用。 这是API响应:
{
“IsBlockUser”:“0”, “BlockedByUserId”:空, “BlockedMessage”:空, “聊天记录”:[
{
“用户名”:“Kunaal Satija”, “用户ID”:“3”, “用户图像”:“380E16D23-6a72-46a4-b5c8-b67a78a3f8a2.jpg”, “留言”:“你好”, “ChatReciverType”:3, “接收者ID”:“3”, “CreatedDate”:“2016年12月13日下午2:23:45”, “PrettyDate”:“2016年12月13日” }] }

这些是我用来解析响应的pojo类:GetChatHistoryResponse.java

public class GetChatHistoryResponse {

@SerializedName("IsBlockUser")
@Expose
private String isBlockUser;
@SerializedName("BlockedByUserId")
@Expose
private Object blockedByUserId;
@SerializedName("BlockedMessage")
@Expose
private Object blockedMessage;
@SerializedName("ChatHistory")
@Expose
private List<ChatHistory> chatHistory = null;

public String getIsBlockUser() {
    return isBlockUser;
}

public void setIsBlockUser(String isBlockUser) {
    this.isBlockUser = isBlockUser;
}

public Object getBlockedByUserId() {
    return blockedByUserId;
}

public void setBlockedByUserId(Object blockedByUserId) {
    this.blockedByUserId = blockedByUserId;
}

public Object getBlockedMessage() {
    return blockedMessage;
}

public void setBlockedMessage(Object blockedMessage) {
    this.blockedMessage = blockedMessage;
}

public List<ChatHistory> getChatHistory() {
    return chatHistory;
}

public void setChatHistory(List<ChatHistory> chatHistory) {
    this.chatHistory = chatHistory;
}}
这是我使用的改装客户端:

    import com.edurev.Application;
import com.edurev.BuildConfig;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 *
 * Dated: 27-09-2016.
 */
public class RestClient {
    private static Retrofit retrofit = null;
    static Integer BKS_KEYSTORE_RAW_FILE_ID = 0;
    // Integer BKS_KEYSTORE_RAW_FILE_ID = R.raw.keystorebks;
    static Integer SSL_KEY_PASSWORD_STRING_ID = 0;
    //Integer SSL_KEY_PASSWORD_STRING_ID = R.string.sslKeyPassword;

    /**
     * @return
     */
    public static ApiInterface getApiInterface() {
        if (retrofit == null) {
            try {
                retrofit = new Retrofit.Builder()
                        .baseUrl(BuildConfig.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .client(httpClient().build())
                        //.client(secureConnection().build())
                        .build();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return retrofit.create(ApiInterface.class);
    }


    /**
     * @return
     */
    static Retrofit getRetrofitBuilder() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BuildConfig.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(httpClient().build())
                    .build();
        }
        return retrofit;
    }

    /**
     * @return
     */
    private static OkHttpClient.Builder httpClient() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        // set your desired log level
        //logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);
        if(BuildConfig.DEBUG)
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
                .readTimeout(30, TimeUnit.SECONDS)
                .connectTimeout(30, TimeUnit.SECONDS);

        // add your other interceptors …
        // add logging as last interceptor
//        httpClient.addInterceptor(logging);
        return httpClient;
    }

    private static OkHttpClient.Builder secureConnection() throws
            KeyStoreException, CertificateException, NoSuchAlgorithmException,
            IOException, KeyManagementException {
        InputStream certificateInputStream = null;
        certificateInputStream = Application.mContext.getResources().openRawResource(BKS_KEYSTORE_RAW_FILE_ID);
        KeyStore trustStore = KeyStore.getInstance("BKS");
        try {
            trustStore.load(certificateInputStream, Application.mContext.getApplicationContext().getString(SSL_KEY_PASSWORD_STRING_ID).toCharArray());
        } finally {
            certificateInputStream.close();
        }
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
        tmf.init(trustStore);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tmf.getTrustManagers(), null);

        /*

        retrofit 1.9.x

        OkHttpClient client = new OkHttpClient();

        client.setSslSocketFactory(sslContext.getSocketFactory());

        client.setWriteTimeout(15, TimeUnit.MINUTES);

        client.setConnectTimeout(15, TimeUnit.MINUTES); // connect

        timeout

        client.setReadTimeout(15, TimeUnit.MINUTES);

        return new OkClient(client);*/

        //Retrofit 2.0.x

        TrustManager[] trustManagers = tmf.getTrustManagers();
        if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
            throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
        }
        X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        OkHttpClient.Builder client3 = new OkHttpClient.Builder().sslSocketFactory(sslSocketFactory, trustManager);
        return client3;
    }
}
这是API接口方法和我使用的调用方法:

`@FormUrlEncoded
 @POST("Chat_GetChatHistory")
 Call<GetChatHistoryResponse> getChatHistory(@FieldMap HashMap<String, String> map);

RestClient.getApiInterface().getChatHistory(mCommonParams.getMap()).enqueue(new Callback<GetChatHistoryResponse>() {
        @Override
        public void onResponse(Call<GetChatHistoryResponse> call, Response<GetChatHistoryResponse> response) {

        }

        @Override
        public void onFailure(Call<GetChatHistoryResponse> call, Throwable t) {

        }
    });
`@FormUrlEncoded
@POST(“聊天记录”)
调用getChatHistory(@FieldMap HashMap-map);
RestClient.getApiInterface().getChatHistory(mCommonParams.getMap()).enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
`

现在,我收到的API响应中的ChatHistory列表为空,但其他键返回得很好。我尝试使用ResponseBody数据类型,以便在出现任何解析错误时跳过解析。但是当我使用web界面调用API时,结果很好,我似乎无法发现问题所在。 下面是一个调试器响应,其中ResponseBody设置为API接口中的数据类型:[size=81 text={“IsBlockUser”:“0”,“BlockedByUserId”:null,“BlockedMessage”:null,]


任何帮助都将不胜感激!

使用另一个名称(如Chat)重命名ChatHistory类。然后使用List ChatHistory=new ArrayList()实例化List;感谢您回答@ImenNmn,但它不起作用我为您感到抱歉:/检查这些问题:它们可能有助于您如何解决问题?@ashishbahl您是如何解决问题的?
`@FormUrlEncoded
 @POST("Chat_GetChatHistory")
 Call<GetChatHistoryResponse> getChatHistory(@FieldMap HashMap<String, String> map);

RestClient.getApiInterface().getChatHistory(mCommonParams.getMap()).enqueue(new Callback<GetChatHistoryResponse>() {
        @Override
        public void onResponse(Call<GetChatHistoryResponse> call, Response<GetChatHistoryResponse> response) {

        }

        @Override
        public void onFailure(Call<GetChatHistoryResponse> call, Throwable t) {

        }
    });