[英]How to solve java.io.EOFException: End of input at line 1 column 1 path 1 in retrofit service call in android?
I am new to android.我是安卓新手。 I am not getting why this error is coming.
我不明白为什么会出现此错误。 Error: java.io.EOFException: End of input at line 1 column 1 path $ W/System.err:at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1393) Please help.
错误:java.io.EOFException:第 1 行第 1 列路径输入结束 $ W/System.err:at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1393) 请帮忙。 This is my code: 1. Pojo
这是我的代码: 1. Pojo
class ProfileEntity implements Parcelable {
@SerializedName("tag")
@Expose
private String tag;
@SerializedName("success")
@Expose
private Integer success;
@SerializedName("error")
@Expose
private Integer error;
@SerializedName("studentId")
@Expose
private String studentId;
@SerializedName("pic_id")
@Expose
private String picId;
@SerializedName("instUrl")
@Expose
private String instUrl;
@SerializedName("course_name")
@Expose
private String courseName;
@SerializedName("batch_name")
@Expose
private String batchName;
@SerializedName("semester_name")
@Expose
private Object semesterName;
@SerializedName("section_name")
@Expose
private String sectionName;
@SerializedName("first_name")
@Expose
private String firstName;
@SerializedName("last_name")
@Expose
private String lastName;
@SerializedName("username")
@Expose
private String username;
@SerializedName("groupname")
@Expose
private String groupname;
@SerializedName("mobile")
@Expose
private String mobile;
@SerializedName("email")
@Expose
private String email;
@SerializedName("dob")
@Expose
private String dob;
@SerializedName("address")
@Expose
private String address;
@SerializedName("dbname")
@Expose
private String dbname;
@SerializedName("Branch_id")
@Expose
private String branchId;
@SerializedName("org_id")
@Expose
private String orgId;
@SerializedName("cyear")
@Expose
private String cyear;
@SerializedName("url")
@Expose
private String url;
@SerializedName("password")
@Expose
private String password;
public final static Creator<ProfileEntity> CREATOR = new Creator<ProfileEntity>() {
@SuppressWarnings({
"unchecked"
})
public ProfileEntity createFromParcel(Parcel in) {
return new ProfileEntity(in);
}
public ProfileEntity[] newArray(int size) {
return (new ProfileEntity[size]);
}
};
protected ProfileEntity(Parcel in) {
this.tag = ((String) in.readValue((String.class.getClassLoader())));
this.success = ((Integer) in.readValue((Integer.class.getClassLoader())));
this.error = ((Integer) in.readValue((Integer.class.getClassLoader())));
this.studentId = ((String) in.readValue((String.class.getClassLoader())));
this.picId = ((String) in.readValue((String.class.getClassLoader())));
this.instUrl = ((String) in.readValue((String.class.getClassLoader())));
this.courseName = ((String) in.readValue((String.class.getClassLoader())));
this.batchName = ((String) in.readValue((String.class.getClassLoader())));
this.semesterName = ((Object) in.readValue((Object.class.getClassLoader())));
this.sectionName = ((String) in.readValue((String.class.getClassLoader())));
this.firstName = ((String) in.readValue((String.class.getClassLoader())));
this.lastName = ((String) in.readValue((String.class.getClassLoader())));
this.username = ((String) in.readValue((String.class.getClassLoader())));
this.groupname = ((String) in.readValue((String.class.getClassLoader())));
this.mobile = ((String) in.readValue((String.class.getClassLoader())));
this.email = ((String) in.readValue((String.class.getClassLoader())));
this.dob = ((String) in.readValue((String.class.getClassLoader())));
this.address = ((String) in.readValue((String.class.getClassLoader())));
this.dbname = ((String) in.readValue((String.class.getClassLoader())));
this.branchId = ((String) in.readValue((String.class.getClassLoader())));
this.orgId = ((String) in.readValue((String.class.getClassLoader())));
this.cyear = ((String) in.readValue((String.class.getClassLoader())));
this.url = ((String) in.readValue((String.class.getClassLoader())));
this.password = ((String) in.readValue((String.class.getClassLoader())));
}
public ProfileEntity() {}
public String getCyear() {
return cyear;
}
public void setCyear(String cyear) {
this.cyear = cyear;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getOrgId() {
return orgId;
}
public void setOrgId(String orgId) {
this.orgId = orgId;
}
public String getBranchId() {
return branchId;
}
public void setBranchId(String branchId) {
this.branchId = branchId;
}
public String getDbname() {
return dbname;
}
public void setDbname(String dbname) {
this.dbname = dbname;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public Integer getSuccess() {
return success;
}
public void setSuccess(Integer success) {
this.success = success;
}
public Integer getError() {
return error;
}
public void setError(Integer error) {
this.error = error;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getPicId() {
return picId;
}
public void setPicId(String picId) {
this.picId = picId;
}
public String getInstUrl() {
return instUrl;
}
public void setInstUrl(String instUrl) {
this.instUrl = instUrl;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getBatchName() {
return batchName;
}
public void setBatchName(String batchName) {
this.batchName = batchName;
}
public Object getSemesterName() {
return semesterName;
}
public void setSemesterName(Object semesterName) {
this.semesterName = semesterName;
}
public String getSectionName() {
return sectionName;
}
public void setSectionName(String sectionName) {
this.sectionName = sectionName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getGroupname() {
return groupname;
}
public void setGroupname(String groupname) {
this.groupname = groupname;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(tag);
dest.writeValue(success);
dest.writeValue(error);
dest.writeValue(studentId);
dest.writeValue(picId);
dest.writeValue(instUrl);
dest.writeValue(courseName);
dest.writeValue(batchName);
dest.writeValue(semesterName);
dest.writeValue(sectionName);
dest.writeValue(firstName);
dest.writeValue(lastName);
dest.writeValue(username);
dest.writeValue(groupname);
dest.writeValue(mobile);
dest.writeValue(email);
dest.writeValue(dob);
dest.writeValue(address);
dest.writeValue(dbname);
dest.writeValue(branchId);
dest.writeValue(orgId);
dest.writeValue(cyear);
dest.writeValue(url);
dest.writeValue(password);
}
public int describeContents() {
return 0;
}
}
ApiInterface Api接口
@Headers("Content-Type: application/json") @POST("profile.php") Call<ProfileEntity> getProfile(@Body ProfileEntity profileEntity);
3.Impl 3.实施
@Override
public void profileCall(ProfileEntity profileEntity) {
ApiInterface controller = ApiClient.getClient(null).create(ApiInterface.class);
Call<ProfileEntity> call = controller.getProfile(profileEntity);
call.enqueue(new Callback<ProfileEntity>() {
@Override
public void onResponse(Call<ProfileEntity> call, Response<ProfileEntity> response) {
if (response.body() != null) {
ProfileEntity entity = response.body();
if (entity.getSuccess().toString().equalsIgnoreCase("1")) {
profilePresentor.onProfileSuccess(entity);
} else {
profilePresentor.onLogFailedMessage(response.message());
}
} else {
profilePresentor.onLogFailedMessage(response.message());
}
}
@Override
public void onFailure(Call<ProfileEntity> call, Throwable t) {
Log.d("FAILURE....",t.toString());
t.printStackTrace();
profilePresentor.onProfileFailure(HttpURLConnection.HTTP_INTERNAL_ERROR,"Oops! Something went wrong. Please try again");
}
});
}
}
4.ApiClient 4.API客户端
class ApiClient {
public static String BASE_URL = "http://xxxx.xxxxxx.com/appAPI_v2/";
static Retrofit retrofit = null;
public static Retrofit getClient(Map<String, String> headerMap) {
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit.Builder builder = new Retrofit.Builder();
builder.baseUrl(BASE_URL);
builder.addConverterFactory(GsonConverterFactory.create(gson));
if (headerMap != null) {
builder.client(getHeader(headerMap));
} else {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.readTimeout(60, TimeUnit.SECONDS);
httpClient.connectTimeout(60, TimeUnit.SECONDS);
if (BuildConfig.DEBUG)
httpClient.addInterceptor(interceptor);
builder.client(httpClient.build());
}
retrofit = builder.build();
return retrofit;
}
@NonNull
private static OkHttpClient getHeader(final Map<String, String> header) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request.Builder builder = original.newBuilder();
for (Map.Entry<String, String> entry : header.entrySet()) {
builder.addHeader(entry.getKey(), entry.getValue());
}
builder.method(original.method(), original.body());
return chain.proceed(builder.build());
}
});
if (BuildConfig.DEBUG) //hides exposing web service call in logcat when app is released
httpClient.addInterceptor(interceptor);
return httpClient.build();
}
}
Thanks in advance.提前致谢。
I have the same problem, There is nothing wrong with your code and your data.我有同样的问题,你的代码和数据都没有问题。 The problem is in your server file (.php).
问题出在您的服务器文件 (.php) 中。 You must add this under the configuration file
你必须在配置文件下添加这个
if ($_SERVER['REQUEST_METHOD'] === 'POST') { // your query }
so your file must be like this所以你的文件必须是这样的
<?php
require "koneksi.php";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST["name"];
mysqli_query($koneksi,"insert into user (name) VALUES ('$name')");
} ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.