How to parse data by array in android? [closed] - android

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am developing an android application were I want to parse data through an array to URL API.
Taking an example, there are 5 TextBox and I enter some information in it. Then all the values entered in textView should parse in an array format to That API URL.
Please help!!

This is what i did, note that this is just an example.
final Map<String,String> postParam = new HashMap<String, String>();
for(int i = 0; i < 5; i++)
postParam.put("child_id[" + i + "]", i+"");
You will get :
child_id[0] with value 0
child_id[1] with value i
And goes on.
UPDATE
In your case, you might want to do something like :
postParam.put("child_id[" + i + "]", myEditText.getText.toString());
for each of your edittext.
Feel free to comment if you dont understand my answer or if i miss-understood you.

I hope i am understanding you correctly, you want to put 5 TextBox's entered text into one Array and then send this Array to API.
Try this:
ArrayList<String> textViewTexts = new ArrayList<String>();
// Put all EditText's text to array
// Do this for each EditText
textViewTexts.add(someEditText.getText());
You can then use textViewTexts.toString() and send this to API.
EDIT:
you can parse textViewTexts like this:
for (int i = 0; i < textViewTexts.size(); i++) {
String text = textViewTexts.get(i);
// Do something with text..
}
EDIT2:
you can parse textViewTexts like this:
JSONArray jArray=new JSONArray();
for (int i = 0; i < textViewTexts.size(); i++) {
String text = textViewTexts.get(i);
jArray.put(text);
}
// Send JSONArray to API
jArray.toString();

If you want to send that array to server via an api, then you should send data in JSONArray like this
JSONArray jArray=new JSONArray();
jArray.put(yourTextViewText1);
jArray.put(yourTextViewText2);
jArray.put(yourTextViewText3);
jArray.put(yourTextViewText4);
jArray.put(yourTextViewText5);
and you can send that to server like that
params.put("key",jArray.toString());
Moreover, It is easy for you web developer to parse this JSONArray.

Related

Json - how to parse a url in Android

I want to parse json from a url and use the json data with a listview.
I also want to only list the score and the name, but I have no idea how. Thanks.
{
"level":[
{
"id":1,
"server":[
{"score":33,"name":"Car"},
{"score":72,"name":"Bus"},
]
}
]
}
Do you know how to retrieve the data?
After you retrieve the data, parsing it is very simple. To get each individual item with its attributes I would use the following code:
String responseFromUrl;
JSONObject JSONResponse = new JSONObject(responseFromURL);
JSONArray level = JSONResponse.getJSONArray("level");
//The following loop goes through each object in "level". This is nessecary if there are multiple objects in "level".
for(int i=0; i<level.length(); i++){
JSONObject object = level.get(i);
int id = object.getInteger("id");
JSONArray server = object.getJSONArray("server");
//This second loop gets the score and name for each object in "server"
for(int j=0; j<server.length(); j++){
JSONObject serverObject = server.get(i);
int score = serverObject.getInteger("score");
String name = serverObject.getString("name");
}
}
Obviously replace "responseFromUrl" with the JSON response from the url in string format. If you don't know why I used JSONObject, JSONArray, String, Integer, etc., or are just confused about this, Udacity has a good course for making http connections and parsing JSON responses from APIs.
Link to Udacity Course
You can use the gson library to convert json to an java object
Download the latest jar and import into your project, currently you can download the latest at this link:
https://repo1.maven.org/maven2/com/google/code/gson/gson/2.8.1/gson-2.8.1.jar
After, this will help you:
https://stackoverflow.com/a/23071080/4508758
Hugs!

Wrong arrangement of data from 'for loop'

I have the following code. I added my text fields dynamically. My desired result shown in Genymotion 5.0 (Google Nexus 5) but when I run my app in other devices/actual device the textfields get shuffled. Please help, Thanks in advance.
JSONObject jsonObject = new JSONObject(question.getSublabels());
final EditText[] editTextSublabels = new EditText[jsonObject.length()];
for (int i = 0; i < jsonObject.length(); i++) {
String names = jsonObject.names().get(i).toString();
editTextSublabels[i] = (EditText) LayoutInflater.from(activity).inflate(R.layout.sublabels, null);
editTextSublabels[i].setId(i);
editTextSublabels[i].setHint(jsonObject.getString(names));
sublabelsContainers.addView(editTextSublabels[i], params);
}
You cannot and should not rely on the ordering of elements within a JSON object.
In JSON, an object is defined thus:
An object is an unordered set of name/value pairs.
If you want order to be preserved, you need to redefine your data structure or put it inside a jsonarray
see http://www.json.org.
A JSONObject is a type of map. It does not preserve ordering. If you want to preserve ordering using JSON, you will need to use an array (and matching JSONArray in Java).

