1

With this app I want to scan the Qr code(which i am already doing) and then get a certain row from a table in the database using the value returned from scanning the QR code in my SELECT statement.

My code so far is as follows:

    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
        }
    }
}
FiF4M33sTeR
  • 269
  • 1
  • 4
  • 13

1 Answers1

0

The best approach is to first get the server-side thing running. For example by using PHP. We need to make a scripts that can get your data input and use it to make a query that gets the data you want. For example like so:

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

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

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

Save this code as process.php and make it available on your webserver. For example: http://www.example.com/process.php

The next thing you want to do is make your Android code to do a request to your php script. For example using this function:

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;

}

Found here: sending JSON to server using POST in android app

You can use the code as follows:

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

String response = makePOSTRequest(String url, nameValuePairs );

After executing the last line, the response from our server should be in the response variable. Search on google for decoding json and do whatever you want with the data! I hope this helps you!

Community
  • 1
  • 1
tonsmets
  • 92
  • 3