hi,
I am connecting to web service using arduino wi-fi sheild and receiving data from it. i am using following code to do it
if (client.connect(server, 80)) {
client.println("GET URL HTTP/1.0");
client.println();
}
char json[] = {};
char c ;
while (client.available()) {
c = client.read();
}
json[] = c;
when i am assigning my output to a char array for parsing it i am getting following errror
error: expected primary-expression before ']' token
hi,
Thanks for your quick reply.............now error is not coming but some junk characters are getting added at the end of array. can you please suggest any solution for this ?
can any one please help me to solve this issue....
Fix line 42.
If that doesn't work, there is something wrong with your code. You'll need to print it out, and hold it up in front of your web cam until we get around to reading it.
hi,
Thanks for your reply. sorry to say that i did not understand what you are saying. can you please elaborate your answer ?. The output i am receiving from web service is :
Why are you pissing away resources on the String class? Pay some attention to what the functions you are using actually return.
Serial.println("connected to server");
client.println("GET /iSmartStoreWS/smartStore/ProductCatalogWS/getProductCatalog/2002 HTTP/1.0");
Those string literals get stored in SRAM, unnecessarily. Save your SRAM for useful stuff:
Serial.println(F("connected to server"));
client.println(F("GET /iSmartStoreWS/smartStore/ProductCatalogWS/getProductCatalog/2002 HTTP/1.0"));
char results[] = {};
How many characters can you store in this array? You lied to the compiler, telling it that you would provide enough information for it to determine the size of the array, when you knew you had no intention of doing so.
results[index] = c;
You just shit on memory you do not own. NOTHING else that happens from here on is guaranteed to work.
hi ,
is this problem is due to memory issue ?. i wanted to store the output coming from web service in char array and the data may vary from time to time. so i doesn't know exactly how much space is required. so i didn't specify the size of char array.
hi ,
is this problem is due to memory issue ?. i wanted to store the output coming from web service in char array and the data may vary from time to time. so i doesn't know exactly how much space is required. so i didn't specify the size of char array.
That's like saying you need a bucket, but you don't know how big a bucket you will need, so you get a thimble instead, thinking that that will be big enough.
You MUST specify a size for the array. You need to determine the maximum amount of data you will handle. That might be 20 characters. It might be 100. Whatever size it is, you simply throw away whatever won't fit.
The less SRAM you waste on string literals and String crap, the more you can use to store the data that you need to parse.
hi,
I have modified my code as you suggested and please correct me if some part of code is to be modified but sill i am facing same issue. i am posting modified code please help in solving this issue.
#include <SPI.h>
#include <WiFi.h>
#include <JsonParser.h>
char ssid[] = "AndroidAP";
char pass[] = "inode@123";
int status = WL_IDLE_STATUS;
IPAddress server(202,91,136,148);
WiFiClient client;
void setup(){
Serial.begin(9600);
Serial.println(F("Attempting to connect to network..."));
status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
Serial.println(F("Couldn't get a wifi connection"));
while(true);
}
else {
Serial.println(F("Connected to wifi network"));
}
}
void loop(){
if (client.connect(server, 80)) {
Serial.println(F("connected to server"));
client.println(F("GET /iSmartStoreWS/smartStore/ProductCatalogWS/getProductCatalog/2002 HTTP/1.0"));
client.println();
}
JsonParser<32> parser;
char results[250] = {};
char c ;
int index = 0;
while (client.available()){
c = client.read();
results[index] = c;
index++;
}
results[index] = '\0';
char json[150] = {};
int j = 0;
for(int i = 128;i < index;i++){
if(results[index] != ';'){
json[j]=results[i];
}
j++;
}
Serial.println(json);
delay(10000);
//char test[] = "{\"productCode\":4002,\"productName\":\"Apples\",\"Weight\":250\",\"minOSA\":6}";
JsonHashTable hashTable = parser.parseHashTable(json);
if (!hashTable.success()){
Serial.println(F("JsonParser.parseHashTable() failed"));
}else{
Serial.println(F("sucess"));
}
}
When SHOULD j be incremented? When IS j incremented?
You didn't answer these.
I thought you might take the hint that it is potentially necessary to print all intermediate variables, until you discover what the problem is. Since you didn't, I'll ask explicitly. What is in json when that for loop ends? Is it what you expect?
for(int i = 128;i < index;i++){
json[j]=results[i];
j++;
}
j is incremented to push the values of results char array to json char array. so j is incremented in for loop until all the values in results char array is pushed to json char array. i have moved the output from results char array to json char array to eliminate http response header.
Value in results char array is :
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/plain
Date: Fri, 04 Jul 2014 19:12:07 GMT
Connection: close
"{\"productCode\":4002,\"productName\":\"Apples\",\"Weight\":250\",\"minOSA\":6}"
Issue now is i want to split the output which is in json char array in to key value pairs using the library i mentioned earlier.
For example :
Product code:4001
Weight:250
I want to split Json data which is in json char array like this.