2016-04-11 60 views
1

在網上搜索了很多東西后,我沒有得到任何適當的解決方案。 我正在從android應用程序發送JSON數據到一個在PHP開發的web服務。在php webservice中解析json請求

當我通過Advanced Rest Cient Appication測試webservice時,我得到了正確的答覆,但是當我調用api時,它給了我null響應。

這是我從那裏我派我的數據服務器的android代碼:

JSONObject obj = new JSONObject(); 
      try { 

      obj.put("name", email_string); 
      obj.put("email", password_string); 

      } catch (JSONException e) { 
      e.printStackTrace(); 
      } 

      try { 
       HttpPost httppost = new HttpPost(url.toString()); 
       httppost.setHeader("Content-type", "application/json"); 
       StringEntity se = new StringEntity(obj.toString()); 
       se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
       httppost.setEntity(se); 
       HttpResponse response = httpclient.execute(httppost); 
       Content = EntityUtils.toString(response.getEntity()); 
       Log.e("TAG", "response: " + response); 
      } 
      catch(Exception e) 
      { 
       //Toast.makeText(getApplicationContext(), "Something went wrong please try again later", Toast.LENGTH_LONG).show(); 
      } 

這裏是我的PHP Web服務代碼:

<?php 
error_reporting(1); 
$conn = new mysqli('localhost', 'xxxxx', 'xxxx', 'xxxx'); 
// Get data 
$data = json_decode("php://input",true); 
print_r($data); 
$handle = fopen("php://input", "rb"); 
$raw_post_data = ''; 
while (!feof($handle)) { 
    $raw_post_data .= fread($handle, 8192); 
} 
fclose($handle); 

$data = json_encode($raw_post_data); 
$data = json_decode($data); 
$data = (array) $data; 
$myarray = split("&", $data[0]); 

$myarray2 = split("=", $myarray[0]); 

    $myarray3 = split("=", $myarray[1]); 

    if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
    } 

    $qry = "SELECT emp_id, name, domain_name FROM Itl_login WHERE domain_name = '".urldecode($myarray2[1])."' and password='".urldecode($myarray3[1])."'"; 

//echo $qry; 
$result = $conn->query($qry); 


while($row = $result->fetch_assoc()) { 
    $json = array("status" => 1, "name" => $row["name"], "domain_name"=> $row["domain_name"], "emp_ID" => $row["emp_id"]); 
} 

/* Output header */ 
header('Content-type: application/json'); 
echo json_encode($json); 



?> 

請給我建議我在做什麼這裏錯了。 任何幫助將appriciated。 預先感謝您。

+0

在你的控制達至$ QRY =「編碼SELECT?$ result = $ conn-> query($ qry);這行的輸出是什麼? –

回答

0

如果它適用於高級REST客戶端應用程序,而不是您的Android應用程序,這是因爲您沒有正確解析您的屬性。

我會說這是一些與此兩條線

$myarray2 = split("=", $myarray[0]); 

$myarray3 = split("=", $myarray[1]); 

你是讓你的「零響應」因爲查詢被嚴重從屬性