2012-04-23 224 views
0

我希望一切都很好。 我想添加Web服務到Android應用程序,爲此我創建了PHP服務器。請求和響應在JSON合成器中。和請求類型是 「後」 這是我的要求甲:使用JSON發送HTTP請求到PHP web服務器,在ANDROID

{"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"}]}}] 

我的Android代碼:

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; 
?> 

request.php

<?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 

} 

?> 

但是我得到空響應,而預期的響應 可以請你幫我如何做到這一點, 感謝您的幫助

回答

1
使用

此代碼

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
nameValuePairs.add(new BasicNameValuePair("json", Json1.toString())); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

代替

request.setEntity(new StringEntity(json1.toString())); 
0

這個請求的創建JSONObect和發送的請求服務器:

HttpPost post = new HttpPost(url); 
post.setEntity(new StringEntity(request.toString(), "utf-8")); 
// where `request` is a JSONObject 
+0

嗨帕雷什,我已經創建了這樣的請求:JSONObject json = new JSONObject(); json.put(「username」,userName);}}}}}}}}}}}}}}}} json.put(「password」,passWord);}}}}}}}}} \t \t \t \t \t \t JSONObject json1 = new JSONObject(); \t \t \t json1.put(「method_name」,「login」); \t \t \t json1.put(「body」,json); 現在還是不行? – Babloo 2012-04-23 07:06:32

+0

是的。即使你可以手動創建一個字符串值。 – 2012-04-23 07:25:29

+0

我得到一個空字符串作爲迴應, – Babloo 2012-04-23 09:04:35

相關問題