Spinner에서 데이터를 전송해서 받아오려고 합니다.
선택한 스피너의 값을 전송해서 php를 통해 json 데이터를 받아오는데
데이터값이 자꾸 전송이안되는거같네요...
java acitivity 에서 전송하는 부분입니다.
protected Void doInBackground(String... params) {
String selectedItem = params[0].toString();
ServiceHandler jsonParser = new ServiceHandler();
try {
//URL 설정, 접속
URL url = new URL("접속URL");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
//전송 모드 설정
http.setDefaultUseCaches(false);
http.setDoInput(true); //서버에서 읽기모드 지정
http.setDoOutput(true); //서버에서 쓰기모드지정
http.setRequestMethod("POST");
http.setRequestProperty("content-type", "application/json");
//서버로 값 전송
StringBuffer buffer = new StringBuffer();
buffer.append("path1").append("=").append(selectedItem);
OutputStreamWriter outStream = new OutputStreamWriter(http.getOutputStream(), "UTF-8");
PrintWriter writer = new PrintWriter(outStream);
writer.write(buffer.toString());
writer.flush();
String json = jsonParser.makeServiceCall(URL_DEP_RESULT, ServiceHandler.POST);
Log.e("Dep_result json : ", "> "+json);
if(json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
if(jsonObj != null) {
JSONArray dep_result = jsonObj.getJSONArray("dep_result");
for(int i=0; i<dep_result.length(); i++) {
JSONObject dep_resObj = (JSONObject)dep_result.get(i);
Dep_result dep_res = new Dep_result(dep_resObj.getString("dep_file"));
fileList.add(dep_res);
}
}
} catch(JSONException e) {
Log.e("Json Error : ", e.toString());
}
} else {
Log.e("Depfile if Error : ", "IF ERROR");
}
} catch(Exception e) {
e.printStackTrace();
}
return null;
php 부분입니다.
<?php
$path1 = $_POST["path1"];
function getPath2List($path1) {
$dir="01/".$path1;
$files = scandir($dir);
$file_path = array();
$file_path["dep_result"] = array();
foreach($files as $ind_file) {
if($ind_file != '.' && $ind_file !='..'){
if(substr($ind_file,0,2) == "20") {
if($path1 == "") {
$tmp = array();
$tmp["dep_file_nopath"] = $ind_file;
} else {
$tmp = array();
$tmp["dep_file"] = $ind_file;
}
}
array_push($file_path["dep_result"], $tmp);
}
}
header('Content-Type: application/json');
echo json_encode($file_path);
}
getPath2List($path1);
?>
위에 보시면 테스트용으로 받아오는 $path1이 없을때 json 데이터가 출력이 됩니다. 아직초보라 좀알려주세요ㅠㅠ