0

I have problem where I don't know. in my flutter project I want to get request. but, I always got error

    E/flutter ( 3700): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FormatException: Unexpected character (at character 396000)
E/flutter ( 3700): ...E PUTIH ALAM  10X20X3CM","ItemType:"Finished Good","ItemClass":"CVL","Po...
E/flutter ( 3700):                                        ^
E/flutter ( 3700): 
E/flutter ( 3700): #0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1405:5)
E/flutter ( 3700): #1      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:935:48)
E/flutter ( 3700): #2      _parseJson (dart:convert-patch/convert_patch.dart:40:10)
E/flutter ( 3700): #3      JsonDecoder.convert (dart:convert/json.dart:506:36)
E/flutter ( 3700): #4      JsonCodec.decode (dart:convert/json.dart:157:41)
E/flutter ( 3700): #5      _ScheduleAddState.getStock (package:myfirstapp/scheduleadd.dart:365:24)
E/flutter ( 3700): <asynchronous suspension>
E/flutter ( 3700): 
                               ^

but I already generate this json data automatically.

I already stuck in this problem,

this is my API

public function StockItemList(){
    $cookie_jar = tempnam("tmp", "cookie");
    //login
        $curl = curl_init();

        curl_setopt_array($curl, array(
          CURLOPT_URL => 'url',
          CURLOPT_RETURNTRANSFER=> true,
          CURLOPT_ENCODING      => '',
          CURLOPT_MAXREDIRS     => 10,
          CURLOPT_TIMEOUT       => 0,
          CURLOPT_FOLLOWLOCATION=> true,
          CURLOPT_HTTP_VERSION  => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'POST',
          CURLOPT_COOKIEJAR     => $cookie_jar,
          CURLOPT_POSTFIELDS    =>'{
            "name"      : "*****",
            "password"  : "*****",
            "company"   :  "**"
        }',
          CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json'),
        )
    );
    $response = curl_exec($curl);
    //get data
    curl_setopt_array($curl, array(
        CURLOPT_URL             => 'url',
        CURLOPT_RETURNTRANSFER  => true,
        CURLOPT_ENCODING        => '',
        CURLOPT_MAXREDIRS       => 10,
        CURLOPT_TIMEOUT         => 0,
        CURLOPT_FOLLOWLOCATION  => true,
        CURLOPT_HTTP_VERSION    => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST   => 'GET',
        CURLOPT_COOKIEFILE      => $cookie_jar
        )
    );
    $datapo = json_decode(curl_exec($curl));
    //logout
    curl_setopt_array($curl, array(
        CURLOPT_URL             => 'url',
        CURLOPT_RETURNTRANSFER  => true,
        CURLOPT_ENCODING        => '',
        CURLOPT_MAXREDIRS       => 10,
        CURLOPT_TIMEOUT         => 0,
        CURLOPT_FOLLOWLOCATION  => true,
        CURLOPT_HTTP_VERSION    => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST   => 'POST',
        CURLOPT_POSTFIELDS      => '{   
            "name" : "*****",
            "password" : "******",
            "company" :   "**"
        }',
        CURLOPT_COOKIEFILE      => $cookie_jar
        )
    );
    $response = curl_exec($curl);
    $data = array();
    $no = 0;
    foreach ($datapo as $doc) {
        $data[$no]['InventoryID'] = $doc->InventoryID->value;
        $data[$no]['Description'] = $doc->Description->value;
        $data[$no]['ItemType'] = $doc->ItemType->value;
        $data[$no]['ItemClass'] = $doc->ItemClass->value;
        $data[$no]['PostingClass'] = $doc->PostingClass->value;
        $data[$no]['TaxCategory'] = $doc->TaxCategory->value;
        $data[$no]['BaseUOM'] = $doc->BaseUOM->value;
        $data[$no]['ItemStatus'] = $doc->ItemStatus->value;
        $no++;
    }
    return json_encode($data);
}

this is my flutter

getdataStock() async{
// await _getToken();
return await http.get(Uri.parse(_url+"/StockItemList"), headers: _setHeaders());}

Future<String> getStock() async {
FocusScope.of(context).requestFocus(FocusNode());
setState(() {
  _isLoading = true;
});
var res = await Network().getdataStock();
var resBody = json.decode(res.body);
// if(resBody['success']){
  setState(() {
    print(resBody['StockItem']);
    // _StockItemData = resBody['StockItem'];
  });
// }
setState(() {
  _isLoading = false;
});
return "success";}

