将文件从 backberry 上传到 Web 服务 = JVM 错误 104 未捕获 NullPointerException?

发布于 2024-11-27 11:00:26 字数 5840 浏览 1 评论 0原文

我正在开发一个小型黑莓项目。

以下是应该执行的步骤:

  1. 用户单击“发言”!按钮。该应用程序记录语音。 [没问题]

  2. 用户说完后,单击“停止”!按钮。一旦单击停止按钮,语音将以 AMR 文件形式保存在 BB 上。然后,该文件将通过 ksoap2 发送到 Web 服务。 Web 服务将以文件名字符串的形式返回响应。问题是Web服务什么也没返回,并且出现错误:JVM错误104:Uncaught NullPointerException我想知道我是否将代码放在正确的位置,或者我对ksoap2做错了什么? p>

这是 Web 服务的代码

namespace VoiceServer
{
    /// <summary>
    /// Converting AMR to WAV
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class Service1 : System.Web.Services.WebService
    {

        public string UploadFile(String receivedByte, String location, String fileName)
{
    String filepath = fileName;

/*don't worry about receivedByte and location, I will work on them after the problem is     solved :) */

    return "Success"+filepath;
}

        private void InitializeComponent()
        {

        }
    }
} 

下面是在 Eclipse 上运行的代码,我不确定是否将用于将文件发送到 Web 服务的代码放在正确的位置。

public class MyAudio extends MainScreen {
private ButtonField _startRecordingButton;
private ButtonField _stopRecordingButton;
private HorizontalFieldManager _fieldManagerButtons;
private VoiceNotesRecorderThread _voiceRecorder;
private LabelField _myAudioTextField;
private DateField hourMin;
private long _initTime;

public MyAudio() {
    _startRecordingButton = new ButtonField("Speak!", ButtonField.CONSUME_CLICK);
    _stopRecordingButton = new ButtonField("Stop!", ButtonField.CONSUME_CLICK);
    _fieldManagerButtons = new  HorizontalFieldManager();
    _voiceRecorder = new VoiceNotesRecorderThread(500000,"file:///store/home/user/voicefile.amr",this);
    _voiceRecorder.start();

    myButtonFieldChangeListener buttonFieldChangeListener = new myButtonFieldChangeListener();
    _startRecordingButton.setChangeListener(buttonFieldChangeListener);     
    _stopRecordingButton.setChangeListener(buttonFieldChangeListener);      

    _fieldManagerButtons.add(_startRecordingButton);
    _fieldManagerButtons.add(_stopRecordingButton);

    _myAudioTextField = new LabelField(" Welcome to VoiceSMS!!!" );
    add(_fieldManagerButtons);
    add(_myAudioTextField);

    SimpleDateFormat sdF = new SimpleDateFormat("ss");
    hourMin = new DateField("", 0, sdF);
    hourMin.setEditable(false);
    hourMin.select(false);
    _initTime = System.currentTimeMillis();
    add(hourMin);
}

public void setAudioTextField(String text) {
    _myAudioTextField.setText(text);
}

public void startTime() {
    _initTime = System.currentTimeMillis();
    hourMin.setDate(0);
}

public void updateTime() {
    hourMin.setDate((System.currentTimeMillis()-_initTime));
}

class myButtonFieldChangeListener implements FieldChangeListener{
    public void fieldChanged(Field field, int context) {
        if(field == _startRecordingButton) {
            try {
                _voiceRecorder.startRecording();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if(field == _stopRecordingButton) {
            _voiceRecorder.stopRecording();

            //----------Send AMR to Web Service-------------//

            Object response = null;
            String URL = "http://http://localhost:portnumber/Service1.asmx";
            String method = "UploadFile";
            String NameSpace = "http://tempuri.org/";
            FileConnection fc = null;
            byte [] ary = null;
            try 
            {
            fc = (FileConnection)Connector.open("file:///store/home/user/voicefile.amr",Connector.READ_WRITE);
            int size = (int) fc.fileSize();
            //String a = Integer.toString(size);
            //Dialog.alert(a);
            ary = new byte[size];
            fc.openDataInputStream().read(ary);
            fc.close();
            }
            catch (IOException e1) 
            {
            e1.printStackTrace();
            }
            SoapObject client = new SoapObject(NameSpace,method);
            client.addProperty("receivedByte",new SoapPrimitive(SoapEnvelope.ENC,"base64",Base64.encode(ary)));
            client.addProperty("location","Test/");
            client.addProperty("fileName","file:///store/home/user/voicefile.amr");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.bodyOut = client;
            HttpTransport http = new HttpTransport(URL);

            try 
            {
            http.call(method,envelope);
            }
            catch(InterruptedIOException io)
            {
            io.printStackTrace();
            }
            catch (IOException e) 
            {
            System.err.println(e);
            }
            catch (XmlPullParserException e) 
            {
            System.err.println(e);

            }
            catch(OutOfMemoryError e)
            {
            System.out.println(e.getMessage());
            }
            catch(Exception e)
            {
            e.printStackTrace();
            }

            try 
            {
            response = envelope.getResponse();
            Dialog.alert(response.toString());
            }
            catch (SoapFault e) 
            {
            System.err.println(e);
            System.out.println("Soap Fault");
            }
            catch(NullPointerException ne)
            {
            System.err.println(ne);
            }
            Dialog.alert(response.toString());
            //Dialog.alert("Send Success");

            //----------End of Upload-to-Web-Service--------//
        }           
    }
}
}

我不知道是否该文件没有发送到网络服务,或者网络服务已获取该文件并没有产生任何响应???我是 BB 编程的真正新手。如果我做错了什么,请告诉我。

提前致谢!!!

I am developing a small blackberry project.

Here are the step that it is supposed to be:

