0

エラーは発生しませんが、コードが新しいデータをテキスト ファイルに保存していません。これが私のコードです。

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();
    }

私は何を間違っていますか?

4

4 に答える 4

0

一日の終わりに私自身の解決策:

サーバー側のファイル(私の場合は.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();
    }
于 2012-06-20T12:00:39.247 に答える
0

次のコーディング スニペットを試してみてください

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} 
    }  
于 2012-06-25T09:43:49.670 に答える
0

コードに FileOutputStream が表示されません。DataOutputStream はどのようにファイルを書き込むことになっていますか?

于 2012-06-20T10:36:06.887 に答える
0

HTTP リクエストを実際にトリガーするには、HTTP レスポンスに関する情報を取得する必要がありURLConnection.getInputStream()ますURLConnection.getResposeCode()

詳細については、 HTTP リクエストの仕組みを参照してください。

于 2012-06-20T10:37:40.003 に答える