我的Retrofit
应该会收到一个书签列表,并且在我使用WAMP
服务器时一切正常。当我将服务器更改为外部服务器时(没有其他更改,只是服务器的IP地址并检索了其他所有内容),我遇到了一个错误:
java.io.EOFException: End of input at line 1 column 1
at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1407)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:553)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:429)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:74)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:116)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:106)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135)
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)
我的改装代码:
public void init() {
OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
HttpLoggingInterceptor debugger =
new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient
.addInterceptor(debugger);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient.build())
.build();
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
String email = pref.getString(Constants.EMAIL, "");
System.out.println(email);
String id_group = pref.getString(Constants.ID_GROUP, "");
System.out.println(id_group);
String nazwa = pref.getString(Constants.NAZWA, "");
Integer id_int_group = Integer.parseInt(id_group);
Bookmark bookmark = new Bookmark(email, id_int_group, nazwa);
ServerRequest request2 = new ServerRequest();
request2.setOperation(Constants.GET_MY_GROUPS);
request2.setBookmark(bookmark);
Call<List<Bookmark>> response2 = requestInterface.operation2(request2);
response2.enqueue(new Callback<List<Bookmark>>() {
@Override
public void onResponse(Call<List<Bookmark>> call, retrofit2.Response<List<Bookmark>> response2) {
listOfBookmarks = response2.body();
bookmarkToString();
simpleAdapter.notifyDataSetChanged(); // refresh listivew
}
@Override
public void onFailure(Call<List<Bookmark>> call, Throwable t) {
Log.d(Constants.TAG, "Nie zaladowano!", t);
}
});
}
EDIT://PHP代码:
<?php
class Bookmark {
private $host = 'localhost';
private $user = 'nwbrn_root';
private $db = 'nwbrn_app';
private $pass = 'zxs@1208NMLK';
private $conn;
public function __construct() {
$this -> conn = new PDO("mysql:host=".$this -> host.";dbname=".$this -> db, $this -> user, $this -> pass);
}
public function checkBookmarkExist($email, $id_group){
try {
$query = $this->conn->prepare("SELECT COUNT(*) from bookmarks WHERE email =:email AND id_group =:id_group");
// $query = $this -> conn -> prepare($sql);
$query->bindParam(':email', $email, PDO::PARAM_STR);
$query->bindParam(':id_group', $id_group, PDO::PARAM_INT);
$query->execute(array('email' => $email, 'id_group' => $id_group));
$row_count = $query -> fetchColumn();
if ( $row_count>0 ) {
$response["result"] = "success";
$response["message"] = "Your favourite!";
return json_encode($response);
}
else {
$response["result"] = "failure";
$response["message"] = "Not in your favourite!";
return json_encode($response);
}
} catch (PDOException $e) {
die ($e->getMessage());
}
}
public function fullStarSelected($email, $id_group, $nazwa){
try {
$query = $this->conn->prepare("DELETE from bookmarks WHERE email =:email AND id_group =:id_group AND nazwa =:nazwa");
// mysqli_set_charset($this->conn, "utf8");
$query->bindParam(':email', $email, PDO::PARAM_STR);
$query->bindParam(':id_group', $id_group, PDO::PARAM_INT);
$query->bindParam(':nazwa', $nazwa, PDO::PARAM_STR);
$query->execute();
if ( $query ->rowCount() > 0 ) {
$response["result"] = "failure";
$response["message"] = "Row not deleted!";
return json_encode($response);
}
else {
$response["result"] = "success";
$response["message"] = "Row deleted successfully!";
return json_encode($response);
}
} catch (PDOException $e) {
die ($e->getMessage());
}
}
public function blankStarSelected($email, $id_group, $nazwa){
try {
$query = $this->conn->prepare("INSERT INTO bookmarks (email, id_group, nazwa) VALUES (:email, :id_group, :nazwa)");
// mysqli_set_charset($this->conn, "utf8");
$query->bindParam(':email', $email, PDO::PARAM_STR);
$query->bindParam(':id_group', $id_group, PDO::PARAM_INT);
$query->bindParam(':nazwa', $nazwa, PDO::PARAM_STR);
$query->execute();
if (!$query) {
printf("Error: %s\n", mysqli_error($this->conn));
exit();
}
$result = array();
// $query1 = $this->conn->prepare("SELECT COUNT(*) from bookmarks WHERE email =:email AND id_group =:id_group LIMIT 1");
if ( $query->rowCount() > 0 ) {
$response["result"] = "success";
$response["message"] = "Row added successfully!";
return json_encode($response);
}
else {
$response["result"] = "failure";
$response["message"] = "Row not added!";
return json_encode($response);
}
} catch (PDOException $e) {
die ($e->getMessage());
}
}
public function getMyGroups($email, $id_group){
try {
$con = mysqli_connect($this->host,$this->user,$this->pass,$this->db);
$sql = "SELECT * FROM bookmarks WHERE email = '$email'";
$res = mysqli_query($con,$sql);
$result = array();
if (!$res) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
while($row = mysqli_fetch_array($res)){
$temp = array();
$temp['id_group']=$row['id_group'];
$temp['email']=$row['email'];
$temp['nazwa']=$row['nazwa'];
array_push($result,$temp);
}
echo json_encode($result);
} catch (PDOException $e) {
die ($e->getMessage());
}
}
}
看到时
java.io.EOFException:第1行第1列的输入结束
它表示解析某些内容时出现问题。希望可以解析一些文本,但文件结尾(EOF)。
然后您说:
当我将服务器更改为外部服务器时(没有其他更改)
如果这以前可行,那么您的问题绝对不会在代码上,而是在您要检索的数据上。您的新服务器正在拒绝您的请求或返回空白数据。尝试手动执行相同的请求(通过postman或其他一些api客户端),然后查看响应是什么。它很可能会告诉您错误在哪里。
我有此错误,因为答案的正文传递的是空文本“”,我通过将其传递为null来解决它,因为正在使用某些依赖于正文值的方法。当值为空时,不进行必要的检查,而是使用主体的空值,以空为条件来限制流程。
在声明中,我继续默认定义一个空值fun response(httpCode: Int, body: String? = null): Response<T>
在调用中,我从Request#response(200, "")
转到了Request#response(200)
根据http代码协议,只有204可以允许主体为空,尽管这可能会根据一个人的需求而有所不同。