this is my API Flutter

getdataStock() async{
// await _getToken();
return await http.get(Uri.parse(_url+"/StockItemList"), headers: _setHeaders());}

and in API call another API. where in API Source have 6000+ rows data. it's always lost some " or have "". and this is makes error

8
  • Honestly, it seems more of an API problem rather than Flutter JSON parsing. Commented Mar 2, 2022 at 8:58
  • if it like that, the problem in my API or API on I calling / using ? and do you know how I can fix it ? thank you Commented Mar 2, 2022 at 9:24
  • The problem is mostly related to how the response body is built. To confirm this, you should try to get this data using CURL or Postman - just to see what kind of response is returned. From your example, I see that your API is built using PHP - if you need to adjust something here, that's out of scope for this question. Commented Mar 2, 2022 at 9:28
  • I already testing on my postman too. but, in postman is fine. I thing it has problem on my api calling on my flutter Commented Mar 2, 2022 at 9:33
  • or I can directly call External API on my flutter. but how I can build like my api doing. using temp session ? Commented Mar 2, 2022 at 10:05

1 Answer 1

1

In here you can use skip and post on your API to take limited data

$skip = (string)(((int)$Page - 1)*50);
    $cookie_jar = tempnam("tmp", "cookie");
    //login
        $curl = curl_init();

        curl_setopt_array($curl, array(
          CURLOPT_URL => 'Login URL',
          CURLOPT_RETURNTRANSFER=> true,
          CURLOPT_ENCODING      => '',
          CURLOPT_MAXREDIRS     => 10,
          CURLOPT_TIMEOUT       => 0,
          CURLOPT_FOLLOWLOCATION=> true,
          CURLOPT_HTTP_VERSION  => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'POST',
          CURLOPT_COOKIEJAR     => $cookie_jar,
          CURLOPT_POSTFIELDS    =>'{
            "name"      : "****",
            "password"  : "****",
            "company"   :  "****"
        }',
          CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json'),
        )
    );
    $response = curl_exec($curl);
    //get data
    curl_setopt_array($curl, array(
        CURLOPT_URL             => 'Get URL ?$top=50&$skip='.$skip,
        CURLOPT_RETURNTRANSFER  => true,
        CURLOPT_ENCODING        => '',
        CURLOPT_MAXREDIRS       => 10,
        CURLOPT_TIMEOUT         => 0,
        CURLOPT_FOLLOWLOCATION  => true,
        CURLOPT_HTTP_VERSION    => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST   => 'GET',
        CURLOPT_COOKIEFILE      => $cookie_jar
        )
    );
    $datapo = json_decode(curl_exec($curl));
    //logout
    curl_setopt_array($curl, array(
        CURLOPT_URL             => 'Logout URL',
        CURLOPT_RETURNTRANSFER  => true,
        CURLOPT_ENCODING        => '',
        CURLOPT_MAXREDIRS       => 10,
        CURLOPT_TIMEOUT         => 0,
        CURLOPT_FOLLOWLOCATION  => true,
        CURLOPT_HTTP_VERSION    => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST   => 'POST',
        CURLOPT_POSTFIELDS      => '{   
            "name" : "****",
            "password" : "****",
            "company" :   "****"
        }',
        CURLOPT_COOKIEFILE      => $cookie_jar
        )
    );
    $response = curl_exec($curl);
        
    $data = array();
    $no = 0;
    foreach ($datapo as $doc) {
        $row = array();
        $row[] = '<button type="button" class="btn btn btn-primary" id="add'.$doc->InventoryID->value.'" onclick="addFunction('."'".$doc->InventoryID->value."'".','."'".$doc->Description->value."'".','."'".$doc->ItemClass->value."'".','."'".$doc->BaseUOM->value."'".')">Add</button>';
        $row[] = $doc->InventoryID->value;
        $row[] = $doc->Description->value;
        $row[] = $doc->ItemType->value;
        $row[] = $doc->ItemClass->value;
        $row[] = $doc->PostingClass->value;
        $row[] = $doc->TaxCategory->value;
        $row[] = $doc->BaseUOM->value;
        $row[] = $doc->ItemStatus->value;
        foreach ($doc->WarehouseDetails as $doc1) {
            $row[] = $doc1->QtyOnHand->value;
        }
        $data[] = $row;
    }
    $output = array(
        'data' => $data,
    );
    echo json_encode($output);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.