모두 잘되고 싶습니다. Android 애플리케이션에 웹 서비스를 추가하려고합니다. PHP 서버를 만들었습니다. 요청 및 응답은 JSON 형식으로 제공됩니다.안드로이드에서 JSON을 사용하여 PHP 웹 서버에 HTTP 요청 보내기
{"method_name":"checkLogin","body": {"username":"[email protected]","password":"password","device_type":"android","lat":"","long":""}}
예상 응답 :
[{"status":"1","message":"Login successfully","data":{"id":"xx","username":"[email protected]","first_name":"xxx","last_name":"yyy","gender":"xxx","relationship":"xxxx","birthdate":"xxxx","hometown":"","major":null,"class":null,"house":null,"device_type":"iphone","current_location_lat":"0.0","current_location_lon":"0.0","profile_image":"no_male_user.png","is_login":"1","created":"2012","status":"1","tag":[{"id":"x","tag_name":"xxxx"},{"id":"x","tag_name":"xxx xxx"}]}}]
내 안드로이드 코드 :
userName = userNameField.getText().toString();
passWord = passWordField.getText().toString();
try {
JSONObject json = new JSONObject();
json.put("username", userName);
json.put("password", passWord);
json.put("device_type", "Android");
json.put("lat", "0.0");
json.put("long", "0.0");
JSONObject json1 = new JSONObject();
json1.put("method_name", "checkLogin");
json1.putOpt("body", json);
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);
HttpPost request = new HttpPost(URL);
request.setEntity(new StringEntity(json1.toString()));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = client.execute(request, responseHandler);
Toast.makeText(this, response, Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Throwable t) {
Toast.makeText(this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}
main.php
<?php
require_once('configure.php');
require_once('db.php');
require_once("request.php");
require_once("error_msg.php");
require_once("./includes/JSON.php");
$json = $_POST['json'];
$objRequest = new Request($json);
$rows = $objRequest -> main();
echo $rows;
?>
및 요청 유형이 내 요청 메이트입니다 "포스트"입니다
<?php
require_once("./includes/JSON.php");
require_once("db.php");
require_once('SimpleImage.php');
class Request
{
function __construct($json)
{
$this -> array1 = @json_decode(stripcslashes(trim($json)));
}
function main()
{
$objDb = new DB();
$conn = $objDb -> getConnection();
if(!$conn)
return Error :: $DB_CONN_ERROR." ".$DB_CONN_ERROR_DESC;
else
{
if($this -> array1 -> method_name == 'checkLogin')
return $this -> getLoginFlag($this -> array1 -> body);
}
}
// For Login Start
function getLoginFlag($body)
{
$username = trim($body -> username);
$password = ($body -> password);
$device_type = trim($body -> device_type);
$this -> rs_login = DB :: selectLoginRecord($body);
$arr_resp = array();
$retArray = array();
$tagListArray = array();
if(mysql_num_rows($this -> rs_login) > 0)
{
while($rows = mysql_fetch_array($this -> rs_login))
{
$arr_resp = array("id"=> $rows['id'],"username"=> $rows['username'],"first_name"=> ucfirst($rows['first_name']),"last_name"=>ucfirst($rows['last_name']),"gender"=>$rows['gender'],"relationship"=>$rows['relationship'],"birthdate"=>date('M d, Y', strtotime($rows['birthdate'])),"hometown"=>$rows['hometown'],"major"=>$rows['major'],"class"=>$rows['class'],"house"=>$rows['house'],"device_type"=>$rows['device_type'],"current_location_lat"=>$rows['current_location_lat'],"current_location_lon"=>$rows['current_location_lon'],"profile_image"=>$rows['profile_image'],"is_login"=>$rows['is_login'],"created"=>date('Y', strtotime($rows['created'])),"status"=>$rows['status']);
}
$this -> rs_tag_list = DB :: selectTagList($arr_resp['id']);
if(mysql_num_rows($this -> rs_tag_list) > 0)
{
while($rows = mysql_fetch_array($this -> rs_tag_list))
{
$tagListArray['tag'][]= array("id"=> $rows['id'],"tag_name"=> $rows['tag_name']);
}
}
$finalArray = array_merge($arr_resp,$tagListArray);
$retArray[] = array("status" => "1","message" => "Login successfully","data" => $finalArray);
}
else
{
$retArray[] = array("status" => "0","message" => "The username or password you entered is incorrect");
}
return json_encode($retArray);
}
// End Login
}
?>
하지만이 작업을 수행하는 방법을 좀 도와주십시오 수 있습니다 대신 예상 응답 의 빈 반응을 얻고있다, 이
안녕하세요 Paresh, 나는 다음과 같은 요청을 만들었습니다. JSONObject json = new JSONObject(); \t \t \t json.put ("username", userName); \t \t \t json.put ("password", passWord); \t \t \t \t \t \t JSONObject json1 = new JSONObject(); \t \t \t json1.put ("METHOD_NAME", "로그인"); \t \t \t json1.put ("body", json); 현재 진행 중이거나하지 않습니까? – Babloo
예. 문자열 값을 수동으로 만들 수도 있습니다. 내가 응답으로 빈 문자열을 얻고있다 –
내가 테스트를위한 코드와 라이브 URL을 모르기 때문에, – Babloo