使用ANRequest和ANResponse时,AndroidNetworking响应始终返回“ connectionError”

时间:2019-12-05 07:29:11

标签: android okhttp android-networking

在通过AndroidNetworkingANRequest使用ANResponse POST时获取响应时遇到问题。响应始终为connectionError。我有类似的要求,但是我的身体是JSONObject,并且工作正常。但是在这种情况下,我使用的是JSONArray。我也尝试阅读此document,但仍然出现connectionError。请参见下面的代码

 private synchronized String sendTickets(Context context, JSONArray jsonArray) {
    String result = null;

    OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
            .connectTimeout(15, TimeUnit.SECONDS)
            .readTimeout(15, TimeUnit.SECONDS)
            .writeTimeout(15, TimeUnit.SECONDS)
            .build();

    AndroidNetworking.initialize(context, okHttpClient);

    ANRequest request = AndroidNetworking.post(MYURL)
            .addHeaders("Content-Type", "application/json")
            .addHeaders("Prefer", "return=representation")
            .addJSONArrayBody(jsonArray)
            .setPriority(Priority.HIGH)
            .build();

    ANResponse response = request.executeForJSONObject();

    if (response.isSuccess()) {
        result = String.valueOf(response.getResult());
        Log.d(ModuleTags.MAIN(), result);
    } else {
        ANError error = response.getError();
        errorHandler(error);
        result = error.getErrorDetail();
    }
    return result;
}

并通过

调用我的主应用
 new Thread(new Runnable() {
           @Override
           public void run() {
               if(WifiStatus.isWifiEnabled(getContext())){
                   String liveResults = sendTickets(getContext(), jsonArray);
                   Log.i("data",liveResults);
               }
           }
       }).start();

0 个答案:

没有答案