2014-10-08 76 views
0

有了這個應用程序,我想掃描的二維碼(我已經這樣做),然後從數據庫中的表中獲取特定的行使用掃描QR返回的值代碼在我的SELECT語句中。將QR碼掃描器android應用程序連接到在線mySQL數據庫

到目前爲止我的代碼如下:

public void Scan(View v) 
{ 
    try { 

     Intent intent = new Intent("com.google.zxing.client.android.SCAN"); 
     intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); // "PRODUCT_MODE for bar codes 

     startActivityForResult(intent, 0); 

    } catch (Exception e) { 

     Uri marketUri = Uri.parse("market://details?id=com.google.zxing.client.android"); 
     Intent marketIntent = new Intent(Intent.ACTION_VIEW,marketUri); 
     startActivity(marketIntent); 

    } 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {   
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 0) { 

     if (resultCode == RESULT_OK) { 
      String contents = data.getStringExtra("SCAN_RESULT");//Value retrieved from qr code is stored in this string 
     } 
     if(resultCode == RESULT_CANCELED){ 
      //handle cancel 
     } 
    } 
} 

回答

0

最好的辦法是先獲得服務器端的東西跑了。例如通過使用PHP。我們需要製作一個可以獲取數據輸入的腳本,並使用它來進行查詢以獲取所需的數據。例如像這樣:

<?php 
    $data = $_POST['data']; 

    // $data holds your data from the android app 
    // Perform your db query here 

    $result = ["Resultdata" => $dbresult]; 
    print json_encode($result); 
?> 

將此代碼保存爲process.php並將其保存在您的網絡服務器上。例如:http://www.example.com/process.php

你想要做的下一件事就是讓你的Android代碼向你的php腳本發出請求。例如,使用此功能:

public String makePOSTRequest(String url, List<NameValuePair> nameValuePairs) { 
    String response = ""; 

    try { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     response = EntityUtils.toString(httpEntity); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    Log.d(LOGTAG, "POST Response >>> " + response); 
    return response; 

} 

這裏找到:sending JSON to server using POST in android app

您可以使用如下代碼:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("data", resultCode)); 

String response = makePOSTRequest(String url, nameValuePairs); 

執行的最後一行之後,從我們的服務器的響應應該是響應變量。搜索谷歌解碼JSON和做任何你想要的數據!我希望這可以幫助你!