Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Ok, so it's been more than 24 hours and I haven't received any feedback regarding my concern, so I'll improve the question further. I'm new to Android and I need help if there is something wrong with my code.

I search the net for a code for scanning barcodes and found one, and I also need to send the info received from the barcode scanner through a web service created in Visual Basic/ASP.NET

Here is the java code:

Java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

public class MainActivity extends Activity {
   
    private static String NAMESPACE = "http://www.sdgsystems.somee.com";
    private static String METHOD_NAME = "ValidateRaffleTicket";
    private static String URL = "http://www.sdgsystems.somee.com/dataverifier.asmx?WSDL";

	String raffleNumber;
	TextView tvQR;
	TextView tvStatus;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        HandleClick hc = new HandleClick();
        findViewById(R.id.btnScanQR).setOnClickListener((HandleClick) hc);
        tvQR = (TextView)findViewById(R.id.tvResult);
        tvStatus = (TextView)findViewById(R.id.tvStatus);
    }

    private class HandleClick implements OnClickListener {
    	public void onClick(View arg0) {
    		Intent intent = new Intent("com.google.zxing.client.android.SCAN");     
    		intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
    		startActivityForResult(intent, 0);	//Barcode Scanner to scan for us
    	}
    }
    
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {  
    	if (requestCode == 0) {  
    		if (resultCode == RESULT_OK) {  
    			//Handle successful scan  
    			raffleNumber = intent.getStringExtra("SCAN_RESULT");  
    			tvQR.setText(raffleNumber);
    			intent.getStringExtra("SCAN_RESULT_FORMAT");
    			AsyncTaskRunner runner = new AsyncTaskRunner();
    			runner.execute();
    		} 
    		else if (resultCode == RESULT_CANCELED) {  
    			//Handle cancel
    			//Toast.makeText(MainActivity.this, "Scan Result:" + intent.getStringExtra("RESULT"), Toast.LENGTH_SHORT).show();
    			tvQR.setText("[Ticket Number]");
    			tvStatus.setText("Scan Cancelled");
    		}  
    	  } 
    }
    
    private class AsyncTaskRunner extends AsyncTask<String, String, String> {
    	
    	private String resp;
    	String test;
    	
    	@Override
    	protected String doInBackground(String... params) {
    	     
    		publishProgress("Loading contents..."); // Calls onProgressUpdate()

    	    try {

    	    	SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    	    	PropertyInfo raffle = new PropertyInfo();
    	    	raffle.setName("raffleNumber");
    	    	raffle.setValue(raffleNumber);
    	    	raffle.setType(String.class);
    	    	request.addProperty(raffle);

    	    	SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    	    	envelope.dotNet=true;
    	    	envelope.setOutputSoapObject(request);
    	       
    	    	HttpTransportSE transport = new HttpTransportSE(URL);
    	       
    	    	try {
    	    		transport.call(NAMESPACE + "/" + METHOD_NAME, envelope);
    	        } 
    	    	catch (IOException e) {
    	    		e.printStackTrace();
    	    	} 
    	    	catch (XmlPullParserException e) {
    	    		e.printStackTrace();
    	    	}

	    		//test = raffle.toString();

    	    	if (envelope.bodyIn != null) {
    	    		
	    		   //SoapPrimitive resultSOAP = (SoapPrimitive) ((SoapObject) envelope.bodyIn).getProperty(0);
	    		   SoapPrimitive resultSOAP = (SoapPrimitive) envelope.getResponse();
	    		   resp=resultSOAP.toString();
	    		   
	    		   runOnUiThread(new Runnable() {

					@Override
					public void run() {
						tvQR.setText(raffleNumber);
						tvStatus.setText(resp);
					}
	    			  
	    		   }); 
   	     
    	    	}
    	    } 
    	    catch (Exception e) {
    	    	e.printStackTrace();
    	    	resp = e.getMessage();
    	    }
    	 
    	    return resp;
    	}

    	@Override
    	protected void onPostExecute(String result) {
    		  //tvQR.setText(raffleNumber);
    		  //tvStatus.setText(resp);
    	}

    	@Override
    	protected void onPreExecute() {
    		// Things to be done before execution of long running operation. For
    		// example showing ProgessDialog
    	}

    	@Override
    	protected void onProgressUpdate(String... text) {
    		tvQR.setText(text[0]);
    	}
    }
    
}


And here is the layout
HTML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${packageName}.${activityClass}" >

    <Button
        android:id="@+id/btnScanQR"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvResult"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="94dp"
        android:text="@string/scan_qr" />

    <TextView
        android:id="@+id/tvResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="68dp"
        android:text="@string/qr_text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/tvStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvResult"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="@string/status_qr"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>


Here is the Web Service
VB
Imports System.Data.SqlClient

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://www.sdgsystems.somee.com/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class DataVerifier
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function ValidateRaffleTicket(ByVal raffleNumber As String) As String
        Dim con As New SqlConnection("workstation id=A1Advertising.mssql.somee.com;packet size=4096;user id=user_name;pwd=password;data source=A1Advertising.mssql.somee.com;persist security info=False;initial catalog=A1Advertising")
        con.Open()
        Dim cmd As New SqlCommand("SELECT * FROM tblTickets WHERE TicketNo='" & Trim(raffleNumber) & "'", con)
        Dim dr As SqlDataReader = cmd.ExecuteReader
        If dr.HasRows Then
            While dr.Read
                If dr(2).ToString = "ACTIVE" Then
                    Return "Ticket Is Valid"
                ElseIf dr(2).ToString = "REGISTERED" Then
                    Return "Ticket Already Registered"
                End If
            End While
        Else
            Return "Invalid Ticket"
        End If
    End Function

End Class


I have tested it directly to a device and based on LogCat it shows it gets the information of the barcode scanned, but somehow the web service keeps on returning "Invalid Ticket", even if the barcode sequence exists on the database.

If the examples I've read says that the part where the app accesses the web service should be done in AsyncTask, is there something wrong with what I'm doing because I've placed my code for getting the barcode information is placed in the onActivityResult? Please help, thanks in advance.
Posted
Updated 10-Jun-14 23:10pm
v4

1 solution

Quote:
the web service keeps on returning "Invalid Ticket", even if the barcode sequence exists on the database.

If you verified that the ticket, i.e. the raffleNumber, is actually in your database. The only point of failure can be that part:
VB
Dim cmd As New SqlCommand("SELECT * FROM tblTickets WHERE TicketNo='" & Trim(raffleNumber) & "'", con)
        Dim dr As SqlDataReader = cmd.ExecuteReader
        If dr.HasRows Then
            While dr.Read
                If dr(2).ToString = "ACTIVE" Then
                    Return "Ticket Is Valid"
                ElseIf dr(2).ToString = "REGISTERED" Then
                    Return "Ticket Already Registered"
                End If
            End While
        Else
            Return "Invalid Ticket"
        End If


Well, looks like the datareader returns no rows. How does your DB table tblTickets look like and how is your raffleNumber formatted?
 
Share this answer
 
Comments
Member 10874831 11-Jun-14 8:12am    
Hi Chris875, thanks for the reply. I've verified that the web service is working. the Table tblTickets only has 3 fields [TicketNo,Site,Status]. I've tried to re-write my code in android without the barcode scanning process and somehow it worked. Most probably there is a problem with the code i'm using to access the web service. I'll try to implement the barcode scanning process. Thanks again for the reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900