현재 데이터베이스에서 특정 사람의 운동 기록정보를 가져올려고 합니다.
그런데org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject에러가 떠서요.. 좀만 도와주시면 감사하겠습니다.
[hashmap을 통한 php전달]
package com.example.myhealthdiary;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class HealthDataRequest extends StringRequest {
final static private String URL =
final static private String URL2 =
final static private String URL3 =
private Map<String, String> map;
public HealthDataRequest(String userId, Response.Listener<String> listener) {
super(Request.Method.POST, URL3, listener, null);
map = new HashMap<>();
map.put("userId",userId);
System.out.println("3번째 메소드 실행됨");
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return map;
}
}
[Volly 서버 전달 코드]
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray results = jsonObject.getJSONArray("result");
System.out.println(results);
for(int i = 0; i < jsonObject.length(); i++) {
JSONObject temp = results.getJSONObject(i);
String time = (String) temp.get("time");
String memo = (String) temp.get("memo");
String routine = (String) temp.get("routine");
String date = (String) temp.get("dates");
String title = jsonObject.getString("title");
HealthLog healthLog = new HealthLog(title, date, time, memo, routine);
Intent intent = new Intent(HealthLogListScreen.this, HealthLogAddScreen.class);
intent.putExtra("log_list", log_list);
startActivityForResult(intent, add_request_code);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
// 서버로 데이터 전달
userId = LoginScreen.user;
HealthDataRequest hdr = new HealthDataRequest(userId, responseListener);
RequestQueue queue = Volley.newRequestQueue(HealthLogListScreen.this);
queue.add(hdr);
[PHP코드]
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$host = "localhost";
$dbUser = "root";
$db_pw = "";
$db_name = "healthdiary";
$connection = mysqli_connect("$host", "$dbUser", "$db_pw", "$db_name");
mysqli_query($connection, 'SET NAMES utf8');
$userId = $_POST["userId"];
$statement = mysqli_prepare($connection, "SELECT * FROM healthlog WHERE userId = ?");
mysqli_stmt_bind_param($statement, "s", $userId);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)) {
$response["success"] = true;
$response["userId"] = $userId;
$response["time"] = $time;
$response["memo"] = $memo;
$response["routine"] = $routine;
$response["dates"] = $dates;
$response["title"] = $title;
}
echo json_encode($response);
?>
[에러코드]
W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:112)
at org.json.JSONObject.<init>(JSONObject.java:169)
at org.json.JSONObject.<init>(JSONObject.java:182)
at com.example.myhealthdiary.HealthLogListScreen$1.onResponse(HealthLogListScreen.java:67)
at com.example.myhealthdiary.HealthLogListScreen$1.onResponse(HealthLogListScreen.java:63)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)