Dynamic textview to be visible on opening the app again [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to add a dynamic textview to the page when I click OK in the dialog box. My problem is that I want that textview to be visible even when the app is opened again.
P.S. I can add multiple textviews(1 at a time) and all should be visible on opening app again. Example : Creating a new Playlist and the new playlist name appears always . Can anyone guide me how to do this?
You can store info about added TextView-s in SharedPreferences and when app is opened again get this info from SharedPreferences by getStringSet for example (to get added TextView's key names) and by other methods and create new TextView-s and add them to an activity layout.
ADDITION:
The most universal approach to this task is to save JSONArray which contains TextView-s data in SharedPreferences as a string by using toString() method and when app is opened again read JSONArray from SharedPreferences as a string and fill data of newly created TextView-s.
EXAMPLE:
private JSONArray data;
...
SharedPreferences pref = getSharedPreferences("application", 0);
data = new JSONArray( pref.getString("text_views_data", null) );
List<TextView> tvList = new ArrayList<TextView>();
for (int i = 0; i < data.length(); i++){
JSONObject ob = data.get(i);
TextView tv = new TextView(this);
tv.setText( ob.getString("text") );
tvList.add(tv);
}
...
private saveTextViewData(TextView tv){
JSONObject ob = new JSONObject();
ob.put("text", tv.getText());
data.put(ob);
SharedPreferences preferences = getSharedPreferences("application", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("text_views_data", data.toString() );
editor.commit();
}
You should call saveTextViewData method when you add new TextView.
you can store each the TextView as an object in an array of objects. Then you can save this array in a SharedPreferences then when you open the application get the array from SharedPreferences and add the TextViews to the application dynamically.
This is a simple solution!

android json object can not be convert to jsonarray? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am getting Exception that json object cannot be converted to json array..,when I am showing values from server, here is my code. Please help me.
if(respons!=null){
try{
JSONObject jsonObj=new JSONObject(respons);
JSONArray post=jsonObj.getJSONArray("List of image URLs");
for(int i =0;i<post.length();i++){
String values = post.getString(i);
_issueList.add(values);
}
}
Here is my json exception at this output
{
"message": [
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389346961Quincredible_1-
2.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389346977Quincredible_1-
3.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389346996Quincredible_1-
4.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389347016Quincredible_1-
5.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389347039Quincredible_1-
6.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389347052Quincredible_1-
7.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389347062Quincr32.png"
]
}
This data is coming from server. I don't know why this error is occurring, my images are not showing and moving.
Please help me. Thank you.
if(respons!=null){
try{
JSONObject jsonObj=new JSONObject(respons);
JSONArray post=jsonObj.getJSONArray("message");
for(int i =0;i<post.length();i++){
String values = post.getString(i);
_issueList.add(values);
}
change to:
if(respons!=null){
try{
JSONObject jsonObj=new JSONObject(respons);
JSONArray post=jsonObj.getJSONArray("message");
for(int i =0;i<post.length();i++){
String values = post.getString(i);
_issueList.add(values);
}
}
key for your json array is message not List of image URLs
Change your below line in which you need to get the arraylist of message where you have written wrong key. Other code is fine.
JSONArray post=jsonObj.getJSONArray("List of image URLs");
Change it to
JSONArray post=jsonObj.getJSONArray("message");
As you are getting only the JSONArray in your response there is not need of JSONObject in your code.
You can parse your array as below which will directly give you an array :
try{
JSONArray post=new JSONArray(respons);
for(int i =0;i<post.length();i++){
String values = post.getString(i);
_issueList.add(values);
}

Json object to json array Java (Android)

It's been a while and i'm trying to ignore some frustrating issue i'm having with json things in java, i'm new to this and read alot however, parsing json in javascript or php was alot better (or easier i dunno) but now in java i cannot convert a jsonobject to jsonarray if it doesn't have a parent, cuz it uses .getJsonArray('array)
BUT what IF i have this :
{"49588":"1.4 TB","49589":"1.4 TB MultiAir","49590":"1.4 TB MultiAir TCT","49591":"1.6L MultiJet","49592":"1750 Tbi","49593":"2.0L MultiJet","49594":"2.0L MultiJet TCT"}
i'm not succeeding in anyway to convert it to array
what i want is to convert this JSONObject to JSONArray loop within its items and add them to a Spinner, now that's the first issue, the second question is: if i convert this to JSONArray how can i add the ID, Text to the spinner? just like the HTML Select tag
<option value="0">Item 1</option>
so it's an issue and a question hope someone can find the solution for this jsonarray thing, without modifying the json output from the website, knowing that if i modify and add a parent to this json, the JSONArray will work. but i want to find the solution for that.
Nothing special i have in the code:
Just a AsynTask Response, a log which is showing the json output i put at the beginning of this question
Log.d("response", "res " + response);
// This will work
jsonCarsTrim = new JSONObject(response);
// This won't work
JSONArray jdata = new JSONArray(response);
Thanks !
How about this:
JSONObject json = new JSONObject(yourObject);
Iterator itr = json.keys();
ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
ArrayList<Integer> links = new ArrayList<Integer>();
int i = 0;
while(itr.hasNext()) {
String key = itr.next().toString();
links.add(i,Integer.parseInt(key));
entries.add(i, (CharSequence) json.getString(key)); //for example
i++;
}
//this is the activity
entriesAdapter = new ArrayAdapter<CharSequence>(this,
R.layout.support_simple_spinner_dropdown_item, entries);
//spinner is the spinner the data is added too
spinner.setAdapter(entriesAdapter);
this should work (works for me), you may have to modify the code.
The way shown, i am adding all entries of the json object into a Spinner, where my json key is the index value and the linked String value of the json object will be shown as Spinner entry (title) in my activity.
Now when an Item is selected, fetch the SelectedItemPosition and you can look it up in the "links" array list, to get the real value.
I'm not sure if this is thing you want but give it a try. There is tutorial to convert the Json to Map. After you convert it, you can iterate through the map.
http://www.mkyong.com/java/how-to-convert-java-map-to-from-json-jackson/
What you have is a JSON object type, not an array type. To iterate you can get the Iterator from the keys method.

Categories

Resources