الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

درس/ انشاء برنامج للجوال يتصل بـ web service

بدأه فتاة البحر في 1 يوليو 2012 · 0 رد · 1,492 مشاهدة · في JavaME
مشاركة: واتساب X فيسبوك تيليجرام
#1

السلام عليكم و رحمة الله و بركاته

هذا الدرس يشرح طريقة انشاء برنامج على الجوال بواسطة لغة JAVA2ME

و من ثم يقوم البرنامج بالاتصال بـ web service للقيام بعملية حسابية معينة

الـ web service تم انشاءها بواسطة لغة C# و وظيفتها هو حساب الجذر التربيعي لاي عدد مدخل

أولا: كود الـ web service

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;

namespace WebServiceVB
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://codeproject.com/webservices/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public double squareroot(double value1)
        {
            return Math.Sqrt(value1);
        }

    }
}

ثانيا : كود البرنامج ، مع ملاحظة انه ينبغى ان تضع الـ Library بالبرنامج ( تجدها في المرفقات)

package eu.itcsolutions.j2me.ksoap;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;

/**
 * @author Fatat Alba7ar
 */
public class MidletkSOAP extends MIDlet implements CommandListener {
    //the main form

    Form mainForm = null;
    //the text-boxes for the input
    TextField txtBoxA = null;
    //the result label
    StringItem result = null;
    //the Exit command
    Command cmdExit = null;
    Command cmdsqrt = null;
    //the Display reference
    Display display = null;

    public MidletkSOAP() {
        //construct the main form
        mainForm = new Form("Calculate Squir Root");

        //construct the controls
        txtBoxA = new TextField("Value 1:", null, 5, TextField.NUMERIC);
        result = new StringItem("Result:", null);

        //add controls to the form
        mainForm.append(txtBoxA);
        mainForm.append(result);

        //construct commands
        cmdExit = new Command("Exit", Command.EXIT, 1);
        cmdsqrt = new Command("square root", Command.SCREEN, 1);

        //add commands
        mainForm.addCommand(cmdsqrt);
        mainForm.addCommand(cmdExit);


    }

    public void startApp() {
        //get the Display reference
        if (display == null) {
            display = Display.getDisplay(this);
        }
        //display the main form
        display.setCurrent(mainForm);

        //register the command listener
        mainForm.setCommandListener(this);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdExit) {
            this.destroyApp(false);
            this.notifyDestroyed();
        } else if (c == cmdsqrt) {
            callWebServiceMethod();
        }
    }

    //the kSOAP solution
    public void callWebServiceMethod() {
        String NAMESPACE = "http://codeproject.com/webservices/"; // لابد أن يكون الرابط هنا هو نفسه الرابط الموجود في ملف الويب سيرفيس بلغة السي شارب
        String METHOD_NAME = "squareroot";
        String SOAP_ACTION = "http://codeproject.com/webservices/squareroot";
        String URL = "http://localhost:54566/ServiceOnly.asmx"; // رابط الويب سيرفيس على جهازك


        //create a SOAP object
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        //add method arguments
        request.addProperty("value1",
                Integer.valueOf(txtBoxA.getString()));



        SoapSerializationEnvelope envelope =
                new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransport j2meHttpTransport = new HttpTransport(URL);

        try {
            //if you want to debug, uncomment next line
            //j2meHttpTransport.debug = true;
            j2meHttpTransport.call(SOAP_ACTION, envelope);
            SoapObject content = (SoapObject) envelope.bodyIn;
            String sqrt = content.getProperty(0).toString();

            //display result
            result.setText(sqrt);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }}

\

طبعا الاكواد اجزاء منها منسوخة من الانترنت و لكني عدلت عليها بحيث تحقق غرضي من البرنامج

لاحظ أن بامكانك نشر الـ web service على شبكة الانترنت من خلال الموقع التالي :

https://somee.com/Default.aspx

كلي امل بأن اكون قدمت لكم الفائدة و أن اكون ممن يزكّي العلم بنشره

( البرنامج + web service + Library في المرفقات )

دعواتكم

المشروع.zip

من فضلك... اذكر الله

لا اله الا الله وحده لا شريك له

له الملك و له الحمد و هو على كل شيء قدير

:thumb_up: :clapping:

(f)

مواضيع مشابهة