안녕하세요. 도저히 해결이 안되서 질문 올립니다.
여행 정보를 서버 데이터베이스 넣으려고 하는데 안되서요.
apmsetup, phpmyadmin을 이용해 서버를 구동하고 있고 서버 연동을 위해 php를 사용합니다.
아래는 안드로이드에서 정보등록 버튼을 클릭할때 동작하는 부분입니다.
Response.Listener responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
Toast.makeText(getApplicationContext(), " / " , Toast.LENGTH_SHORT).show();
if(success) {
AlertDialog.Builder builder = new AlertDialog.Builder(TourRegisterActivity.this);
builder.setMessage("투어등록 성공");
builder.setPositiveButton("확인", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.show();
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(TourRegisterActivity.this);
dialog = builder.setMessage("투어등록 실패")
.setNegativeButton("확인",null)
.create();
dialog.show();
}
}catch (Exception e){
e.printStackTrace();
}
}
};
TourRegisterRequest tourRegisterRequest = new TourRegisterRequest(tourID, tourName, tourPrice, tourCity, tourCountry, minNum, maxNum, meetPlace, meetTime, estTime, guideID, responseListener);
RequestQueue queue = Volley.newRequestQueue(TourRegisterActivity.this);
queue.add(tourRegisterRequest);
--------------------------------------------------------------------------------------------
public class TourRegisterRequest extends StringRequest {
final static private String URL = "http://****:55555/GoodGuide/TourRegister.php";
private Map<String, String> parameters;
public TourRegisterRequest(String tourID, String tourName, String tourPrice, String tourCity, String tourCountry, String minNum,
String maxNum, String meetPlace, String meetTime, String estTime, String guideID, Response.Listener<String> listener){
super(Method.POST, URL, listener, null);
parameters = new HashMap<>();
parameters.put("tourID", tourID);
parameters.put("tourName", tourName);
parameters.put("tourPrice", tourPrice);
parameters.put("tourCity", tourCity);
parameters.put("tourCountry", tourCountry);
parameters.put("minNum", minNum);
parameters.put("maxNum", maxNum);
parameters.put("meetPlace", meetPlace);
parameters.put("meetTime", meetTime);
parameters.put("estTime", estTime);
parameters.put("guideID", guideID);
}
public Map<String, String> getParams() {
return parameters;
}
}
---------------------------------------------------------------------------------------
/EGL_emulation: eglMakeCurrent: 0x985a6de0: ver 3 0 (tinfo 0xa360b2b0)
W/System.err: org.json.JSONException: Value DB of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONObject.<init>(JSONObject.java:163)
at org.json.JSONObject.<init>(JSONObject.java:176)
W/System.err: at com.example.user.goodguide.TourRegisterActivity$2$1.onResponse(TourRegisterActivity.java:86)
at com.example.user.goodguide.TourRegisterActivity$2$1.onResponse(TourRegisterActivity.java:82)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6541)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
D/EGL_emulation: eglMakeCurrent: 0x985a6de0: ver 3 0 (tinfo 0xa360b2b0)
D/EGL_emulation: eglMakeCurrent: 0x985a6de0: ver 3 0 (tinfo 0xa360b2b0)
처음에 실행시에는 투어 등록 성공이라는 다이얼로그가 뜨긴했는데 서버 데이터베이스에는 내용이 삽입되어 있지 않더라고요. 그리고 이후에 코드 상 고친것은 없는데 계속
org.json.JSONException: Value DB of type java.lang.String cannot be converted to JSONObject 이 메시지가 뜨면서 안되더라고요. 뭐 php파일이 utf-8이 아니라 ansi로 해야된다. 등등 뭐 여러가지 해봤는데도 잘 안되서 질문드립니다. 아래는 php파일입니다.
<?php
$con = mysqli_connect("localhost", "goodguide", "rntrkdlem", "goodguide");
if(mysqli_connect_errno($con)) {echo "DB 접속 실패";} else {echo "DB 접속 성공";}
$tourID = $_POST["tourID"];
$tourName = $_POST["tourName"];
$tourPrice = $_POST["tourPrice"];
$tourCity = $_POST["tourCity"];
$tourContry = $_POST["tourContry"];
$minNum = $_POST["minNum"];
$maxNum = $_POST["maxNum"];
$meetPlace = $_POST["meetPlace"];
$meetTime = $_POST["meetTime"];
$estTime = $_POST["estTime"];
$guideID = $_POST["guideID"];
$statement = mysqli_prepare($con, "INSERT INTO TOUR_INFO VALUES (?,?,?,?,?,?,?,?,?,?,?)");
mysqli_stmt_bind_param($statement, "sssssssssss", $tourID, $tourName, $tourPrice, $tourCity, $tourContry, $minNum, $maxNum, $meetPlace, $meetTime, $estTime, $guideID);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>