java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第2列路径$处为BEGIN_OBJECT 对于下面的代码,我得到了错误。 在此之前,我收到了套接字超时错误,为此我添加了计时器。
响应不会来。每次仅执行失败条件。任何人都可以帮助我在哪里做错
OnOFF.setOnToggledListener (new OnToggledListener ( ) {
@Override
public void onSwitched(LabeledSwitch labeledSwitch, boolean isOn) {
String lightID="";
String lightType="";
String level="";
String Status="";
if(isOn) {
final ProgressDialog progressDialog = new ProgressDialog (getActivity ());
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Authenticating...");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setCancelable(false);
progressDialog.show();
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API.URL_BASE)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client (client)
.build();
API service = retrofit.create (API.class);
lightID = "1234";
lightType = "1";
level = "5";
try {
if (OnOFF.isOn ( )) {
Status = "1";
} else {
Status = "0";
}
JSONObject parmobject = new JSONObject ( );
parmobject.put ("Status", Status);
parmobject.put ("lightID", lightID);
parmobject.put ("lightType", lightType);
parmobject.put ("level", level);
Call<OnOffStatusList> userCall = service.getMyJSON (parmobject.toString ());
userCall.enqueue(new Callback<OnOffStatusList> () {
@Override
public void onResponse(Call <OnOffStatusList> call, Response <OnOffStatusList> response) {
if(response!=null && response.code ()==200){
Log.d("Call request", call.request().toString());
Log.d("Call request header", call.request().headers().toString());
Log.d("Response raw header", response.headers().toString());
Log.d("Response raw", String.valueOf(response.raw().body()));
Log.d("Response code", String.valueOf(response.code()));
if(response.isSuccessful()) {
//the response-body is already parseable to your ResponseBody object
Log.d("Call request", call.request().toString());
Log.d("Call request header", call.request().headers().toString());
Log.d("Response raw header", response.headers().toString());
Log.d("Response raw", String.valueOf(response.raw().body()));
Log.d("Response code", String.valueOf(response.code()));
if(response.isSuccessful()) {
String Status=new Gson().toJson(response.body());
Log.d("Status", Status);
luminary.setImageDrawable (ContextCompat.getDrawable (getContext ( ), R.drawable.luminaryon));
}
else {
Log.d("Response errorBody", String.valueOf(response.errorBody()));
}
}
System.out.println(response.body ());
}
}
@Override
public void onFailure(Call<OnOffStatusList> call, Throwable t) {
System.out.println("onFailure");
System.out.println(t.fillInStackTrace());
Toast.makeText(getActivity (), "Some error occurred -> ", Toast.LENGTH_LONG).show();;
// progressDialog.dismiss();
}
});
}
catch (JSONException e) {
e.printStackTrace ( );
}
return;
}
}
});
OnOffStatusList.java:
公共类OnOffStatusList {
@SerializedName("status")
private List <Status> status;
public List<Status> getStatus() {
return status;
}
public void setStatus(List<Status> status) {
this.status = status;
}
}
Status.java:
公共类状态{
@SerializedName("id")
private String id;
@SerializedName("status")
private Integer status;
@SerializedName("val")
private String val;
public String getVal() {
return val;
}
public void setVal(String val) {
this.val = val;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
答案 0 :(得分:0)
在所有项目中(全局)使用相同的改造实例,不要每次都初始化它:
将此添加到“活动”或“应用程序”类中:
final API service;
public API getAPI(){
if(service == null) {
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API.URL_BASE)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(API.class);
}
return service;
}
将其称为getAPI()以调用API:
OnOFF.setOnToggledListener (new OnToggledListener ( ) {
@Override
public void onSwitched(LabeledSwitch labeledSwitch, boolean isOn) {
String lightID="";
String lightType="";
String level="";
String Status="";
if(isOn) {
final ProgressDialog progressDialog = new ProgressDialog (getActivity ());
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Authenticating...");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setCancelable(false);
progressDialog.show();
lightID = "1234";
lightType = "1";
level = "5";
List<OnOffStatusList> onOffStatusListList=new ArrayList <> ();
try {
if (OnOFF.isOn ( )) {
Status = "1";
} else {
Status = "0";
}
JSONObject parmobject = new JSONObject ( );
parmobject.put ("Status", Status);
parmobject.put ("lightID", lightID);
parmobject.put ("lightType", lightType);
parmobject.put ("level", level);
Call<List<OnOffStatusList>> userCall = getAPI().getMyJSON (parmobject.toString ());
userCall.enqueue(new Callback<List<OnOffStatusList>> () {
@Override
public void onResponse(Call<List<OnOffStatusList>> userCall, Response<List<OnOffStatusList>> response) {
Log.d ("response",response.body ().toString ());
if (response != null && response.code ()==200) {
Integer status=onoffStatusList.get (1).getStatus();
String val=onoffStatusList.get (2).getVal();
if(status==1) {
luminary.setImageDrawable (ContextCompat.getDrawable (getContext ( ), R.drawable.luminaryon));
}
else {
luminary.setImageDrawable (ContextCompat.getDrawable (getContext ( ), R.drawable.luminaryoff));
}
}
}
@Override
public void onFailure(Call<List<OnOffStatusList>> call, Throwable t) {
// Toast.makeText(LoginActivity.this, "Some error occurred -> ", Toast.LENGTH_LONG).show();;
// dialog.dismiss();
}
});
} catch (JSONException e) {
e.printStackTrace ( );
}
}
}
});
答案 1 :(得分:0)
有3个呼叫失败的问题