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:
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);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
raffleNumber = intent.getStringExtra("SCAN_RESULT");
tvQR.setText(raffleNumber);
intent.getStringExtra("SCAN_RESULT_FORMAT");
AsyncTaskRunner runner = new AsyncTaskRunner();
runner.execute();
}
else if (resultCode == RESULT_CANCELED) {
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...");
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();
}
if (envelope.bodyIn != null) {
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) {
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(String... text) {
tvQR.setText(text[0]);
}
}
}
And here is the layout
<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
Imports System.Data.SqlClient
<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.