在通过AndroidNetworking
和ANRequest
使用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();