upload by clicking a button

Discussion in 'iOS Programming' started by Kareny, Jun 23, 2010.

  1. Kareny macrumors newbie

    Joined:
    Jun 21, 2010
    #1
    Hi guys,
    I am trying upload by clicking a button.
    Is there any code between
    -(IBAction)buttonClick: (id) sender

    and

    -(void)Upload

    Thanks very much!
     
  2. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #2
    Post your current code and some details on your server setup and we can help you diagnose your problem.
     
  3. Kareny thread starter macrumors newbie

    Joined:
    Jun 21, 2010
    #3
    Thanks very much and here is my code:


    Code:
    @interface testingViewController : UIViewController 
    <UIActionSheetDelegate> {
    	IBOutlet UITextField *NameField;
    	IBOutlet UIButton    *SUBMITButton;
    	NSMutableData *webData;
    }
    
    @property (retain, nonatomic) UITextField *NameField;
    @property (retain, nonatomic) UIButton * SUBMITButton;
    -(IBAction)textFieldDoneEditing: (id)sender;
    -(IBAction)SUBMITPressed: (id)sender;
    @property (retain, nonatomic) NSMutableData *webData;
    @end
    
    
    #import "testingViewController.h"
    
    @implementation testingViewController
    
    @synthesize NameField;
    @synthesize SUBMITButton;
    @synthesize webData;
    -(IBAction)textFieldDoneEditing: (id)sender
    {
    	[sender resignFirstResponder];
    }
    - (IBAction)SUBMITPressed: (id)sender {
    			if (![sender isKindOfClass:[UIButton class]])
    				return;}//really do not know what code should be
    				
    -(void)Upload
    {
    	NSString *soapMessage	= [NSString stringWithFormat:
    								   @"<?xml version=\"1.0\"encoding=\"utf-8\"?>\n"
    								   "soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"xmls:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
    								   "<soap:Body>\n"
    								   "<Upload xmlns=\"http://tempuri.org/\">\n"
    								   "<name>%@</name>\n"
    												
    								   "</Upload>\n"
    								   "</soap:Body>\n"
    								   "</soap:Envelope>\n",NameField.text
    								   ];
    		
    		
    		NSURL *url = [NSURL URLWithString:@"http://10.11.10.91/vesupload/Service.asmx?wsdl"];
    		NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    		NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMessage length]];
    		
    		[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    		[theRequest addValue:@"http://10.11.10.91/vesupload/UploadImage" forHTTPHeaderField:@"SOAPAction"];
    		[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    		[theRequest setHTTPMethod:@"POST"];
    		[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    		
    		
    		NSURLConnection * theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    		
    		if(theConnection)
    		{
    			webData = [[NSMutableData data]retain];
    			
    		}
    		else {
    			NSLog(@"theConnection is NULL");
    		 }
    }
    The server set up is:
    Code:
    SOAPAction:"http://tempuri.org/Upload"
    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmls:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmls:soap="http://schemas.xmlsoap.org/soap/envelope/">\n"
    								   "<soap:Body>\n"
    								   "<Upload xmlns=\"http://tempuri.org/\">\n"
    								   "<name>string</name>\n"
    												
    								   "</Upload>\n"
    								   "</soap:Body>\n"
                                                                       "</soap:Envelop>\n"
     
  4. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #4
    You don't appear to be calling [self Upload] from your SUBMITPressed: method.
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    Or using code tags to make it easy to read on the forum :p
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    If your code is supposed to upload when the submit button is tapped then your action method should look like

    Code:
    - (IBAction)SUBMITPressed: (id)sender {
         [self Upload];
    }
    which will start your POST.

    Otherwise, tell us what you want to happen and what does happen.
     
  7. Kareny thread starter macrumors newbie

    Joined:
    Jun 21, 2010
    #7
    You got the point~~I really should use the code tags~:p
     
  8. Kareny thread starter macrumors newbie

    Joined:
    Jun 21, 2010
    #8
    Hi, thanks for the help, I want to press the SUBMIT button, then upload the 'name' on to my web server. I put the code in my file and also added
    -(void)Upload;
    into my interface file.

    But still, there was 'name' on my server. Any idea about this?:)
    BTW, the Xcode did not have any warnings when I pressed Build & Run.
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    What do your NSURLConnection delegate methods look like?
     
  10. Kareny thread starter macrumors newbie

    Joined:
    Jun 21, 2010
    #10
    NSURLConnection * theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    No, that's just where you set the delegate, not the delegate methods themselves (like, for example in this case, connection:didReceiveResponse:). If that doesn't make sense, I suggest stepping away from the real coding and going and (re)learning the basics of Objective-C.
     
  12. Kareny thread starter macrumors newbie

    Joined:
    Jun 21, 2010
    #12
    I got the code like this,but still not working.Is the code right?
    Code:
    #pragma mark NSURLConnection delegate methods
    -(void)connection: (NSURLConnection *)connection didReceiveResponse: (NSURLResponse *)response
    {
            [webData setLength: 0];
            NSLog(@"connection: didReceiveResponse:1");
    }
    -(void)connection: (NSURLConnection *)connection didReceiveData: (NSData *)data
    {
            [webData appendData:data];
            NSLog(@"connection: didReceiveData:2");
    
    }
    -(void)connectionDidFinishLoading: (NSURLConnection *)connection
    {
            NSLog(@"3 DONE. Received Bytes: %d", [webData length]);
            NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
            NSLog(theXML);
            [theXML release];
    }
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    The code seems to be right, but only insomuch as it encounters no problems, which obviously you are. Have you coded a connection:didFailWithError:? You say it's not working. Please elaborate? Are you getting run-time errors? How much of the NSLogs you've included are being executed?
     
  14. Kareny thread starter macrumors newbie

    Joined:
    Jun 21, 2010
    #14

    Thanks again for reply!!
    I have coded this:
    Code:
    -(void)connection: (NSURLConnection *)connection didFailWithError: (NSError *)error
    {
    NSLog(@"ERROR with theConnection"):
    [connection release];
    [webData release];
    }
    I do not have any errors or warnings after I click 'Bulid and Run'. The problem is I got nothing on my webservice. I want to send a 'name.text' on my webservice, but I got nothing.
     
  15. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    You should also check for errors in your connection:didReceiveResponse:'s NSHTTPURLResponse (see the SeismicXML sample app for how this might be done).

    I didn't get an answer to this question: How much of the NSLogs you've included are being executed?

    Also, the issue may exist on the server side. Is it receiving the SOAP request and responding appropriately?
     
  16. Kareny thread starter macrumors newbie

    Joined:
    Jun 21, 2010
    #16

    Thanks again for this.
    I did learn the sample and check the NSHTTPURLResponse, there was still nothing.Also the server side was fine. It is receiving the SOAP request, as I have tested it already.

    I think the problem was the NSLogs part, I don't think the NSLogs were executed.
     
  17. Kareny thread starter macrumors newbie

    Joined:
    Jun 21, 2010
    #17
    Hi dejo,
    also I use the breakpoints, it stop at
    NSURL *url = [NSURL URLWithString:mad:"http://10.11.10.91/vesupload/Service.asmx?wsdl"];
    I think maybe the problem is about the conncetion,did I miss some code for the conncetion part?
     
  18. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #18
    Is that the line where you set the breakpoint? If so, then that's where it's supposed to stop.

    I tried putting your code into a simple project (needed to add statics for NameField and webData) and I also am not getting any NSLog output. I tried putting breakpoints in all the NSURLConnection delegate methods and none of them are being triggered. This leads me to believe that the issue lies within the communication with the SOAP server. The NSURLConnection is being initialized fine but neither connection:didReceiveResponse: nor connection:didFailWithError: is being triggered. Also, I don't even get any response when trying to access http://10.11.10.91/ from a browser.
     
  19. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #19
    10.x.x.x is ipv4's private address space. It is never routed onto the internet.

    http://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces

    The OP might want to use the 'tcpdump' command or a GUI network-traffic watcher of some kind, to see what is happening on the actual network. He says the request appears on the server, but is the server sending a response? If not, then none of the requester's delegate methods will be called.

    One might also use the 'curl' command to test the server's response to a known-good request.

    Interarchy 8 has a traffic watcher. Interarchy 9 doesn't. I use 8, so I don't have any suggestions for a GUI traffic watcher.
     
  20. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #20
    Ah. That's what I suspected but didn't realize the private space included everything after the 10. So used to seeing 10.0.1.x. Thanks for clarifying that.
     
  21. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #21
    To the OP:
    Post your most recent code.

    Your original code contains a number of bugs, which will cause nothing to happen. You said you've made changes, but we need to see the actual code resulting from those changes. As it now stands, we're all debugging what you told us about your code; we're not debugging your actual code.

    You should also copy and paste the actual data received by the server. I don't believe you did this before. I surmise this because there is a typo, </soap:Envelop> instead of </soap:Envelop>, which appears in what you said is your server setup, but which doesn't appear in the source code you posted.
     

Share This Page