Java tutorial
/* * Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.android.gms.samples.vision.barcodereader; import android.content.Intent; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.View; import android.widget.CompoundButton; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.common.api.CommonStatusCodes; import com.google.android.gms.vision.barcode.Barcode; import com.android.volley.AuthFailureError; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import org.json.JSONException; import org.json.JSONObject; import java.util.HashMap; import java.util.Map; import static android.widget.Toast.LENGTH_SHORT; /** * Main activity demonstrating how to pass extra parameters to an activity that * reads barcodes. */ public class MainActivity extends Activity implements View.OnClickListener { // use a compound button so either checkbox or switch widgets work. private CompoundButton autoFocus; private CompoundButton useFlash; private TextView statusMessage; private TextView barcodeValue; private static final int RC_BARCODE_CAPTURE = 9001; private static final String TAG = "BarcodeMain"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); statusMessage = (TextView) findViewById(R.id.status_message); barcodeValue = (TextView) findViewById(R.id.barcode_value); autoFocus = (CompoundButton) findViewById(R.id.auto_focus); useFlash = (CompoundButton) findViewById(R.id.use_flash); findViewById(R.id.read_barcode).setOnClickListener(this); } /** * Called when a view has been clicked. * * @param v The view that was clicked. */ @Override public void onClick(View v) { if (v.getId() == R.id.read_barcode) { // launch barcode activity. Intent intent = new Intent(this, BarcodeCaptureActivity.class); intent.putExtra(BarcodeCaptureActivity.AutoFocus, autoFocus.isChecked()); intent.putExtra(BarcodeCaptureActivity.UseFlash, useFlash.isChecked()); startActivityForResult(intent, RC_BARCODE_CAPTURE); } } /** * Called when an activity you launched exits, giving you the requestCode * you started it with, the resultCode it returned, and any additional * data from it. The <var>resultCode</var> will be * {@link #RESULT_CANCELED} if the activity explicitly returned that, * didn't return any result, or crashed during its operation. * <p/> * <p>You will receive this call immediately before onResume() when your * activity is re-starting. * <p/> * * @param requestCode The integer request code originally supplied to * startActivityForResult(), allowing you to identify who this * result came from. * @param resultCode The integer result code returned by the child activity * through its setResult(). * @param data An Intent, which can return result data to the caller * (various data can be attached to Intent "extras"). * @see #startActivityForResult * @see #createPendingResult * @see #setResult(int) */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == RC_BARCODE_CAPTURE) { if (resultCode == CommonStatusCodes.SUCCESS) { if (data != null) { final Barcode barcode = data.getParcelableExtra(BarcodeCaptureActivity.BarcodeObject); statusMessage.setText(R.string.barcode_success); barcodeValue.setText(barcode.displayValue); Log.d(TAG, "Barcode read: " + barcode.displayValue); RequestQueue queue = Volley.newRequestQueue(this); StringRequest postRequest = new StringRequest(Request.Method.POST, "http://172.16.9.79/ingresoUTB/registro", new Response.Listener<String>() { @Override public void onResponse(String response) { Toast.makeText(MainActivity.this, "Error de conexin", LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, "Error de conexin", LENGTH_SHORT).show(); } }) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("codigo", barcode.displayValue.toLowerCase()); params.put("autor", "celador"); return params; } @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> params = new HashMap<String, String>(); params.put("Content-Type", "application/x-www-form-urlencoded"); return params; } }; queue.add(postRequest); } else { statusMessage.setText(R.string.barcode_failure); Log.d(TAG, "No barcode captured, intent data is null"); } } else { statusMessage.setText(String.format(getString(R.string.barcode_error), CommonStatusCodes.getStatusCodeString(resultCode))); } } else { super.onActivityResult(requestCode, resultCode, data); } } }