Question

Hi in my application i have stored data in my local database sqlite3. Now i have to upload the data form local database sqlite3 to online server. For retrieving the data i have used the dataDictionary to get all the data form the sqlite3 database.

In my h file i have my url to insert the data in to my online server like this.

  #define kPostURL @"url"
  #define Kname @"name"
  #define kphone @"phone"
  #define karea @"area"
  #define kcity @"city"

In this below code i have stored the all the data in data dictionary and i have stored the data into a nsmutable array called array. If i print the array in log its showing all the data. The problem is now how to pass this nsmutable data into online server using the post method.

   NSMutableArray *array = [[NSMutableArray alloc] init];
   entries = [[NSMutableArray alloc] init];
   [self openDB];
   NSString *sql = [NSString stringWithFormat:@"SELECT * FROM reg"];
   const char *query_stmt = [sql UTF8String];
   sqlite3_stmt *statement;

  if (sqlite3_prepare_v2(db, query_stmt, -1, &statement, nil)==SQLITE_OK) {
     while (sqlite3_step(statement)==SQLITE_ROW) {

        NSMutableDictionary *_dataDictionary=[[NSMutableDictionary alloc] init];

        NSString *date = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
        field1Str= date;

        NSString *customer = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
        field2Str = customer;

        NSString *code1 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
        field3Str = code1;

        NSString *code2 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,3)];
        field4Str = code2;

        //NSString *str = [[NSString alloc]initWithFormat:@"%@ - %@ - %@ - %@",field1Str,field2Str,field3Str,field4Str];
        //[entries addObject:str];

        [_dataDictionary setObject:[NSString stringWithFormat:@"%@",field1Str] forKey:@"Kname"];
        [_dataDictionary setObject:[NSString stringWithFormat:@"%@",field2Str] forKey:@"kphone"];
        [_dataDictionary setObject:[NSString stringWithFormat:@"%@",field3Str] forKey:@"karea"];
        [_dataDictionary setObject:[NSString stringWithFormat:@"%@",field4Str] forKey:@"kcity"];

        [array addObject:_dataDictionary];

In this below code I'm passing the data using post method to the online server that the data has already declare in the .m file but i don't how to nsmutablearray in the post method.

  -(void) withName:(NSString *) na phone:(NSString *) ph are:(NSString *) arr cit:(NSString *) citt {


     if (na!= nil && ph != nil && arr != nil && citt != nil ) {

      NSMutableString *postString = [NSMutableString stringWithString:kPostURL];

      [postString appendString:[NSString stringWithFormat:@"?%@=%@", Kname,na]];
      [postString appendString:[NSString stringWithFormat:@"&%@=%@", kphone,ph]];
      [postString appendString:[NSString stringWithFormat:@"&%@=%@", karea,arr]];
      [postString appendString:[NSString stringWithFormat:@"&%@=%@", kcity,citt]];



      [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

      NSMutableURLRequest *request =[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
      [request setHTTPMethod:@"POST"];

      postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

    }
   }

In this above code I'm passing the data by a button click . Please tell how to make it have been stuck here for long time its eating my head. Please give me some solution.

   - (IBAction)send:(id)sender {
      [self withName:field1Str phone:field2Str are:field3Str cit:field4Str];
     }

Thanks.

Was it helpful?

Solution

It depends on what the URL format that the server accepts is.

The easiest answer, but also the worst one (less future-proof), if the server supports it, is to use an URL like: ?kname[0]=x&kname[1]=y to pass two entries.

However if you want to post a lot of data at the same time I would recommend using the body of the request, not the URL, because you'll hit the maximum URL length pretty soon.

So you should first design your server to accept an array of records, and then use the format defined on the server side in your client. Typically a good solution would be to use NSJSONSerialization to serialize your array of dictionaries on the client side, set the JSON string as the request's body, and parse the JSON server-side.

Also, you should escape each of the strings before adding them to your URL. Otherwise how will you make a difference between the & separating your fields and any & appearing in your strings?

OTHER TIPS

You have to first understand what parameters your webservice required. For sending data to the server you may create XML structure based on you array content and pass it to the webservice parameter. You may create XMl like below

<record>
<name>value</name>
<city>value</city>
.
.
</record>

and repeat set of record number of required times.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top