JSON parsing problem in Android with Volley library - arrays

I am trying to get JSON response from server and then display it in my android but failing to do so due to parsing error
Android error
Caused by: org.json.JSONException: Value [{"Result":"Login Success","User_Name:":"Ayan"}] of type org.json.JSONArray cannot be converted to JSONObject
enter image description here

In your response, you have the field like "User_Name:" or did you wrote the response wrong?
check the second field clearly ["User_Name:":"Ayan"]
JsonObject object = new JsonObject(response);
String result = object.getString("Result");
String userName = object.getString("User_Name:");
It should match as like the field else it will give the exception

try this
JSONArray jsonarray = new JSONArray(response);
JsonObject object = jsonarray.getJSONObject(0);
String result = object.getString("Result");
String userName = object.getString("User_Name:");

Related

JSON Object parsing problem in Android with Volley library

I am trying to get JSON response from server and then display it in my android but failing to do so due to parsing error
my server response jsonformat= {
"Result": "Login Success",
"User_Name:": "Ayan"
}
But in android I can't ge the second object and it is returned as null
enter image description here
In your response, you have the field like "User_Name:" or did you wrote the response wrong?
JsonObject object = new JsonObject(response);
String result = object.getString("Result");
String userName = object.getString("User_Name:");
check this #Ayan

Partial document update in SOLR 6.5.1

I have ID(unique key) and URL fields in my indexed document. They have same values. And I am able to update the URL field(changing the DNS) as following:
{"id":"ABC.com/content/dam/images/infographics/Infographic_Final.pdf","url":{"set":"XYZ.com/content/dam/images/infographics/Infographic_Final.pdf"}}
what i am trying to achieve is i have 1000 documents having field ID starting with ABC.com. It should update URL field with XYZ.com and the rest of the URL-path must remain the same. can i achieve this? i dont want to update URL repeatedly for 1000 times.
Thanks in advance.
if you are asking for a 'bulk update' like SQL's 'UPDATE table WHERE...' that is not possible in Solr. You have to submit each doc (mind you, you can submit many docs in one request, but all doc's info must be in there).
I was able to achieve it using Java program. I used Solr-query to find all ID starting with ABC.com. I got URL corresponding to that ID and replaced ABC.com with XYZ.com and kept the rest of the path same. Used set command and updated all the URLs(only URL field) using while loop.
String urlString = "http://localhost:8090/solr/collectionName";
SolrClient solrClient = new HttpSolrClient.Builder(urlString).build();
SolrQuery query=new SolrQuery();
query.setQuery("id:*ABC*");
query.setRows(2147483647);
QueryRequest req = new QueryRequest(query);
QueryResponse response = req.process(solrClient);
SolrDocumentList docList=response.getResults();
Iterator <SolrDocument> itr=docList.iterator();
String IdValue="";
Map<String, String> cmd1;
Map<String, String> cmd2;
UpdateRequest ureq=new UpdateRequest();
while(itr.hasNext()){
JSONObject resultItems = new JSONObject();
SolrDocument doc= itr.next();
IdValue=(String)doc.getFieldValue("id");
SolrInputDocument newdoc = new SolrInputDocument();
cmd1 = new HashMap<String, String>();
String URL=IdValue.replace("www.ABC.com", "www.XYZ.com");
cmd1.put("set", URL);
newdoc.addField("id", IdValue);
newdoc.addField("url", cmd1);
ureq.add(newdoc);
cmd1=null;
cmd2=null;
}
NamedList res = solrClient.request(ureq);
System.out.println(" response "+res);
solrClient.commit();
solrClient.close();

How to send dictionary with array using JSON?

My fellow co-worker backend programmer said that he has configure an API that expect to receive something like this from my mobile app:
[{"id":50},{"id":60}]
I'm using Alamofire which receive param dictionary to be sent. But I believe this is also the same mechanism using NSURLSession or any other third party plugins.
The question is: how should I construct the dictionary to send an array of ids, like how a HTTP form can have several text field with the same id, and then it will be received on the server end as an array of id? What I've tried so far and all fails:
param.setValue(50, forKey:"id");
param.setValue(60, forKey:"id");
// this only send the last single value
param.setValue([50, 60], forKey:"id");
// this results in error (415 unsupported media type)
param.setValue(50, forKey:"id[0]");
param.setValue(60, forKey:"id[1]");
// this also results in error (415 unsupported media type)
How can I send this correctly just like how web form send? Thanks.
I think keyword for your question is "Alamofire send json"
If your server accepts json data, you can do like this:
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let values = [["id":50],["id":60]]
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(values, options: [])
Alamofire.request(request)
.responseJSON { response in
// do whatever you want here
}
Good luck!
The problem with the first method is you're overwriting the value for the key id, that's the reason why it only sends the last value. Try sending the params as an array.
let dict = NSMutableArray()
param.setValue(50, forKey:"id")
dict.addObject(param)
param.setValue(60, forKey:"id")
dict.addObject(param)
Pass the dict as the parameter for the request method.

Trying to retrieve first string from PubNub history Response

I know that the response from the pubnub history() is:
[["message","Message","message"],"Start Time Token", "End Time Token"]
im creating an string to receive the response:
String msg = response.toString();
And this should give me the full array, but now to retrieve the first message im doing this:
String[] msgOne = msg[0];
And this is not working.
for pubnub history method , the response is a org.json.JSONArray so to get the messages array you can use something like this.
JSONArray messages = (JSONArray)( ((JSONArray)response).get(0));
JSONArray class here http://www.json.org/javadoc/ provides more info about the methods that you can use on messages variable.

quoted format json from Solrj

It looks like the QueryResponse from Solrj has no mean to give you a quoted Json string with wt=on or not. All I received is something like this
{responseHeader={status=0,QTime=2,params= {fl=id,productName,imageFront,priceEng,priceEngExp...
Question:
1) Am I missing something here ? Or there is no way to get the json response properly from the Solr server by Solrj.
2) Now on my client, if I convert the non-quoted json string from Solrj, does it mean it was done two times, once in server time and one in the Solrj client time ?
You can get JSON response by setting wt=json to the Solr query. Example URL is shown below :
localhost:8983/solr/select/?q=:&rows=10&indent=on&wt=json
You can't get JSON response using Solrj. You don't need to use Solrj for this purpose.By sending HTTP requests to the URL above, you can get json response.
With newer versions of Solr (starting with 4.7.0) it is possible to return the query response directly in json-format. This can be done with the NoOpResponseParser.
SolrQuery query = new SolrQuery();
QueryRequest req = new QueryRequest(query);
NoOpResponseParser rawJsonResponseParser = new NoOpResponseParser();
rawJsonResponseParser.setWriterType("json");
req.setResponseParser(rawJsonResponseParser);
NamedList<Object> resp = mySolrClient.request(req);
String jsonResponse = (String) resp.get("response");
System.out.println(jsonResponse );

Resources