I am developing the android app and i want to get the token from header using volley which is in server json response and store it in sq-lite for maintaining the session of user,i search everywhere bt i am not getting how to retrieve the token from json,please help me about this issue
this is my json response
{ "token": "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI5ODkzMjY3OTQ0IiwiYXVkIjoid2ViIiwiZXhwIjoxNTA1ODEwNTQ5LCJpYXQiOjE1MDUyMDU3NDl9.cf9QGwp7zLVSTuiunClkauTFEXe6jtcNCxzqIntEtmWTDfLqAD3nKiXIf-fg0vEV_74SOcPVYxDcTUwCZqntVA" }
try this
JSONObject jsonObject = new JSONObject("your json string");
String token= jsonObject.optString("token");
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(response);
String access_token= jsonObject.optString("access_token");
Log.v("####","hellow token " + access_token);
} catch (JSONException e) {
e.printStackTrace();
}
Try this
Related
I have a bug in the code, but I can not find it. I have to read the message and the code from the JSON request below.
try {
Log.d("qwertz", json);
progressDialog.dismiss();
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("server_response");
JSONObject JO = jsonArray.getJSONObject(0);
String code = JO.getString("code");
String message = JO.getString("message");
if (code.equals("win"))
{
showDialog("Du hast etwas gewonnen", message,code);
}
else if (code.equals("false"))
{
showDialog("Du hast leider nichts gewonnen", message,code);
}
} catch (JSONException e) {
e.printStackTrace();
}
That is a JSON example
{
"server_response": {
"code": "win",
"message": "Du hast einen PzKpfw S35 739(f) gewonnen"
}
}
I advice you not to process json directly by yourself, instead you can use many libraries that will do the job for you. To mention you can use jackson or gson.
If you still want to correct your code you can try this:
JSONObject response = jsonObject.getJsonObject("server_response");
String code = response.getString("code");
How can I create a JSON with this format in Android?
I want to send this JSON in my SOAP Web Service Parameter
"{'\obj\':{\"MeterSrNo\":\"5\"},'\SPName\':'XXMFU_GETMobilityDetail'}"
SPName is my second parameter.
Use JSONObject
try {
JSONObject jsonObject = new JSONObject();
JSONObject obj = new JSONObject();
obj.put("MeterSrNo", "5");
jsonObject.put("obj", obj);
jsonObject.put("SPName", "XXMFU_GETMobilityDetail");
} catch (JSONException e) {
}
I'm trying to get a particular value from a field within a JSON file. This is my first time working with a JSON file so I'm not sure if I'm doing it correctly.
try {
URL url = new URL(API_URL + city +".json");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
bufferedReader.close();
return stringBuilder.toString();
}
This block would get the JSON from a weather website and then return the JSON as a string. This string would then be used below. Unfortunately whenever I run this app in Android Studio and go through the logs the logs constantly say that there was no value in weather.
protected void onPostExecute(String response) {
if(response == null) {
response = "THERE WAS AN ERROR";
}
progressBar.setVisibility(View.GONE);
Log.i("INFO", response);
responseView.setText(response);
// TODO: check this.exception
// TODO: do something with the feed
try {
JSONObject object = (JSONObject) new JSONTokener(response).nextValue();
String weather = object.getString("weather");
Toast.makeText(MyActivity.this, "There is rain", Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
What I'm trying to do is grab information from the weather section
Here is the JSON link I'm trying to parse from. This text box thought there were too many links in the JSON response for me to post it here. I'm trying to figure out what is going on and any help would be greatly appreciated.
You can print out response to see if it is exactly your JSON or not.
Assume you get the JSON String correctly.
JSONObject object = new JSONObject(response);
String weather = object.getString("weather");
However, if the JSON your obtained is exactly your response, the above code do not work because there is not weather String. The root only contain response and current_observation.
If what you what to get is weather under current_observation, you should use the code below.
JSONObject object = new JSONObject(response);
JSONObject current_obs = object.getJSONObject("current_observation");
String weather = current_obs.getString("weather"); // Clear
JSONObject object = (JSONObject) new JsonObject(responseString);
String weather = object.getString("weather");
First you need to get your response in string.
Now,start json parsing as per response.
OR
You can use Gson class as well for json parsing.
Get json value from JSONObject:
{"weather":"sunny"}
code snippet to parse the JSONObject.
JSONObject object = new JsonObject(response);
String weather= object.getString("weather");
Get json value from JSONArray:
[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]
code snippet to parse the JsonArray.
JSONArray jsonarray = new JSONArray(response);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String name = jsonobject.getString("name");
String url = jsonobject.getString("url");
}
Hope it'll help you.
I have a sample Weather app done using open weather map api. It has all you asked for.
Code here:
http://opensourceandroid.in/weather_forecast.html
{
"status": "ERROR",
"msg_content": "Your old password was entered incorrectly. Please enter it again.",
"code": "400",
"msg_title": "Sorry. Error in processing your request"
}
if (str.startsWith("Bad Request"))
{
textview.setText(" ");
}
How to print inside the textview to display the msg_content using json
You need to parse json using JSON object.
JSONObject obj = new JSONObject(str);
Then find string from JSON object whatever you want.
if (obj.has("msg_content")) {
String value = obj.getString("msg_content");
textview.settext(value);
}
You will need to create a JSONObject and pass it the string as a parameter.
JSONObject obj = new JSONObject(str);
Then to find a key in the JSONObject just call, always check if the JSONObject has that key before trying to retrieve it.
if (obj.has("status"){
String status = obj.getString("status");
}
if (obj.has("msg_content"){
String content = obj.getString("msg_content");
textview.setText(content);
}
JsonReader is implemented in API 11. If you want to use it in GingerBread or below try this
Use following Code for extracting Information from JSON Objects:
try {
Iterator keys = jsonObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
if(key.equals("msg_content"))
textView.setText(jsonObject.getString(key));
}
} catch (JSONException e) {
e.printStackTrace();
}
Also, if u have JSON as String, u can populate an object using following code:
try {
jsonObject = new JSONObject(theJsonString);
} catch (JSONException e1) {
e1.printStackTrace();
}
I'm quite new to JSON but I would create a JsonReader and parse the JSON as per here
I am using following piece of code for parsing JSON in my android application.
String result = postHttpResponse(uri, json);
try {
JSONObject jsonResult = new JSONObject(result);
boolean authenticated = jsonResult.getBoolean("Authenticated");
if(authenticated){
user = new User();
JSONObject jsonUser = (JSONObject) jsonResult.get("User");
user.setName(jsonUser.getString("Name"));
user.setUserId(jsonUser.getString("UserId"));
}
} catch (JSONException e) {
Log.e(TAG, e.getMessage());
}
jsonUser.getString("UserId") returns the value "corp\\ns0017" instead of "corp\ns0017". Actualy it is escaping the character "\" in the string with one more "\". but at the time of parsing i want to remove the extra "\". How can i achieve it?