条码扫描器(Android)和Web服务(.NET)的集成如何工作? [英] How does Integration of Barcode Scanner (Android) and Web Service (.NET) works?

查看:85
本文介绍了条码扫描器(Android)和Web服务(.NET)的集成如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以已经超过24小时了,而且我没有收到有关我的担忧的任何反馈,所以我会进一步改进这个问题。我是Android新手,如果我的代码有问题,我需要帮助。



我在网上搜索扫描条形码的代码并找到一个,并且我还需要通过在Visual Basic / ASP.NET中创建的Web服务发送从条形码扫描器收到的信息



这是java代码:



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);	//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

<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>





这是Web服务



Here is the Web Service

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





我已将其直接测试到设备并基于LogCat显示它获取扫描条形码的信息,但不知何故,Web服务不断返回无效票证,甚至如果是b arcode序列存在于数据库中。



如果我读过的例子说应用程序访问Web服务的部分应该在AsyncTask中完成,那么有什么东西吗?错误的我正在做什么,因为我已经把我的代码用于获取条形码信息放在onActivityResult?请提前帮助。



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.

推荐答案

{packageName}。
{packageName}.


{activityClass} >

< 按钮

android:id = @ + id / btnScanQR

android:layout_width < span class =code-keyword> = wrap_content
< span class =code-attribute>
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>
{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 Serv ice



Here is the Web Service

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 b arcode 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.



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.


Quote:

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

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:


If you verified that the ticket, i.e. the raffleNumber, is actually in your database. The only point of failure can be that part:

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 r eturns no rows. How does your DB table tblTickets look like and how is your raffleNumber formatted?



Well, looks like the datareader returns no rows. How does your DB table tblTickets look like and how is your raffleNumber formatted?


这篇关于条码扫描器(Android)和Web服务(.NET)的集成如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