-1

Here is the below code I edited. Please help me out. I was trying to display the data in list view.

     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];


     NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
     NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
                                                 cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                             timeoutInterval:30];

     if ([response statusCode] >= 200 && [response statusCode] < 300)
     {
         NSString *responseData = [NSJSONSerialization JSONObjectWithData:urlData
                                                                  options:NSJSONReadingAllowFragments error:&error];
          NSLog(@"responseData : %@", responseData);
         NSArray *entries = [NSJSONSerialization JSONObjectWithData:[responseData dataUsingEncoding:NSUTF8StringEncoding]
                                                            options:0 error:&error];


When I keep a log for NSURL it is navigating to the web service and gives me a result that I need, but the problem arises at responseData
user3440782
  • 144
  • 1
  • 14
  • 2
    So, what does `error` tell you? – Hot Licks Nov 03 '14 at 16:58
  • Yes, the error would be useful too. Also, what IS happening? You didn't say what was going wrong. – Fogmeister Nov 03 '14 at 16:59
  • And your code appears to be badly mucked up. Is `responseData` really an NSString??? – Hot Licks Nov 03 '14 at 17:02
  • @HotLicks he's taking the string and converting it back to data. – Fogmeister Nov 03 '14 at 17:03
  • Wait... what? You're... ok, just look at my answer. It will fix it. – Fogmeister Nov 03 '14 at 17:03
  • @Fogmeister - It's unclear what he's doing. – Hot Licks Nov 03 '14 at 17:04
  • @HotLicks yeah, I'd assumed the top line was `stringFromData`. – Fogmeister Nov 03 '14 at 17:05
  • When I use NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30]; NSLog(@"urlData1 : %@", urlData); NSLog(@"urlRequest : %@", urlRequest); The Log I got is correct.. – user3440782 Nov 03 '14 at 17:06
  • We have no idea what data you're getting where. For starters, convert `urlData` to NSString and log that -- show us the results (edit into your question). Then we will have a starting point. – Hot Licks Nov 03 '14 at 17:08
  • 1
    Can you post the actual data that came from urlData? From your code it looks like you're double encoding your JSON on the server, something like: `"[{\"ID_PROJECT:\"...` – Brian Nickel Nov 03 '14 at 17:17

1 Answers1

-1

You are converting your data into a string and then back into data.

Just use the data that you get in the first place...

urlData = [NSURLConnection sendSynchronousRequest:urlRequest
                                returningResponse:&response
                                            error:&error];

if ([response statusCode] >= 200
    && [response statusCode] < 300)
{
    NSArray *entries = [NSJSONSerialization JSONObjectWithData:urlData
                                                       options:NSJSONReadingAllowFragments
                                                         error:&error];

    // do something with the JSON object
}

This should be all you need to do.

EDIT

OK, do this and report back what it says...

urlData = [NSURLConnection sendSynchronousRequest:urlRequest
                                returningResponse:&response
                                            error:&error];

if ([response statusCode] >= 200
    && [response statusCode] < 300)
{
    NSString *theJSONString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];

    NSLog(@"The JSON is... %@", theJSONString);
}

This will at least give us an idea of what exactly is coming through.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • When I use the above code, Result is 2014-11-03 12:02:23.983 TimesheetIOS[24108:4038141] responseData : (null) 2014-11-03 12:02:23.984 TimesheetIOS[24108:4038141] Error : Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7d1253c0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} – user3440782 Nov 03 '14 at 17:03
  • @user3440782 please just show the JSON that you get from the api. Don't try to convert it to anything. Just print the JSON. – Fogmeister Nov 03 '14 at 17:06
  • @user3440782 I've edited the answer slightly. Try the new code. If that doesn't work please try the debugging stuff I put at the end so we can try to diagnose what is going on. – Fogmeister Nov 03 '14 at 17:15
  • Here we go [{\"ID_PROJECT\":0,\"NM_PROJECT\":\"INVALID\",\"ID_TASK\":0,\"TASk_NAME\":\"INVALID\",\"ID_SUB_TASK\":0,\"SUBTASK_NAME\":\"INVALID\",\"No_Hours\":0,\"dt_event\":\"INVALID\",\"supervisor\":\"INVALID\",\"ID_TIMESHEET_DTLS\":0,\"STATUS\":null,\"FL_APPROVE\":\"0\",\"NM_LEAVE\":null}] – user3440782 Nov 03 '14 at 17:41
  • @user3440782 did you try the (slightly updated) code in the first code snippet? Does it still show the same error? Also, is that **exactly** what was logged out? – Fogmeister Nov 03 '14 at 17:42
  • Yes is the above result I Posted just now – user3440782 Nov 03 '14 at 17:44
  • [{\"ID_PROJECT\":0,\"NM_PROJECT\":\"INVALID\",\"ID_TASK\":0,\"TASk_NAME\":\"INVA‌​LID\",\"ID_SUB_TASK\":0,\"SUBTASK_NAME\":\"INVALID\",\"No_Hours\":0,\"dt_event\":‌​\"INVALID\",\"supervisor\":\"INVALID\",\"ID_TIMESHEET_DTLS\":0,\"STATUS\":null,\"‌​FL_APPROVE\":\"0\",\"NM_LEAVE\":null}] – user3440782 Nov 03 '14 at 17:45
  • @user3440782 yes please. It's not really clear what you are doing or what is going wrong. – Fogmeister Nov 03 '14 at 17:56
  • Why are you converting the data into a string? It makes no sense? – Fogmeister Nov 03 '14 at 18:17
  • Do we have any option without converting the data to a string and displaying in List view? – user3440782 Nov 03 '14 at 18:29
  • Here is the result when we keep a log for NSData if it is helpful for you. <225b7b5c 2249445f 50524f4a 4543545c 223a302c 5c224e4d 5f50524f 4a454354 5c223a5c 22494e56 414c4944 5c222c5c 2249445f 5441534b 5c223a30 2c5c2254 41536b5f 4e414d45 5c223a5c > – user3440782 Nov 03 '14 at 18:32
  • @user3440782 you are not trying to create a string. You are trying to create an NSArray. **Did you try my updated code?** you still haven't answered it? I added the option for the NSJSON function. – Fogmeister Nov 03 '14 at 18:47