【问题标题】:How to write to a text file - HttpConnection J2ME如何写入文本文件 - HttpConnection J2ME
【发布时间】:2012-06-22 11:30:37
【问题描述】:

我没有收到任何错误,但我的代码没有将任何新数据保存到文本文件中,这是我的代码:

public void saveToLeaderboard() throws IOException {
        String toSave = "Random info I want to save";
        HttpConnection connection = null;

        connection = (HttpConnection) Connector.open("EXTERNAL URL", Connector.READ_WRITE, true);
        connection.setRequestMethod(HttpConnection.POST);

        DataOutputStream out = new DataOutputStream(connection.openDataOutputStream());
        out.writeUTF(toSave);        

        out.flush();
        connection.close();
    }

我做错了什么?

【问题讨论】:

    标签: java java-me midp httpconnection


    【解决方案1】:

    你最好试试下面的编码sn-p

    public void saveToLeaderboard() {
    
        String toSave = "Random info I want to save";
        HttpConnection connection = null;
        int response_code=HttpConnection.HTTP_OK-1;
        FileConnection fc=null;
        DataOutputStream dos=null;
        DataOutputStream out
        InputStream dis=null; 
        try
        {    
        connection = (HttpConnection) Connector.open("EXTERNAL URL");
        connection.setRequestMethod(HttpConnection.POST);
    
         out= connection.openDataOutputStream();
        out.writeUTF(toSave);        
    
        out.flush();
        response_code=connection.getResponseCode();
    
        if(response_code==HttpConnection.HTTP_OK)
        {
           dis=connection.openDataInputStream();
           int ch;
    
           fc=(FileConnection)Connector.open(path,Connector.READ_WRITE); //path is a path of the file to be saved 
           fos=fc.openDataOutputStream(); byte temp; while((ch=dis.read()) !=-1) 
           { 
               temp=(byte)ch; fos.write(temp); 
           } 
           fos.flush(); 
         } 
          else 
           { //Status code not ok }
    
        }
        catch(ConnectionNotFoundException cnex){//Connection not found} 
        catch(IOException cnex){//ioe exception} 
        catch(Exception cnex){//exception} 
    
    
        if(out!=null)
        {
           try{
               out.close();
           }
           catch(Exception cnex){//exception} 
        }  
        if(dis!=null)
        {
           try{
               dis.close();
           }
           catch(Exception cnex){//exception} 
        }  
        if(connection!=null)
        {
           try{
               connection.close();
           }
           catch(Exception cnex){//exception} 
        }  
       if(fos!=null)
        {
           try{
               fos.close();
           }
           catch(Exception cnex){//exception} 
        }  
    

    【讨论】:

      【解决方案2】:

      我自己的解决方案:

      我创建了一个服务器端文件(在我的例子中,它是一个 .Net 文件 [.aspx]),我设置了我的外部 url 来调用我的 .Net 页面,其中包含我需要的数据的 url 变量。

      并使用服务器代码(在我的情况下为 C#)将基本保存到 txt 文件。

      public void saveToLeaderboard() throws IOException {
              String toSave = "Data to save";
              InputStream inputStream = null;
              HttpConnection connection = (HttpConnection) Connector.open("External Url" + "?info=" + toSave, Connector.READ_WRITE, true);
              connection.setRequestMethod(HttpConnection.POST);
              inputStream = connection.openInputStream();
              inputStream.close();
              connection.close();
          }
      

      【讨论】:

      • 很好,但这就是我的建议。您需要从连接中获取输入流或响应代码。
      • 还是谢谢你,但你提出了我可以使用哪些方法或流程结构的建议。一天结束时,我保留了相同的代码,但更改了 url 以定位我的服务器端 C# 页面,而不是尝试直接编辑文本文件。
      【解决方案3】:

      要真正触发 HTTP 请求,您需要获取有关 HTTP 响应的任何信息,例如使用 URLConnection.getInputStream()URLConnection.getResposeCode() 的响应正文等。

      更多详情请见How HTTP request works

      【讨论】:

        【解决方案4】:

        我在您的代码中看不到任何 FileOutputStream。 DataOutputStream 应该如何写入文件?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-12-03
          • 1970-01-01
          • 2012-09-15
          • 2021-07-17
          • 1970-01-01
          • 2017-05-31
          • 2017-07-21
          • 2019-01-15
          相关资源
          最近更新 更多