Im trying to parse JSON and my code is not working with one URL and it works with other. I think its the coma in question but I did not sure so please look at my code and help.
I tried to replace coma with dot in loop, but what i get is JSON Exception. And when I change the URL, there is no exception. Confusing...
This is not working URL: http://api.hnb.hr/tecajn/v1
And this is one I tested and it works: https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=e402c76fc8584a1c81849179f1277a74
While changed the URL I also changed the data, so its not the problem in writing..I guess
Here is my code for coma replacement:
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new
InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
String coma = line.replaceFirst(",",".");
sb.append(coma).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
And the other part with URL and JSONArray:
#Override
protected Void doInBackground(Void... arg0) {
URLconnection urlConn = new URLconnection();
// Making a request to url and getting response
String url = "http://api.hnb.hr/tecajn/v1";
//.........connection.........
String response = urlConn.makeServiceCall(url);
Log.e(TAG, "Response from url: " + response);
if (response != null) {
try {
JSONObject jsonObj = new JSONObject(response);
// Getting JSON Array node
arr = jsonObj.getJSONArray("values");
for (int i = 0; i < article.length(); i++) {
JSONObject c = arr.getJSONObject(i);
header = c.getString("Valuta");
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error.");
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: 216",
Toast.LENGTH_LONG).show();
}
});
}
Commas aren't the issue. It's the very first character. One is a square bracket and the other is a curly one.
You therefore need to parse the first response as an Array rather than an Object
new JSONArray(response);
That being said, you therefore cannot use the same methods for both URLs
Related
i am currently trying to parse a simple JSON information but can't figure out the JSON object and array part... I'm trying to extract from this JSON(below) the app_time and postcode + address. Can anyone give me a solution about my "extractFeatureFromJson()", sorry about the formatting it's my first post here.
{
"data": [
{
"id": 24256,
"app_time": 1904280242,
"addresses": [
{
"id": 1,
"postcode": "9000",
"address": "Street:Street, City: City, Country: Country"
}
],
"comments": [
{
"id": 1,
"comment": "Comment",
"created_at": 234234234
}
]
}
]
}
public static final String LOG_TAG = MainActivity.class.getSimpleName();
private static final String _URL = "https://.......com/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ScheduleAsyncTask task = new ScheduleAsyncTask();
task.execute();
}
private void updateUi(Event job) {
TextView titleTextView = (TextView) findViewById(R.id.time);
titleTextView.setText(getDateString(job.time));
TextView dateTextView = (TextView) findViewById(R.id.address);
dateTextView.setText(job.address);
}
private String getDateString(long timeInMilliseconds) {
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
return formatter.format(timeInMilliseconds);
}
private class ScheduleAsyncTask extends AsyncTask<URL, Void, Event> {
#Override
protected Event doInBackground(URL... urls) {
// Create URL object
URL url = createUrl(_URL);
// Perform HTTP request to the URL and receive a JSON response back
String jsonResponse = "";
try {
jsonResponse = makeHttpRequest(url);
} catch (IOException e) {
// TODO Handle the IOException
}
Event jobs = extractFeatureFromJson(jsonResponse);
return jobs;
}
#Override
protected void onPostExecute(Event job) {
if (job == null) {
return;
}
updateUi(job);
}
/**
* Returns new URL object from the given string URL.
*/
private URL createUrl(String stringUrl) {
URL url = null;
try {
url = new URL(stringUrl);
} catch (MalformedURLException exception) {
Log.e(LOG_TAG, "Error with creating URL", exception);
return null;
}
return url;
}
private String makeHttpRequest(URL url) throws IOException {
String jsonResponse = "";
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("X-Application", ".....");
urlConnection.connect();
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} catch (IOException e) {
// TODO: Handle the exception
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
// function must handle java.io.IOException here
inputStream.close();
}
}
return jsonResponse;
}
private String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
private Event extractFeatureFromJson(String scheduleJSON) {
try {
JSONObject baseJsonResponse = new JSONObject(scheduleJSON);
JSONArray featureArray = baseJsonResponse.getJSONArray("comments");
// If there are results in the features array
// Extract out the first feature
JSONObject firstFeature = featureArray.getJSONObject(0);
JSONObject properties = firstFeature.getJSONObject("comment");
// Extract out the time address values
String address = properties.getString("address");
long time = properties.getLong("app_time");
// Create a new {#link Event} object
return new Event(address, time);
} catch (JSONException e) {
Log.e(LOG_TAG, "Problem parsing the JSON results", e);
}
return null;
}
}
}
As per your json structure, data is json array and so is addresses and comments, so you have to work your way down to those json arrays and finally json objects.
One way to understand json structure (objects and array is to format it and view it. Use online json formatters like https://jsonformatter.org/ or install plugins in notepad++ to format json.
NOTE: I am not giving you full solution as it is for your own benefit so here is what you should do and add debugger points and system.out.println (Log.d in android) for understanding json object and array you traverse and learn yourself.
JSONObject obj = new JSONObject(str);
JSONObject appTime = obj.getJSONArray("data").getJSONObject(0).getJSONObject(“app_time”);
JSONObject postal_code = obj.getJSONArray("data").getJSONObject(0).getJSONArray(“addresses”).getJSONObject(0).getJSONObject(“postcode”);
You have to add appropriate null checks at each step to avoid NPE.
You can also use JSONObject’s methods to retrieve specific data type (getString, getLong, etc.)
https://developer.android.com/reference/org/json/JSONObject.html
if (scheduleJSON!= null) {
try {
JSONObject jsonObj = new JSONObject(scheduleJSON);
// Getting JSON Array node
JSONArray data= jsonObj.getJSONArray("data");
// looping through All data
for (int i = 0; i < data.length(); i++) {
JSONObject id = data.getJSONObject(id);
JSONObject app_time=data.getJSONObject(app_time);
String id = c.getString("id");
// Getting JSON Array node
JSONArray addresses=new JSONArray(data.getJSONObject(i).getString("addresses"));
JSONArray comments= new JSONArray(data.getJSONObject(i).getString("comments"));
}
}catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
}
Everything seems to be fine except for the JSON exception error.
Here it is specifically: org.json.JSONException:
Value username of type `java.lang.String` cannot be converted to `JSONObject`.
There are a number of other flags that I can check in logcat and everything seems pretty good except that. I think the problem is that I need to convert the String encodedStr into a JSON object and return it to ASYNC TASK.
private class AsyncDataClass extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
//Use HashMap, it works similar to NameValuePair
Map<String, String> dataToSend = new HashMap<>();
dataToSend.put("username", params[1]);
dataToSend.put("password", params[2]);
//Server Communication part - it's relatively long but uses standard methods
//Encoded String - we will have to encode string by our custom method (Very easy)
String encodedStr = getEncodedData(dataToSend);
//Will be used if we want to read some data from server
BufferedReader reader = null;
//Connection Handling
try {
//Converting address String to URL
URL url = new URL(serverUrl);
//Opening the connection (Not setting or using CONNECTION_TIMEOUT)
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//Post Method
con.setRequestMethod("POST");
//To enable inputting values using POST method
//(Basically, after this we can write the dataToSend to the body of POST method)
con.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
//Writing dataToSend to outputstreamwriter
writer.write(encodedStr);
//Sending the data to the server - This much is enough to send data to server
//But to read the response of the server, you will have to implement the procedure below
writer.flush();
//Data Read Procedure - Basically reading the data comming line by line
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = reader.readLine()) != null) { //Read till there is something available
sb.append(line + "\n"); //Reading and saving line by line - not all at once
}
line = sb.toString();//Saving complete data received in string, you can do it differently
//Just check to the values received in Logcat
Log.i("custom_check", "The values received in the store part are as follows:");
Log.i("custom_check", line);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close(); //Closing the
} catch (IOException e) {
e.printStackTrace();
}
}
}
//Same return null, but if you want to return the read string (stored in line)
//then change the parameters of AsyncTask and return that type, by converting
//the string - to say JSON or user in your case
**strong text**return encodedStr;**strong text**
**strong text**HERE IS THE PROBLEM I BELIEVE.
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
System.out.println("Resulted Value: " + result);
if (result.equals("") || result == null) {
Toast.makeText(MainActivity.this, "Server connection failed", Toast.LENGTH_LONG).show();
return;
}
int jsonResult = returnParsedJsonObject(result);
if (jsonResult == 0) {
Toast.makeText(MainActivity.this, "Invalid username or password", Toast.LENGTH_LONG).show();
return;
}
if (jsonResult == 1) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
intent.putExtra("USERNAME", enteredUsername);
intent.putExtra("MESSAGE", "You have been successfully login");
startActivity(intent);
}
}
private int returnParsedJsonObject(String result) {
JSONObject resultObject = null;
int returnedResult = 0;
try {
resultObject = new JSONObject(result);
returnedResult = resultObject.getInt("success");
} catch (JSONException e) {
e.printStackTrace();
}
return returnedResult;
}
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = br.readLine()) != null) {
answer.append(rLine);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return answer;
}
//************getEncodedData*****************//
private String getEncodedData(Map<String,String> data) {
StringBuilder sb = new StringBuilder();
for(String key : data.keySet()) {
String value = null;
try {
value = URLEncoder.encode(data.get(key), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if(sb.length()>0)
sb.append("&");
sb.append(key + "=" + value);
}
return sb.toString();
}`enter code here`
I am trying to extract data from Json string which is obtained by a response using only Java code.
I am posting my Java code here.
OUTPUT:
Entering into while loop
[{"name":"Frank","food":"pizza","quantity":3}]
This is my Java code.
public void receive()
{
System.out.println("Entering into sendNote method");
try {
// make json string,
String json = "{\"name\":\"Frank\",\"food\":\"pizza\",\"quantity\":3}";
// send as http get request
URL url1 = new URL("http://myurl/file.php?usersJSON="+userList);
URLConnection conn1= url1.openConnection();
//I am receiving exactly what I have sent....
BufferedReader rd = new BufferedReader(new InputStreamReader(conn1.getInputStream()));
String line;
while ((line = rd.readLine()) != null)
{
System.out.println("Entering into while loop");
System.out.println(line);// line contains the received json parameters
//I want to enter the recieved parameters into my database
//
//
//
//I need the solution right here....
}
rd.close();
}
catch (Exception e)
{
System.out.println("Error Occured while receiving");
e.printStackTrace();
}
}
Thank you !!!!!
#Ankur:
This is how I tried,
# Lahiru Prasanna, #ankur-singhal
Thanks a lot.!!
I think that you successfully got HttpResponse.here variable called response is HttpResponse.
// Could do something better with response.
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
try {
while ((line = reader.readLine()) != null) {
builder.append(line);
}
content.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
JSONObject jsonObject = new JSONObject(builder.toString());
} catch (JSONException e) {
System.out.println("Error parsing data " + e.toString());
}
}
} catch (Exception e) {
System.out.println("Error: " + e.toString());
System.out.println( "" + e.toString());
System.out.println("" + e.toString());
}
And important thing is never use Strings for json operations.
you can retrieve your data from jsonObject like this
String name = jsonObject.getString("name");
There are few ways to achive the same.
1.) Create a response pojo
MyResponse ob = new ObjectMapper().readValue(jsonString, MyResponse.class);
// then call getters on above.
2.) Get key/values
JSONObject json = (JSONObject)new JSONParser().parse(""name":"Frank","food":"pizza","quantity":3}");
System.out.println("name=" + json.get("name"));
System.out.println("width=" + json.get("food"));
3.) Converting Json to HashMap
public static void main(String[] args) {
try {
jsonToMap("{\"name\":\"Frank\",\"food\":\"pizza\",\"quantity\":3}");
} catch (JSONException e) {
e.printStackTrace();
}
}
public static void jsonToMap(String t) throws JSONException {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jObject = new JSONObject(t);
Iterator<?> keys = jObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
String value = jObject.getString(key);
map.put(key, value);
}
System.out.println("json : " + jObject);
System.out.println("map : " + map);
}
output
json : {"name":"Frank","food":"pizza","quantity":3}
map : {food=pizza, name=Frank, quantity=3}
Guys can you help me a little bit, Im getting this error:
"JSONException: Value <!DOCTYPE of type java.lang cannot be converted to JSONObject"
When I'm parsing the data here is my code:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
}catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
Here is the code where I'm instantiating the Parser:
private void fillSpinnerCabTypes() {
List<String> cabTypesSpinner = new ArrayList<String>();
JSONParser jsonParser = new JSONParser();
JSONObject cabTypesObject = jsonParser.getJSONFromUrl(urlTypeCabs);
try{
TypesArray = cabTypesObject.getJSONArray(TAG_TYPES);
for(int i = 0; i < TypesArray.length(); i++){
JSONObject c = TypesArray.getJSONObject(i);
String name = c.getString(TAG_NAME);
cabTypesSpinner.add(name);
}
}catch(Exception e ){
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, cabTypesSpinner);
final Spinner spnCabTypes = (Spinner)findViewById(R.id.spnTypeOfCab);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spnCabTypes.setAdapter(adapter);
}
I'm really stuck with this. I'm populating the spinner from a database in a backend on Django in the server.
This is my JSON data
{"Types": [{"name": "Normal"}, {"name": "Discapacitados"}, {"name": "Buseta"}]}
This issue comes from the server.
The URL you're requesting, send you back data but not in the JSON format.
The Exception you get is telling you that the String the server send you back starts with:
<!DOCTYPE
This can be:
A simple webpage (instead of raw JSON). It correspond to the first XML tag of a web page (source)
An error page generated by the server, and printed in HTML
To debug this further, simply print the content of your json variable in the logcat:
Log.d("Debug", json.toString());
jObj = new JSONObject(json);
This problem came in my code also.and solution was different.It occured due to spelling mistake of webservice.
Solution 1:
for example real the url is
http://example.com/directory/file.php
and i had used
http://example.com/directory/file1.php
Solution 2:
use loopj library .it exactly gives you the explained error.
AsyncHttpClient client = new AsyncHttpClient();
client.post(str , localRequestParams, new AsyncHttpResponseHandler() {
#Override
public void onFinish() {
super.onFinish();
Log.i("onFinish","onFinish");
}
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Log.i("onSuccess","onSuccess");
}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.i("onFailure","onFailure");
}
});
I got code that gets JSONArrays, but however when I try to get JSONObject that contains only one JSONArray it gives me empty JSONArray.
For example if I need to get data from this JSONObject:
{"events":[{"start":1357714800,"end":1357736400,"name":"Example1","description":""}]}
I get {"events":[]} as JSONObject, [] meaning that it doesn't contain any JSONArrays. Also length of JSONObject is in this case 0. But it doesn't throw any kind of Exceptions.
but if JSONObject contains multiple JSONArrays like this:
{"events":[{"start":1357714800,"end":1357736400,"name":"Example1","description":""},{"start":1357714600,"end":1357736500,"name":"Example2","description":""},{"start":1357514800,"end":1357536400,"name":"Example3","description":""}]}
then my code works perfect.
Here is the code I use to parse JSON:
private void getObjects(String url) throws JSONException, Exception {
JSONObject jsonObject = new JSONObject(new NetTask().execute(url).get());
JSONArray job1 = jsonObject.getJSONArray("events");
System.out.println(jsonObject.toString());
System.out.println("JOB1 LENGTH: "+job1.length());
for (int i = 0; i < job1.length(); i++) {
JSONObject jsonEvent = job1.getJSONObject(i);
int start = jsonEvent.getInt("start");
int end = jsonEvent.getInt("end");
String name = jsonEvent.getString("name");
String description = jsonEvent.getString("description");
}
}
public class NetTask extends AsyncTask<String, Integer, String>
{
#Override
protected String doInBackground(String... params)
{
String jsonText = "";
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1) {
buffer.append(chars, 0, read);
}
jsonText = buffer.toString();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return jsonText;
}
}
is there something wrong that I am missing or is this normal behaviour?
I tried your given code (though I just made the AsyncTask just return the single-array string, and had to replace the opptunti.getString() stuff with jsonEvent.getString()). It worked fine, aside from the fact that you're probably blocking the UI thread to wait for the server response.
My guess is the problem is that you are hitting the wrong URL, that the parameters are wrong or something like that.