I'm trying to get 2 values from Wikipedia with the next API link:
https://en.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&prop=pageimages&format=json&pithumbsize=500&utf8
because its generated by random, sometimes it doesn't return one of the values I need, but I will solve this later, currently I'm having a problem with accessing the two values I need in the Json, "title" and "source"
The returned Json is like this:
{"batchcomplete":"","continue":{"grncontinue":"0.360395277951|0.360395626487|10429617|0","continue":"grncontinue||"},"query":{"pages":{"38690716":{"pageid":38690716,"ns":0,"title":"Alaine Chartrand","thumbnail":{"source":"https://upload.wikimedia.org/wikipedia/commons/d/d4/Alaine_Chartrand.jpg","width":267,"height":400},"pageimage":"Alaine_Chartrand.jpg"}}}}
this is the code, can anybody figure out why does it go to JSONException?
String API = "https://en.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&prop=pageimages&format=json&pithumbsize=500&utf8";
//open connection with wikipedia.
HttpURLConnection httpcon = (HttpURLConnection) new URL(API).openConnection();
//read all the input from wikipedia.
BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
String responseSB = in.lines().collect(Collectors.joining());
in.close();
JSONObject incomingJSON = new JSONObject(responseSB);
//crashes here
String mTitle = incomingJSON.getString("title");
String mUrl = incomingJSON.getString("source");
If you notice the JSON it is randomly generated but with specific format
Case 1
{
"batchcomplete": "",
"continue": {
"grncontinue": "0.720220803439|0.720221273467|12887566|0",
"continue": "grncontinue||"
},
"query": {
"pages": {
"4897672": {
"pageid": 4897672,
"ns": 0,
"title": "New Hope, Sunnyvale, Texas"
}
}
}
}
query and pages were always existed, and in pages the key is always randomly generate, so it is Map<String, JSONObject> map of String key and JSONObject as value, Then you need to get the title value from map values
String API = "https://en.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&prop=pageimages&format=json&pithumbsize=500&utf8";
//open connection with wikipedia.
HttpURLConnection httpcon = (HttpURLConnection) new URL(API).openConnection();
//read all the input from wikipedia.
BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
String responseSB = in.lines().collect(Collectors.joining());
in.close();
JSONObject incomingJSON = new JSONObject(responseSB);
Map<String,JSONObject> map = (Map<String, JSONObject>) incomingJSON.getJSONObject("query").getJSONObject("pages");
map.forEach((k,v)->System.out.println(" The key is : "+k+" the title is : "+v.getString("title")));
Case 2 With source
{
"batchcomplete": "",
"continue": {
"grncontinue": "0.165621850014|0.165622038679|37982311|0",
"continue": "grncontinue||"
},
"query": {
"pages": {
"57529788": {
"pageid": 57529788,
"ns": 0,
"title": "Model Store",
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/23/Australia_New_South_Wales_relief_location_map.png/500px-Australia_New_South_Wales_relief_location_map.png",
"width": 500,
"height": 443
},
"pageimage": "Australia_New_South_Wales_relief_location_map.png"
}
}
}
}
So source may not present in every response, handle with try catch
String API = "https://en.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&prop=pageimages&format=json&pithumbsize=500&utf8";
//open connection with wikipedia.
HttpURLConnection httpcon = (HttpURLConnection) new URL(API).openConnection();
//read all the input from wikipedia.
BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
String responseSB = in.lines().collect(Collectors.joining());
in.close();
JSONObject incomingJSON = new JSONObject(responseSB);
Map<String,JSONObject> map = (Map<String, JSONObject>) incomingJSON.getJSONObject("query").getJSONObject("pages");
map.forEach((k,v)->{
System.out.println(" The key is : "+k+" the title is : "+v.getString("title"));
//use try catch to get source because you will not get the same response every time
String source = v.getJSONObject("thumbnail").getString("source");
});
}
You can't get title and source directly from JSON response because it has to contain multiple inner objects. Below is the code snap for reading title and source.
// new code
JSONObject incomingJSON = new JSONObject(responseSB);
JSONObject innerObject = incomingJSON.getJsonObject("query").getJsonObject("pages").getJsonObject("38690716");
String mTitle= innerObject.getString("title");
String mUrl= innerObject.getJsonObject("thumbnail").getString("source");
//crashes here
String mTitle = incomingJSON.getString("title");
String mUrl = incomingJSON.getString("source");
try this...
JSONObject incomingJSON = new JSONObject(responseSB);
JSONObject TitleObjects = incomingJSON.getJSONObject("query");
JSONObject j_Objects_01 = TitleObjects.getJSONObject("pages");
JSONObject j_Objects_02 = j_Objects_01.getJSONObject("38690716");
String mTitle = j_Objects_02.getString("title");
JSONObject j_Objects_03 = j_Objects_02.getJSONObject("thumbnail");
String mUrl = j_Objects_03.getString("source");
You should be aware that the page id will change and the thumbnail is optional.
// new code
JSONObject incomingJSON = new JSONObject(responseSB);
JSONObject pages = incomingJSON.getJSONObject("query").getJSONObject("pages");
Iterator<String> it = pages.keys();
while(it.hasNext()) {
JSONObject page = pages.getJSONObject(it.next());
String mTitle= page.getString("title");
if(page.keySet().contains("thumbnail")) {
String mUrl= page.getJSONObject("thumbnail").getString("source");
}
}
So because the ID kept changing, I've decided to go from another approach.
I've used the following code:
Pattern p = Pattern.compile("\"source\":\"(.*?)\",\"width");
Matcher m = p.matcher(responseSB);
if (m.find()) {
url = m.group(1);
}
p = Pattern.compile("\"title\":(.*?)\",\"thumbnail");
m = p.matcher(responseSB);
if (m.find()) {
description = m.group(1);
}
Related
public static String getsheetdata() throws IOException {
String name = null;
String email = null;
String phone = null;
String fin = null;
String address = null;
String car_registraion = null;
String question = null;
String pin = null;
String car_registraion_date = null;
String url = "https://sheets.googleapis.com/v4/spreadsheets/1BH-e3-XSZ9LjsQqELjZLpZbnB4DmIhrPy2VDAZsP9KM/values/lead!A2:J2?key=AIzaSyDJRy73ru1BSLFCb9nknUF8SlZd4LxwJAc";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.0 Safari/532.5");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
//Read JSON response and print
JSONObject myResponse = new JSONObject(response.toString());
return inputLine;
}
I am getting following response
Sending 'GET' request to URL : https://sheets.googleapis.com/v4/spreadsheets/1BH-e3-XSZ9LjsQqELjZLpZbnB4DmIhrPy2VDAZsP9KM/values/lead!A2:J2?key=AIzaSyDJRy73ru1BSLFCb9nknUF8SlZd4LxwJAc
Response Code : 200
{ "range": "lead!A2:J2", "majorDimension": "ROWS", "values": [ [ "Lead Data Set 1 - Normal FOC Lead", "Bhupendra", "bhupendra+283273#abc.com", "2389432432", "90909892098988771", "Street123, Berlin", "1289243424321", "no comments", "10115", "12 / 12 / 2017" ] ]}
I need to fill the response data in following variables .
String name = null;
String email = null;
String phone = null;
String fin = null;
String address = null;
String car_registraion = null;
String question = null;
String pin = null;
String car_registraion_date = null;
Would appreciate if anyone can help me on it.
You can use any JSON to Java unmarshalling library to convert the JSON to Java object. Check options and examples
Create JSONObject of the response string you are getting and then extract values fields from JSONObject as JSONArray and then traverse through that JSONArray to get list of your object.
To add to Hiren's answer, you can try (using org.json):
JSONObject myResponse = new JSONObject(response.toString());
JSONArray jsonArr = (JSONArray) myResponse.get("values");
JSONArray requiredValues = jsonArr.getJSONArray(0);
String[] values = new String[requiredValues.length()];
for (int i = 0; i < requiredValues.length(); i++) {
values[i] = requiredValues.getString(i);
}
Now the "values" part of response will be stored in String[] values
for (int j = 0; j < values.length; j++) {
System.out.println(values[j]);
}
This will print
Lead Data Set 1 - Normal FOC Lead
Bhupendra
bhupendra+283273#abc.com
2389432432
90909892098988771
Street123, Berlin
1289243424321
no comments
10115
12 / 12 / 2017
You can assign it accordingly. Hope it helps.
I'm trying to read the following json output from a URL
{
"error": false,
"status": 200,
"message": "License Key activated successfully.",
"data": {
"expire": 1582657054,
"activation_id": 1519628117,
"expire_date": "2020-02-25 18:57",
"timezone": "UTC",
"the_key": "Cqu62al903ICv40am9nM68Y7o9-32",
"url": "http://domain/my-account/view-license-key/?key=test-32",
"has_expired": false,
"status": "active",
"allow_offline": true,
"offline_interval": "days",
"offline_value": 1,
"downloadable": {
"name": "v1.1.5",
"url": "https://domain/product-1.1.5.zip"
},
"ctoken": "dsfejk8989"
}
}
I'm trying to get both values "status: 200," and "activation_id".
I've tried looking online and parsing. Nothing seems to work. I'm sort of new to the whole json reading.
try {
JSONParser jsonParser = new JSONParser();
String jsonS = "";
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
jsonS += inputLine;
}
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(jsonS, JsonObject.class);
int id = jsonObject.get("status").getAsInt();
cintout(id);
cout(link);
cout(inputLine);
try {
if (id == 200)
return ValidationType.VALID;
else
return ValidationType.WRONG_RESPONSE;
} catch (IllegalArgumentException exc) {
if (id == 200)
return ValidationType.VALID;
else
return ValidationType.WRONG_RESPONSE;
}
} catch (IOException e) {
e.printStackTrace();
return ValidationType.VALID;
}
I've managed to retrieve the status value but not the activation id.
You used two library for JSON parsing which is not required in this context. Assuming you want to use Gson. Remove JSONParser jsonParser = new JSONParser();
Now, in your JSON data activation_id can be reached at Root -> data -> activation_id. Root represents whole JSON object which stored to jsonObject. data key itself represent an object. Therefore we can reach to activation_id by getting data key value as an object and then get activation_id as int/string.
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(jsonS, JsonObject.class);
int id = jsonObject.get("status").getAsInt();
int activationId = jsonObject.get("data").getAsJsonObject().get("activation_id").getAsInt();
For more information about json objects: https://www.shapediver.com/blog/json-objects-explained/
You need to get the data object with Gson before you can access its fields:
int activation_id = jsonObject.get("data").getAsJsonObject().get("activation_id").getAsInt();
I am currently developing an app and need to parse JSON objects from inside an unnamed array.
I can only manage to parse JSON arrays with a name such as this one: http://jsonparsing.parseapp.com/jsonData/moviesDemoItem.txt.
The code that I used for the one above is
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String asd = buffer.toString();
JSONObject parentObject = new JSONObject(asd);
JSONArray parentArray = parentObject.getJSONArray("movies");
JSONObject fObject = parentArray.getJSONObject(0);
String movie = fObject.getString("movie");
int year = fObject.getInt("year");
return movie + year;
The code includes "movies" which is the array name .
What should I change to parse only the objects from within a JSON array such as https://restcountries.eu/rest/v1/all?
Your countries list is simply an array. Doesn't need a name.
Simply replace
JSONObject parentObject = new JSONObject(asd);
with
JSONArray parentObject = new JSONArray(asd);
See this post for how to iterate over that array to parse the remainder of the objects.
How to parse JSON in Android
Starting something like
for (int i=0; i < parentObject.length(); i++) {
Alternatively, Volley's JsonArrayRequest would be useful, or learning about Retrofit+Gson would be even better if you don't feel like manually parsing the JSON data yourself.
as mentioned in my older topic 3 days ago - Last Topic
i got a json response and changed it to a string. The Json Response represents an User-Object. Within the User-Object i wanted to search for a specific project and delete it. After that, i want to post it again via HttpPost.
private static String getContent(HttpResponse response) {
HttpEntity entity = response.getEntity();
if (entity == null) return null;
BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(entity.getContent()));
String line = reader.readLine();
reader.close();
return line;
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
String StringResponse = getContent(JsonResponse);
JSONObject jsonObject = new JSONObject(StringResponse);
JSONArray ProjectsArray= jsonObject.getJSONArray("projects");
Searching for a specific project by saving the attributes in a JsonArray.
ArrayList<Integer> indexesToRemove = new ArrayList<Integer>();
for (int i = 0; i < projectsArray.length; i++) {
JSONObject current = projectsArray.get(i);
if (current.get("projectKey") == "**ProjectName**") {
indexesToRemove.add(i);
}
}
Deleting the project...
for (int i = indexesToRemove.size()-1; i>=0; i--)
{
projectsArray.remove(indexesToRemove.get(i));
}
That works perfect and my searched project is deleted. But the problem is, that i want to post the modified UserObject/String again via HttpPost. And my deleted project is just in my JsonArray "projectsArray" and not in my string from the beginning. I can't post "projectsArray"....
HttpPost UserChange = new HttpPost (TestUserURL+user); //TODO:
UserChange.setHeader("Accept", "application/json");
UserChange.setHeader("Content-type", "application/json");
params = new StringEntity("ModifiedJsonString", HTTP.UTF_8); // How do i get the complete Json string?
UserChange.setEntity(params);
HttpResponse UserChangeResponse = httpclient.execute(UserChange);
HttpEntity entity2 = UserChangeResponse.getEntity();
if (entity2 != null) {
entity2.consumeContent();
}
I need the "ModifiedJsonString", which includes the complete json file from the beginning.
params = new StringEntity(ModifiedJsonString, HTTP.UTF_8);
Best Regards
The following code removes one of the selected project.
String jsonString = "{ \"account\": \"Kpatrick\", \"firstname\": \"Patrick\", \"instances\": [ { \"id\": \"packerer-pool\", \"key\": \"packerer-pool123\", \"userAccount\": \"kpatrick\", \"firstname\": \"Patrick\", \"lastname\": \"Schmidt\" } ], \"projects\": [ { \"id\": \"packerer-projectPool\", \"projectKey\": \"projectPool-Pool\", \"cqprojectName\": \"xxxxx\" }, { \"id\": \"packerer-secondproject\", \"projectKey\": \"projectPool-Pool2\", \"cqprojectName\": \"xxxx\" }, { \"id\": \"packerer-thirdproject\", \"projectKey\": \"projectPool-Pool3\", \"cqprojectName\": \"xxxx\" } ], \"clients\": [], \"dbid\": 76864576, \"version\": 1, \"id\": \"dbpack21\"}";
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(jsonString);
ArrayList<String> listOfNodes = new ArrayList<String>();
JSONArray projectArray = (JSONArray) jsonObject.get("projects");
int len = projectArray.size();
if (projectArray != null) {
for (int i = 0; i < len; i++) {
String projectId = ((JSONObject) projectArray.get(i)).get("projectKey").toString();
if (!projectId.equals("projectPool-Pool2")) {
listOfNodes.add(projectArray.get(i).toString());
}
}
}
// Remove the element from arraylist
// Recreate JSON Array
JSONArray jsArray = new JSONArray();
jsArray.addAll(listOfNodes);
jsonObject.remove(projectArray);
jsonObject.put("projects", listOfNodes);
System.out.println(jsonObject.toString());
This for example , prints the following JSON string removing one of the projects.
Once you have this , you can then use this to create a StringEntity and then use it in HTTPPost calls. Hope it helps
I am trying to send a request to the Grooveshark API using POST Payload and their requested methods, and I have found a problem. Allow me to show you my code first.
public void getResponse() throws Exception
{
if(service.equals("Grooveshark")) link += getHmacMD5(privateGroovesharkKey, jsonInfo.toString());
if(requestedMethod.equals("GET")) infoURL = new URL(link+arguments);
else infoURL = new URL(link);
HttpURLConnection connection = (HttpURLConnection) infoURL.openConnection();
connection.setRequestMethod(requestedMethod);
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
if(service.equals("Grooveshark"))
{
connection.setRequestProperty("Content-Type","application/json");
OutputStream output = connection.getOutputStream();
output.write(jsonInfo.toString().getBytes());
}
else if(requestedMethod.equals("POST") || requestedMethod.equals("PUT"))
{
OutputStream output = connection.getOutputStream();
output.write(arguments.getBytes());
}
connection.connect();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null)
sb.append(line).append('\n');
setJsonResult(sb.toString());
System.out.println(jsonResult);
jsonFinal = new JSONObject(jsonResult);
connection.disconnect();
}
I have got that code up here in my project, and I can successfully send requested to any API Webservice that uses JSON in their responses. Now there's only a problem: In Android, it does not give me the WHOLE answer. I've tried running the code on a separate Java (no Android) project, and I get the following output. Although, if I run it on Android, the Log shows me the following:
{"header":{"hostname":"RHL073"},"result":{"songs":[{"SongID":5443351,"SongName":"??????\u00b7???? (FINAL FANTASY XII????)","ArtistID":713,"ArtistName":"Final Fantasy","AlbumID":898007,"AlbumName":"Final Fantasy XII Original Soundtrack","CoverArtFilename":"","Popularity":1214500005,"IsLowBitrateAvailable":tr
And it stops on that tr. Has it anything to do with the parsing of the file that I actually apply afterwards? I don't think it is, but just in case, here it is [This is how I call the search, JSONHandler being the object that contains the code provided above]:
public void performSearch() throws Exception
{
JSONObject search = new JSONObject();
search.put("method", method);
JSONObject header = new JSONObject();
header.put("wsKey", key);
JSONObject parameters = new JSONObject();
parameters.put("query", getSearchQuery());
parameters.put("country", "Portugal");
parameters.put("limit", limit);
parameters.put("offset", "");
search.put("header", header);
search.put("parameters", parameters);
JSONHandler jsonHandler = new JSONHandler(link, search, "Grooveshark", "POST", "");
JSONObject finalResult = jsonHandler.getJsonFinal();
JSONArray songs = finalResult.getJSONObject("result").getJSONArray("songs");
ArrayList<Result> allResults = new ArrayList<Result>();
for(int i = 0; i < songs.length(); i++)
{
JSONObject inner = (JSONObject) songs.get(i);
String name = inner.getString("SongName");
int ID = inner.getInt("SongID");
String artist = inner.getString("ArtistName");
Result res = new Result(name, artist, ID);
res.setAlbumName(inner.getString("AlbumName"));
boolean low = inner.getBoolean("IsLowBitrateAvailable");
int bit = 0;
if(low) bit = 1;
else bit = 0;
res.setIsLowBitRateAvailable(bit);
}
setResults(allResults);
}
As you can clearly see, I am using the json.org library. I really don't understand what's the problem here. Has anyone got any idea as to why?