  1. User clicks Speak! button. The application record speech voice. [No Problem]

  2. When user finishes speaking, click Stop! button. Once the stop button is clicked, the speech voice will be saved on BB as an AMR file. Then, the file will be sent to web service via ksoap2. Web service will return response as a string of file name. The problem is web service return nothing and there is an error occur: JVM error 104: Uncaught NullPointerException I wonder if I placed the code on the right place, or I did something wrong with ksoap2??

here is the code for web service

namespace VoiceServer
{
    /// <summary>
    /// Converting AMR to WAV
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class Service1 : System.Web.Services.WebService
    {

        public string UploadFile(String receivedByte, String location, String fileName)
{
    String filepath = fileName;

/*don't worry about receivedByte and location, I will work on them after the problem is     solved :) */

    return "Success"+filepath;
}

        private void InitializeComponent()
        {

        }
    }
} 

Below is the code running on Eclipse, I'm not sure if I placed the code for sending file to web service on the right place.

public class MyAudio extends MainScreen {
private ButtonField _startRecordingButton;
private ButtonField _stopRecordingButton;
private HorizontalFieldManager _fieldManagerButtons;
private VoiceNotesRecorderThread _voiceRecorder;
private LabelField _myAudioTextField;
private DateField hourMin;
private long _initTime;

public MyAudio() {
    _startRecordingButton = new ButtonField("Speak!", ButtonField.CONSUME_CLICK);
    _stopRecordingButton = new ButtonField("Stop!", ButtonField.CONSUME_CLICK);
    _fieldManagerButtons = new  HorizontalFieldManager();
    _voiceRecorder = new VoiceNotesRecorderThread(500000,"file:///store/home/user/voicefile.amr",this);
    _voiceRecorder.start();

    myButtonFieldChangeListener buttonFieldChangeListener = new myButtonFieldChangeListener();
    _startRecordingButton.setChangeListener(buttonFieldChangeListener);     
    _stopRecordingButton.setChangeListener(buttonFieldChangeListener);      

    _fieldManagerButtons.add(_startRecordingButton);
    _fieldManagerButtons.add(_stopRecordingButton);

    _myAudioTextField = new LabelField(" Welcome to VoiceSMS!!!" );
    add(_fieldManagerButtons);
    add(_myAudioTextField);

    SimpleDateFormat sdF = new SimpleDateFormat("ss");
    hourMin = new DateField("", 0, sdF);
    hourMin.setEditable(false);
    hourMin.select(false);
    _initTime = System.currentTimeMillis();
    add(hourMin);
}

public void setAudioTextField(String text) {
    _myAudioTextField.setText(text);
}

public void startTime() {
    _initTime = System.currentTimeMillis();
    hourMin.setDate(0);
}

public void updateTime() {
    hourMin.setDate((System.currentTimeMillis()-_initTime));
}

class myButtonFieldChangeListener implements FieldChangeListener{
    public void fieldChanged(Field field, int context) {
        if(field == _startRecordingButton) {
            try {
                _voiceRecorder.startRecording();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if(field == _stopRecordingButton) {
            _voiceRecorder.stopRecording();

            //----------Send AMR to Web Service-------------//

            Object response = null;
            String URL = "http://http://localhost:portnumber/Service1.asmx";
            String method = "UploadFile";
            String NameSpace = "http://tempuri.org/";
            FileConnection fc = null;
            byte [] ary = null;
            try 
            {
            fc = (FileConnection)Connector.open("file:///store/home/user/voicefile.amr",Connector.READ_WRITE);
            int size = (int) fc.fileSize();
            //String a = Integer.toString(size);
            //Dialog.alert(a);
            ary = new byte[size];
            fc.openDataInputStream().read(ary);
            fc.close();
            }
            catch (IOException e1) 
            {
            e1.printStackTrace();
            }
            SoapObject client = new SoapObject(NameSpace,method);
            client.addProperty("receivedByte",new SoapPrimitive(SoapEnvelope.ENC,"base64",Base64.encode(ary)));
            client.addProperty("location","Test/");
            client.addProperty("fileName","file:///store/home/user/voicefile.amr");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.bodyOut = client;
            HttpTransport http = new HttpTransport(URL);

            try 
            {
            http.call(method,envelope);
            }
            catch(InterruptedIOException io)
            {
            io.printStackTrace();
            }
            catch (IOException e) 
            {
            System.err.println(e);
            }
            catch (XmlPullParserException e) 
            {
            System.err.println(e);

            }
            catch(OutOfMemoryError e)
            {
            System.out.println(e.getMessage());
            }
            catch(Exception e)
            {
            e.printStackTrace();
            }

            try 
            {
            response = envelope.getResponse();
            Dialog.alert(response.toString());
            }
            catch (SoapFault e) 
            {
            System.err.println(e);
            System.out.println("Soap Fault");
            }
            catch(NullPointerException ne)
            {
            System.err.println(ne);
            }
            Dialog.alert(response.toString());
            //Dialog.alert("Send Success");

            //----------End of Upload-to-Web-Service--------//
        }           
    }
}
}

I don't know if the file is not sent to web service, or web service has got the file and produce no response??? I am a real newbie for BB programming. Please let me know if I did anything wrong.

Thanks in advance!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤舞 2024-12-04 11:00:26

您的 URL 变量值存在拼写错误。

“http://”输入两次

String URL = “http://http://localhost:portnumber/Service1.asmx”;

There is a typo in your URL variable value.

"http://" typed twice

String URL = "http://http://localhost:portnumber/Service1.asmx";

攒眉千度 2024-12-04 11:00:26

万岁!!!问题解决了!

只是按照Rafael的建议更改了URL,并在Web服务代码中的“公共字符串UploadFile”上方添加了[WebMethod]

Hooray!!! Problem Solved!

just changed URL as Rafael suggested and added [WebMethod] above "public string UploadFile" in the web service code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文