您好,下面的代码描述了服务器的json请求和json响应,我们将按钮命名为ADD,如果您按下ADD按钮,则将名称和级别作为请求发送,并根据请求获取状态来自服务器。
我在执行错误的地方找不到错误。我没有从服务器得到任何响应。
Add.java:
Add.setOnClickListener (new View.OnClickListener ( ) {
@Override
public void onClick(View v) {
final String name = et_building.getText().toString();
final String level = "1";
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API.URL_BASE)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
API service = retrofit.create(API.class);
try{
JSONObject parmobject=new JSONObject ();
parmobject.put("name",name);
parmobject.put("level",level);
Call<NewBuilding> userCall = service.getbuildinglist (parmobject.toString ());
userCall.enqueue(new Callback<NewBuilding> () {
@Override
public void onResponse(Call<NewBuilding> call, Response<NewBuilding> response) {
if (response != null && response.code ()==200) {
JSONObject obj = null;
try {
obj = new JSONObject (String.valueOf (response));
status=obj.getJSONObject (name).toString ();
} catch (JSONException e) {
Log.e("My App", "Could not parse malformed JSON:");
}
if(status.equalsIgnoreCase ("success")){
makeText(getActivity (), "Building successfully Added", Toast.LENGTH_SHORT).show();
arrayList.add (name);
adapter.notifyDataSetChanged ();
}
} else {
makeText(getActivity (), "Please try again for Adding New Building", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<NewBuilding> call, Throwable t) {
t.printStackTrace ();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
});
POJO类:
NewBuilding.java:
public class NewBuilding {
@SerializedName ("status")
@Expose
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
Logcat错误:
W/System.err: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
W/System.err: at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1568)
at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1409)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:593)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:425)
W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:205)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:39)
W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:223)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:121)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206)
W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)