I have a Json Format
Json Url
http://inter.youdao.com/intersearch?tag=simple-eh&from=en&to=hi&q=spin
I need only first i
first i - n. घुमाव; चक्रण; झुकाव
second i. v. घूमना; कातना
Is it possible that first i + second i word only first word = n. घुमाव, v. घूमना
The problem was showing result in android no eh velue.
How to get hindi word.
Json Result
{
"data": {
"eh": {
"": "spɪn",
"ukphone": "spɪn",
"ukspeech": "spin&type=1",
"trs": [
{
"i": "n. घुमाव; चक्रण; झुकाव"
},
{
"i": "v. घूमना; कातना"
}
],
Android Implement :
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
urlJsonObj, null, new Response.Listener<JSONObject>() { #Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
// Parsing json object response
// response will be a json object
JSONObject result = response.getJSONObject("data");
String eh= result.getString("eh");
JSONObject result1 = response.getJSONObject("eh");
String hindi= result1.getString("trs");
jsonResponse = "";
jsonResponse += "Hindi: " + hindi ;
txtResponse.setText(jsonResponse);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
hidepDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
finally fix the Problem
Thanks
here is code :
try {
// Parsing json object response
// response will be a json object
String syncresponse = response.getString("data");
JSONObject object2 = new JSONObject(syncresponse);
String synckey = object2.getString("eh");
JSONObject object3 = new JSONObject(synckey);
JSONArray jArray1 = object3.getJSONArray("trs");
for(int i = 0; i < jArray1 .length(); i++)
{
JSONObject object4 = jArray1.getJSONObject(i);
String comp_id = object4.getString("i");
String replace = object4.getString( "i" ).replace( ";", "," );
translate.setText(mWord + "=" + comp_id);
}
Related
This my response of JSON DATA:
{
"markers": [
{
"error": false,
"title": "name",
"lat": "17.415751",
"lng": "78.443277",
"video": "vlc-record-2020-02-14-16h29m53s-GlintandGlowJewellerypresentation.mp4",
"image": "51457.jpg",
"address": "Hyderabad,
"phone": "4023384555",
"name": "name"
}
]
}
This is my first time making an android application that parses JSON data. However, I'm encountering an error regarding JSON parsing. I've tried Googling the problem but most of the solutions didn't work for me.
My request looks something like this:
RequestQueue req = Volley.newRequestQueue(getApplicationContext());
JSONObject jsonObject = null;
JsonObjectRequest strReq = new JsonObjectRequest(Request.Method.GET,
url,null, new Response.Listener<JSONObject>() {
//http://justasec.in/JustASec/jas.php
#Override
public void onResponse(JSONObject response) {
Log.e(TAG, "response: " + response);
try {
JSONObject obj = new JSONObject(response.toString());
JSONArray commentsObj = obj.getJSONArray("markers");
for (int i = 0; i < commentsObj.length(); i++) {
JSONObject commentObj = (JSONObject) commentsObj.get(i);
String title = commentObj.getString("title");
String address = commentObj.getString("address");
String phone = commentObj.getString("phone");
// String contact_person = commentObj.getString("contact_person");
lat = commentObj.getString("lat");
lng = commentObj.getString("lng");
/*Image = "http://amazearapp.com/jvdweb/images/medium/"+ commentObj.getString("images");
VIDEO_SAMPLE ="http://amazearapp.com/jvdweb/videos/" + commentObj.getString("video");
initializePlayer();
*/
mapView.getMapAsync(DataActivity.this);
Title.setText(title);
//Address.setText(address);
/* if (contact_person != null && !contact_person.isEmpty()) {
ContactPerson.setText(contact_person);
} else {
ContactPerson.setText("NA");
ContactPerson.setVisibility(View.GONE);
}*/
if (phone != null && !phone.isEmpty()) {
Number = phone;
} else {
Number = "NA";
call.setVisibility(View.GONE);
}
sliderImg = new ArrayList<>();
DiscountModel model = new DiscountModel();
model.setName("Image");
model.setImage(commentObj.getString("image"));
sliderImg.add(model);
if (commentObj.getString("video") != null) {
DiscountModel model1 = new DiscountModel();
model1.setName("Video");
model1.setImage(commentObj.getString("video"));
sliderImg.add(model1);
}
Log.v("size", sliderImg.size() + " ");
viewPagerAdapter = new ViewPagerAdapter(sliderImg,DataActivity.this);
viewPager.setAdapter(viewPagerAdapter);
}
} catch (JSONException e) {
Log.e(TAG, "json parsing error: " + e.getMessage());
// Toast.makeText(getApplicationContext(), "json parse error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
NetworkResponse networkResponse = error.networkResponse;
Log.e(TAG, "Volley error: " + error.getMessage() + ", code: " + networkResponse);
// Toast.makeText(getApplicationContext(), "Server down", Toast.LENGTH_SHORT).show();
}
}){
I am parsing json using volley but its not working and getting error. Follwing is my code and json reponse. please help me to solve this
private void getStaffList() {
showpDialog();
RequestQueue requestQueue = Volley.newRequestQueue(this);
final String url = "url";
try {
final JSONObject jsonObj = new JSONObject();
jsonObj.put("username", "test");
jsonObj.put("password", "123456");
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, jsonObj, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("TAG", "Main response=" + response);
staffarraylist=new ArrayList<DataModel>();;
try {
JSONObject jobSuccess=response.getJSONObject("TABLE_DATA");
Log.d("TAG", "JSONObj response=" + jobSuccess);
JSONArray jarMyData=jobSuccess.getJSONArray("data");
Log.d("TAG", "JSONArray response=" + jarMyData);
for (int i = 0; i < jarMyData.length(); i++) {
JSONArray jar = jarMyData.getJSONArray(i);
DataModel movie = new DataModel();
movie.setName(jar.getString(0));
movie.setOccupation(jar.getString(1));
movie.setPlace(jar.getString(2));
movie.setId(jar.getString(3));
movie.setDate(jar.getString(4));
movie.setPrice(jar.getString(5));
staffarraylist.add(movie);
}
}catch (JSONException e)
{
Log.d("JSONException",e.toString());
}
rcAdapter = new RecyclerViewAdapterHome(MainActivity.this, staffarraylist);
recyclerView.setAdapter(rcAdapter);
hidepDialog();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("TAG", "JSONObj Error: " + error.getMessage());
hidepDialog();
//Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
}
});
requestQueue.add(jsonObjReq);
} catch (JSONException e) {
e.printStackTrace();
}
}
Logcat
Main response={"TABLE_DATA":"{\"data\":[[\"Tiger Nixon\",\"System Architect\",\"Edinburgh\",\"5421\",\"2011/04/25\",\"$320,800\"],[\"Garrett Winters\",\"Accountant\",....so on
D/JSONException: org.json.JSONException: Value {"data":[["Tiger Nixon","System Architect","Edinburgh","5421","2011/04/25","$320,800"],["Garrett Winters","Accountant","Tokyo","8422","2011/07/25","$
First check that your json format is correct or not by clicking here and paste your json for checking. If the response is correct then use Gson. It will parse the response without any error.
/**
* Method to make json array request where response starts with [
* Requesting data from url
* */
private String urlJsonArry_local = "http://www.endorecord.in/endorecord/hospitaladmin/Api/device_hospitals.php?deviceid=";
private void makeJsonArrayRequest() {
showpDialog();
JsonArrayRequest req = new JsonArrayRequest(urlJsonArry_local +androidID,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
try {
// Parsing json array response
// loop through each json object
jsonResponse = "";
for (int i = 0; i < response.length(); i++) {
JSONObject person = (JSONObject) response
.get(i);
String name = person.getString("hospitalid");
String email = person.getString("hospital_name");
/*JSONObject phone = person
.getJSONObject("phone");
String home = phone.getString("home");
String mobile = phone.getString("mobile");
*/
jsonResponse += "Name: " + name + "\n\n";
jsonResponse += "Email: " + email + "\n\n";
//jsonResponse += "Home: " + home + "\n\n";
//jsonResponse += "Mobile: " + mobile + "\n\n\n";
}
Toast.makeText(getApplicationContext(), jsonResponse, Toast.LENGTH_LONG).show();
//txtResponse.setText(jsonResponse);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
hidepDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req);
}
you should be passing your url in the request "http://www.google.com"+androidID not this. And the Url urlJsonArry_local that you have mentioned above returns json object response not jsonArray, Your code and Json reponse are completely irrelavant
for the Json respone in above url
private String urlJsonArry_local = "http://www.endorecord.in/endorecord/hospitaladmin/Api/device_hospitals.php?deviceid=";
RequestQueue queue = MyVolley.getRequestQueue();
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,urlJsonArry_local,null/* paramateres passed here*/,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
JSONObject jObject = response;
String status = jObject.getString("status");
JSONArray hospitalArray = jObject.getJSONArray("hospital");
//your for loop to parse hospital array here
hideProgressDialog();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
hideProgressDialog();
}
});
queue.add(jsObjRequest);
I am trying to display data received from a json data source. But I am getting an error when I try to parse the data. I am not able to figure out how to solve it.
The json data looks something like this:
click here to view
Here is the function I wrote for parsing :
public void makeJSONRequest() {
JsonArrayRequest req = new JsonArrayRequest(endpoint_final,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
// UI
swipeRefreshLayout.setRefreshing(false);
for (int i = 0; i < response.length(); i++) {
try {
JSONObject object = response.getJSONObject(i);
JSONObject newsItems = object.getJSONObject("newsItems");
for (int j = 0; j < newsItems.length(); j++) {
titles[j] = newsItems.getString("title");
snips[j] = newsItems.getString("snip");
links[j] = newsItems.getString("link");
}
} catch (JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
Toast.makeText(getActivity(), "JSON Parsing error", Toast.LENGTH_LONG).show();
}
}
mAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error: " + error.getMessage());
Toast.makeText(getContext(), "Error Receiving News", Toast.LENGTH_LONG).show();
}
});
// Adding request to request queue
InitializeVolley.getInstance().addToRequestQueue(req);
}
The error I am getting is :
Error: org.json.JSONException: Value {"newsItems":[{"link":"http://www.srmuniv.ac.in/announcement/special-training-details","snip":"Dear Students,Those who are interested in Placements must follow certain guidelines.You have to clear the tests conducted by CDC and only those...","title":"CDC: Special Training - Updated"}....
Any help will be appreciated.
Thank you!
First of all this json is not an array, it is an object. You should use JsonObjectRequest instead of JsonArrayRequest. You were parsing the json wrongly.
public void makeJSONRequest() {
JsonObjectRequest req = new JsonObjectRequest(endpoint_final,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
// UI
swipeRefreshLayout.setRefreshing(false);
JSONArray newsAr = response.getJsonArray("news");
for (int i = 0; i < newsAr.length(); i++) {
try {
JSONObject newsItem = newsAr.getJSONObject(i);
titles[j] = newsItem.getString("title");
snips[j] = newsItem.getString("snip");
links[j] = newsItem.getString("link");
} catch (JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
Toast.makeText(getActivity(), "JSON Parsing error", Toast.LENGTH_LONG).show();
}
}
mAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error: " + error.getMessage());
Toast.makeText(getContext(), "Error Receiving News", Toast.LENGTH_LONG).show();
}
});
// Adding request to request queue
InitializeVolley.getInstance().addToRequestQueue(req);
}
JSONObject newsItems = object.getJSONObject("newsItems");
replace with
JSONArray newsItems = object.getJSONArray("newsItems");
Instead of writing your own parsing function you could (and definitely should) use a library which does the heavy lifting for you. One example would be Google's Gson library, check it out: https://github.com/google/gson
Parse like :
try {
JSONObject objResponse = new JSONObject(response.toString());
JsonArray arrResponse = objResponse.getJSONArray("newsItems");
for (int i = 0; i < arrResponse.size(); i++) {
JSONObject objItems = arrResponse.get(i);
String link = objItems.getString("link");
}
}catch (Exception e){
e.printStackTrace();
}
JSONObject object =new JSONObject[response];
JSONArray newsItems = object.getJSONArray("newsItems");
for (int j = 0; j < newsItems.length(); j++) {
JsonObject internal=newsItems.getJsonObject(j);
titles[j] = internal.getString("title");
snips[j] = internal.getString("snip");
links[j] = internal.getString("link");
}
Use JSONArray instead of JSONObject class as newsItems is JsonArray.
Try with this:
public void makeJSONRequest() {
JsonArrayRequest req = new JsonArrayRequest(endpoint_final,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
// UI
swipeRefreshLayout.setRefreshing(false);
jo = new JSONObject(response);;
JSONArray array = jo.getJSONArray("newsItems");
for (int x = 0; x < array.length(); x++) {
try {
titles[j] = array.getJSONObject(x).optString("title");
snips[j]= array.getJSONObject(x).optString("snip");
links[j] = array.getJSONObject(x).optString("link");
} catch (JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
Toast.makeText(getActivity(), "JSON Parsing error", Toast.LENGTH_LONG).show();
}
}
mAdapter.notifyDataSetChanged();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error: " + error.getMessage());
Toast.makeText(getContext(), "Error Receiving News", Toast.LENGTH_LONG).show();
}
}
);
// Adding request to request queue
InitializeVolley.getInstance().addToRequestQueue(req);
}
I have followed this tutorial.
The author directly uses the data in the callback by setting the textview within the callback. What I would like to do is populate an array with the response I'm getting from my request, and then be able to use that array elsewhere (as the response listener is an anonymous inner class, I can't figure out how to get data from it; any attempt to assign to an array inside the listener has proved fruitless.
Thanks, please bear with me as I'm still a beginner.
The listener:
JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
try {
// Parsing json array response
// loop through each json object
jsonResponse = "";
for (int i = 0; i < response.length(); i++) {
JSONObject person = (JSONObject) response
.get(i);
String name = person.getString("name");
String email = person.getString("email");
JSONObject phone = person
.getJSONObject("phone");
String home = phone.getString("home");
String mobile = phone.getString("mobile");
jsonResponse += "Name: " + name + "\n\n";
jsonResponse += "Email: " + email + "\n\n";
jsonResponse += "Home: " + home + "\n\n";
jsonResponse += "Mobile: " + mobile + "\n\n\n";
}
txtResponse.setText(jsonResponse);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
hidepDialog();
}
});
EDIT: The code below outlines the problem I'm having. I'm aware this is probably due to misunderstanding or misapplication on my part but still.
private void makeJsonArrayRequest(String url){
//ONLY WAY TO ACCESS INSIDE LISTENER IS TO MAKE FINAL
final ArrayList<String> string = new ArrayList<>();
showpDialog();
JsonArrayRequest req = new JsonArrayRequest(
url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject field = (JSONObject) response.get(i);
string.add(i, field.getString("title"));
//THIS PRINTS OUT ALL MY TITLES CORRECTLY, SHOWING THAT
//THE STRINGS ARRAY IS POPULATED IN THIS SCOPE
Log.d(AppController.TAG, string.get(i));
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(context,
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(AppController.TAG, "Error: " + error.getMessage());
Toast.makeText(context,
error.getMessage(), Toast.LENGTH_SHORT).show();
hidepDialog();
}
}
);
AppController.getInstance().addToRequestQueue(req);
//THIS CAUSES AN OUT OF BOUNDS EXCEPTION, AS IT THINKS THE ARRAY HAS NOT BEEN POPULATED
Log.d(AppController.TAG, string.get(3));
}
I think the problem is using variable string as a copy of actual string inside OnResponse(..).
Try adding static to this variable :
final static ArrayList<String> string = new ArrayList<>();