java.lang.string无法转换为JSONArray Android Mysql
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java.lang.string无法转换为JSONArray Android Mysql相关的知识,希望对你有一定的参考价值。
我正在尝试推送Book On Not的通知今天为我的库移动应用程序,但在我登录后,我的应用程序中弹出此错误。
4:运行java.lang.string无法转换为JSONArray。
我的php工作正常,但我该如何解决这个错误?
W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
W/System.err: at org.json.JSONArray.<init>(JSONArray.java:96)
W/System.err: at org.json.JSONArray.<init>(JSONArray.java:108)
W/System.err: at com.example.ivan.librarymobileapp.backWorkerNotif.doInBackground(backWorkerNotif.java:67)
W/System.err: at com.example.ivan.librarymobileapp.backWorkerNotif.doInBackground(backWorkerNotif.java:23)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err: at java.lang.Thread.run(Thread.java:818)
这是我的代码:
backWorkerNotif类
public class backWorkerNotif extends AsyncTask<String, String, String> {
String result = null;
String[] BookTittle;
Context context;
public backWorkerNotif(Context context){this.context = context;}
@Override
protected String doInBackground(String... params) {
String selectNotif_url = "http://192.168.254.120/LibrayAPI/SelectNotif.php";
String type = params[0];
if (type.equals("Notif")) {
String DateNow = params[1];
String BorrowerID = params[2];
try {
String data = URLEncoder.encode("date_now", "UTF-8") + "=" +
URLEncoder.encode(DateNow, "UTF-8");
data += "$" + URLEncoder.encode("borrower_id", "UTF-8") + "=" +
URLEncoder.encode(BorrowerID, "UTF-8");
URL url = new URL(selectNotif_url);
URLConnection urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(urlConnection.getOutputStream());
outputStreamWriter.write(data);
outputStreamWriter.flush();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line + "
");
}
result = sb.toString();
} catch (Exception ex) {
}
//Json
try {
JSONArray jsonArray = new JSONArray(result);
JSONObject jsonObject;
BookTittle = new String[jsonArray.length()];
for (int i = 0;i< jsonArray.length();i++){
jsonObject = jsonArray.getJSONObject(i);
BookTittle[i] = jsonObject.getString("BookTittle");
NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_book_black_24dp)
.setContentTitle("Library notification")
.setContentText("Today is the Due Day of"+ BookTittle[i] +"You Borrowed");
NotificationManager notificationManager =(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(0,mbuilder.build())
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String jsonArray) {
super.onPostExecute(jsonArray);
}
}
登录类
public class Login extends AppCompatActivity {
EditText username,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = findViewById(R.id.edtUsername);
password = findViewById(R.id.edtPassword);
}
public void onLogin(View view) {
String Username = username.getText().toString();
String Password = password.getText().toString();
backWorkerNotif backWorkerNotif = new backWorkerNotif(this);
Date date = Calendar.getInstance().getTime();
SimpleDateFormat SF = new SimpleDateFormat("yyyy-MM-dd");
String DateNow = SF.format(date);
backWorkerNotif.execute("Notif", DateNow, Username);
String Type = "login";
GlobalVariable.BorrowerID = Username;
backgroundWorker _backgroundWorker = new backgroundWorker(this);
_backgroundWorker.execute(Type, Username, Password);
}
}
我的PHP代码
<?php
require "Connection.php";
$DateNow = $_POST["date_now"];
$BorrowerID = $_POST["borrower_id"];
$query = "SELECT A.`BookId`,B.`BookTittle`,A.`BorrowerId` FROM `tblborrow` AS A INNER JOIN `tblbooks` AS B ON A.`BookId` = B.`BookId` WHERE `DueDate` = '$DateNow' AND `BorrowerId` = '$BorrowerID'";
$result = mysqli_query($conn,$query);
while($e = mysqli_fetch_assoc($result)){
$output[] = $e;
}
echo json_encode($output);
print (json_encode($output));
?>
我的PHP的结果
[
{"BookId":"101","BookTittle":"Counting dolars","BorrowerId":"11"},
{"BookId":"9876","BookTittle":"ihi","BorrowerId":"11"},
{"BookId":"542","BookTittle":"hiragana","BorrowerId":"11"},
{"BookId":"5421","BookTittle":"wow","BorrowerId":"11"}
][
{"BookId":"101","BookTittle":"Counting dolars","BorrowerId":"11"},
{"BookId":"9876","BookTittle":"ihi","BorrowerId":"11"},
{"BookId":"542","BookTittle":"hiragana","BorrowerId":"11"},
{"BookId":"5421","BookTittle":"wow","BorrowerId":"11"}
]
我做错了什么?
正如Exception所说,你收到的<br
值不是json格式。
将“application / json”作为请求内容类型的标题。但这并不能保证您的代码始终有效。通过这样做,您只需确保您将以json格式接收响应。
问题是关于输出流的格式。如果您的连接获得任何类型的HTTP异常或其他类型的错误,您将无法知道,因为您吞下它。以这种方式改变它:
try {
String data = URLEncoder.encode("date_now", "UTF-8") + "=" +
URLEncoder.encode(DateNow, "UTF-8");
data += "$" + URLEncoder.encode("borrower_id", "UTF-8") + "=" +
URLEncoder.encode(BorrowerID, "UTF-8");
URL url = new URL(selectNotif_url);
URLConnection urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(urlConnection.getOutputStream());
outputStreamWriter.write(data);
outputStreamWriter.flush();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line + "
");
}
result = sb.toString();
//JSON
JSONArray jsonArray = new JSONArray(result);
JSONObject jsonObject;
BookTittle = new String[jsonArray.length()];
for (int i = 0;i< jsonArray.length();i++){
jsonObject = jsonArray.getJSONObject(i);
BookTittle[i] = jsonObject.getString("BookTittle");
NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_book_black_24dp)
.setContentTitle("Library notification")
.setContentText("Today is the Due Day of"+ BookTittle[i] +"You Borrowed");
NotificationManager notificationManager =(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(0,mbuilder.build());
}
} catch (Exception ex) {
ex.printStackTrace();
}
请向我们提供您从php收到的样品输出。我的意思是result
值
以上是关于java.lang.string无法转换为JSONArray Android Mysql的主要内容,如果未能解决你的问题,请参考以下文章
JSON 异常:值 <? java.lang.String 类型的无法转换为 JSONObject
org.json.JSONException:java.lang.String 类型的值连接无法转换为 JSONArray
java.lang.String 无法从 Okhttp 转换为 JSONArray
org.json.JSONException:java.lang.String 类型的值 <br 无法转换为 JSONObject