2016-02-11 47 views
1

我是Android開發人員的初學者。我想將一個php文件連接到android應用程序。我的PHP代碼是與android項目的php連接

<?php  
    $con = mysqli_connect("localhost", "root", "", "invoice_db"); 

    if(mysqli_connect_errno($con)) { 
     echo "Failed to connect"; 
    } 

    $response["sucess"]=0; 
    $invoiceid = $_POST['invc']; 
    $response = array(); 
    $sql = "SELECT sl_no from invoice_table where invoice_id='$invoiceid'"; 
    $result = mysqli_query($con,$sql); 

    if(!empty($result)) { 
     $row = mysqli_fetch_array($result); 
     $data = $row[0]; 
     $response["sucess"] = 1; 
    } 
    mysqli_close($con); 
?> 

這裏INVC'是HttpRequest都有,

JSONObject json = jsonParser.makeHttpRequest(url_check_user, "POST", params); 

而且我JSONParser頁面包含,

if (method == "POST") { 
    // request method is POST 
    // defaultHttpClient 
    System.out.println("Inside json parser POST condition"); 
    DefaultHttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost(url); 
    httpPost.setEntity(new UrlEncodedFormEntity(params)); 

    System.out.println("Inside json parser POST condition" + params); 

    HttpResponse httpResponse = httpClient.execute(httpPost); 
    HttpEntity httpEntity = httpResponse.getEntity(); 
    Log.d("From httpentity", httpEntity.toString()); 
    System.out.println("ppppppppppppphhhhhhhhhhhhhhhhhhhhppppppppppp"); 
    is = httpEntity.getContent(); 
} 

現在我要檢查,參數是否傳遞給php頁面或不。所以我想安慰/登錄貓$invoiceid。 Eclipse IDE中怎麼可能?

+1

你可能會改變的另一件事是'method =='POST''到'method.equals(「POST」)'。 –

+0

這是真的,在Java中,您必須使用'.equals(...)'方法比較字符串,因爲'=='運算符比較實例(即引用) – user2340612

回答

0

如果你想在PHP代碼中打印一個變量,你可以做echo $variable。但請注意,PHP代碼將在服務器上執行,而不是在您的Android設備上執行。此外,您的PHP代碼容易受到SQL注入攻擊。

+0

任何其他可能的可用於顯示eclipse console/log中的變量貓。我不想顯示在瀏覽器控制檯中 – Rose

+0

PHP在Web服務器上執行,它與Android無關,因此無法將任何內容打印到logcat。您可以打印到LogCat調用的**結果**,即字符串/ Json/HTML /無論您的PHP代碼是否返回。 – user2340612

+0

@Rose:如果您在瀏覽器中訪問PHP的URL,那麼只有在瀏覽器中顯示結果。如果你通過你的應用程序運行它,那麼你將不得不使用'Log'打印它。正如已經指出的那樣,例如@ user2340612,你必須將結果放在'String'中。這裏的要點是,如果你需要檢查輸出/結果,你將不得不在你的PHP –

0

您可以在您的php文件中使用JSON編碼方法來獲得正確的JSON響應。

$response = array (
       'invoiceid' => $verify_code,  
      ); 
print json_encode($response); 

將在一個格式的JSON字符串返回給你的應用程式,例如

{"invoiceid":"null"} 

,您可以解碼並記錄它像這樣

InputStreamReader isw = new InputStreamReader(is); 
      BufferedReader br = new BufferedReader(isw); 
      String line = ""; 
      StringBuffer buffer = new StringBuffer(); 
      while ((line = br.readLine()) != null){ 
       buffer.append(line); 
      } 
String finalresult = buffer.toString(); 
JSONObject myobject = new JSONObject(finalresult); 
      String flag= myobject.getString("invoiceid"); 

log.e("mylog",flag) 

因此,這將是可見你的logcat文件。