I am hitting a particular URL and getting some response from it. I am storing the response into a string. But my string stores about 3500 characters and then shows ... . How can I make it to store all the response data into the String? Please note that response is a JSON.
Since you are dealing with a large json, better use JsonReader . This way you wont have to worry about those dots, and the outofmemory exceptions.
Try to debug and have a proof by displaying your String.length() value... Is it really 3500 ?
Related
Please i am trying to send from an EditText a text with space between words but nothing inserted in my db !
and when i send a text with no space works and inserted !
please what is the problem here ??
MyAsyncTaskresources attemptLogin= new MyAsyncTaskresources();
attemptLogin.execute("http://xxx.xxxxx.com/add_demande.php?de="+infos_demande.getStringExtra("de").toString()+"&a="+infos_demande.getStringExtra("a").toString()+"&date="+infos_demande.getStringExtra("date").toString()+"&heur="+infos_demande.getStringExtra("heur").toString()+"&id_user="+infos_demande.getStringExtra("id_conducteur"+i).toString());
Have you try adding
N'
as a first part of the string?
You need to Encode the String values using URLEncoder.encode(<Query Value>,"UTF-8") before appending the values to the URL String.
MyAsyncTaskresources attemptLogin= new MyAsyncTaskresources();
attemptLogin.execute("http://xxx.xxxxx.com/add_demande.php?de="+URLEncoder.encode(infos_demande.getStringExtra("de").toString(),"UTF-8")+"&a="+URLENcoder.encode(infos_demande.getStringExtra("a").toString(),"UTF-8")+"&date="+URLEncoder.encode(infos_demande.getStringExtra("date").toString(),"UTF-8")+"&heur="+URLEncoder.encode(infos_demande.getStringExtra("heur").toString(),"UTF-8")+"&id_user="+URLEncoder.encode(infos_demande.getStringExtra("id_conducteur"+i).toString(),"UTF-8"));
First make your code readable, build your string before putting it in attemptLogin.execute(...)
now you have your string build you can check your string, try that and make sure your attemptLogin.execute() argument is a legit string!
using volley is best and fast than use this old process
volley library in android , can send request and get response easy and adding parameter such as string and no problem of space , and there is handle for server error to check where your error
Android JSON parsing is rather straightforward until it comes to have json reserved characters in your keys/values. I have JSON coming from an HTTP socket whose response is put into a string variable. It looks like this
{"ZboAdtPw4bA":"Ben Heck"s PlayStation 4 Slim Teardown","iC4qIx72_Cc":"Ben Heck's Xbox Slim Teardown"}
See the double quotation in the first value? It even screws up on StackOverflows web page. How am I supposed to escape/prevent this from happening? If I do a:
response = response.replace("\"", "");
Then all the double quotation get replaced, not just the ones in the key/value pair. This is because its all contained in one string at the moment. I am wondering if there is an easy way to do this with android. And of course, java answers are acceptable to. Now I could do this since its just a single dimensional key/value pair easily, I may not even need JSON, but I would like to adhere to standards.
you are simply trying to ruin the basic of a JSON .
you simply add
"/"" to the java code .
other than that its not possible for the parser to differentiate between the quotations from the JSON format or the quotations in the string .
I'm currently deveolping an Android application that has Django framework as it's server side.
When i'm posting a data of a new user to my database i am POSTing a multipart request that has a user part inside.
The user for some reason is represented as a list but when i take it out of the request.data['user'] it's a str instance (Yea i dont know why...)
When i fetch that str i started working on it with json package.
I looked up on the internet (to many places..) how to convert a string in json format to a dictionary.
What i found is that when you use the json.loads command it doesn't give a dict back but a str instance :)
This is the code on my server side when i enter the create function of the ModelViewSet that handles the creation of the user.
userJson = request.data['user']
userJson = json.dumps(userJson)
userJson = json.loads(userJson)
What i tried to do is to make a string of my own in JSON format and that called the json.loads() command which gave me the dict object..
There seems to be a problem with processing the str from the http request of django rest framework for some reason or there's something else i am not seeing.
I tried the following links -
Converting JSON String to Dictionary Not List
http://docs.python-guide.org/en/latest/scenarios/json/
Didn't worked also..
Now, i tried accessing the str i got from json.loads() like a dictionary in this way.
id = userJson['id']
Now lets say maybe i passed a wrong json format to the loads function, it should have thrown an exception..
The code above (getting the id) raised an exception of 'String indices must be integer' - it doesn't convert it to dict! LOL xD
Good note worth mentioning - I'm trying to convert the json to a dictionary so i could access it like this - dictObject['id']
Well i would really appreciate every help!
Thanks :)
For some reason , when i did this commands-
userJson = request.data['user']
userJson = json.loads(userJson)
userJson = json.loads(userJson)
What i got to have inside the userJson after the second json.loads(userJson) I got the actual dict object to the userJson member.
Appearently it is a bug.
21 January - another update, I truly was doing double Json encoding on the Android application so that was the reason for double json. loads()
I have to submit data from 30 pages into the server.These datas from 30 pages are to be made into a single string and that i have to upload that single string into the server using json.
Each page may contain many answers tht may be either in plain text(value we receive from edit text),from check boxes(yes or no) and so on.....please suggest me a way to add all these data into a single string and upload it using json.
Based on the comment I suspect that you believe that you need to treat these "pages" as strings that you concat. However, what I think you're overlooking is that JSON is pretty versatile in how you add objects to it.
So, let's say you have the thing that you want to ship to your server and you call it
JSONObject myEntireFile = new JSONObject();
you can now add stuff to it at any time like this...
JSONObject page1 = new JSONObject();
myEntireFile.put("page1", page1);
meanwhile you can put whatever you want IN page 1 (cause that's just another serialized container).
You can keep doing this until you're ready to send it, at which time you just call
myEntireFile.toString();
which will convert your object into one long, well formatted, JSON string, that you can then open store for later use.
Suppose I have a json string like this:
{ ... "key1":"value1"; ... }
with a key1-value1 pair somewhere deep down the json structure (which includes other things such as array, dictionary, etc...). I don't know exactly (and don't care) how the exact structure of the json is.
Is there a simple way to extract the "value1" ? (if there are 2 "key1" in the json string then I just need the first one).
As far as I know, you have no chance of doing it manually.
If you really don't know what's the structure of the JSON string you're expecting, you can try a graph search approach, such as DFS (http://en.wikipedia.org/wiki/Depth-first_search).
For every key, check if it is an array.
If so, go inside and repeat the procedure. If nothing was found in a given array, backtrack.
Interrupt your process once you have found your key.