0

I get no errors but my code is not saving any new data to the text file, here is my code:

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

What am I doing wrong?

gnat
  • 6,213
  • 108
  • 53
  • 73
Marc Uberstein
  • 12,501
  • 3
  • 44
  • 72

4 Answers4

0

I can't see any FileOutputStream in your code. How is DataOutputStream supposed to write a file?

iozee
  • 1,339
  • 1
  • 12
  • 24
0

To actually trigger an HTTP request you need to get any information about the HTTP response, such as the response body using URLConnection.getInputStream() or URLConnection.getResposeCode() and so on.

See How HTTP request works for more details.

Community
  • 1
  • 1
Umer Hayat
  • 1,993
  • 5
  • 31
  • 58
0

My own solution at the end of the day :

I created a server side file (in my case it was a .Net file [.aspx]), I set my external url to call my .Net page with url variables that holds my data needed.

And did a basic save to txt file with server code (in my case C#).

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();
    }
Marc Uberstein
  • 12,501
  • 3
  • 44
  • 72
  • Nice but that was what I suggested. You need to get input stream or response code from your connection. – Umer Hayat Jun 21 '12 at 06:50
  • Thanks anyway, but you made suggestions what methods or structure of flow I can use. At the end of the day I kept my same code but changed the url to target my server side C# page instead of trying to edit the text file directly. – Marc Uberstein Jun 21 '12 at 07:09
0

Better you try the following coding snippet

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} 
    }  
SIVAKUMAR.J
  • 4,258
  • 9
  • 45
  • 80