how to convert or read php object response to json? - android

i have get response like this
Array (
[status] => 9
[message] => Your devices not verify!
)
i use this code but not read response Html.fromHtml(jsonString).toString()
so,how can read this in android or json object format?
i also use
GsonBuilder gsonb = new GsonBuilder();
Gson gson = gsonb.create();
Post pst;
pst = gson.fromJson(jsonString, Post.class);
Post.class
class Post
{
String message;
String status;
}

Related

How to get Response values (Login Responses) from retrofit android

i am beginner of Android Learner,i used retrofit in my project,i get responses from login Request
{"android":{"msg":"User login successfull","data":{"id":"14","name":"meena","url":"","email":"meena04#gmail.com","password":"202cb962ac59075b964b07152d234b70","phone":"","country":"China","photo":"","status":"pending","role":"3","device_id":"","oauth_uid":"","resume":"","oauth_provider":null,"created":"1503316508"}}}
try this to you can parse your json like this
try
{
JSONObject json = new JSONObject(jsonResponse);
String msg= data.getString("msg")// get your msg
JSONObject data = json.getJSONObject("data");// now get your secong json object like this
String id= data.getString("id");// get the id from data json object like this
String name= data.getString("name");// get the name from data json object like this
String url= data.getString("url");// get the url from data json object like this
String email= data.getString("email");// get the email from data json object like this
String password= data.getString("password");// get the password from data json object like this
String phone= data.getString("phone") ; // get the phone from data json object like this
String country= data.getString("country");// get the country from data json object like this
String photo= data.getString("photo");// get the photo from data json object like this
String status= data.getString("status");// get the status from data json object like this
String role= data.getString("role");// get the role from data json object like this
String device_id= data.getString("device_id");// get the device_id from data json object like this
String oauth_uid= data.getString("oauth_uid");// get the oauth_id from data json object like this
String resume= data.getString("resume");// get the resune from data json object like this
String oauth_provider= data.getString("oauth_provider");// get the oauth_provider from data json object like this
String created= data.getString("created");// get the created from data json object like this
Log.e("RESULT :-> ",msg+" "+id+""+name+" "+url+""+email+""+password+" "+phone+" "+phone
+country+" "+photo+""+status+" "+role+" "+device_id+" "+resume+" "+oauth_provider+" "+created+ " ");
} catch (JSONException e) {
e.printStackTrace();
}
You can use Gson for this :
first, create a pojo using http://pojo.sodhanalibrary.com/
let's name it Response class
now use gson as follows:
Gson gson = new Gson();
Response Response = gson.
fromJson(response.toString(), Response.class);
best method :
1. use retrofit Gson library compile
'com.squareup.retrofit2:converter-gson:2.2.0'
add GsonFactory to retrofit
new Retrofit.Builder()
.baseUrl(Common.Urls.BaseURL)
.addConverterFactory(GsonConverterFactory.create());
create pojo as above
observe retrofit response as a class object:
#POST(Common.Urls.LOGIN_API)
Observable signIn(#Body HashMap request);

Unable to get response when code 401,422 Unauthorized in retrofit in android

I have onResponse and onFailure block when requesting for api. In response block checking response.isSuccessful() when success mean code 200 then i can easily parse the response but when 400,401,422 then i can't able to parse response.when i am trying to get body or errorBody then return `null.
if (response.isSuccessful())
{
Gson gson = new Gson();
String json = gson.toJson(response.body());
Log.e("isSuccessful",json);
}
else {
Log.e("errorBody",response.errorBody().toString());
Gson gson = new Gson();
String json = gson.toJson(response.body());
Log.e("errorBody",json);
}

Working with GSON have error on android 6

I wrote a code, get String base on Json format of a web service and then should to convert string to object. I wrote below code:
//builder = new GsonBuilder();
//builder.excludeFieldsWithModifiers(Modifier.FINAL,Modifier.TRANSIENT,Modifier.STATIC);
//builder.excludeFieldsWithoutExposeAnnotation();
try {
Gson gson = new Gson();
//Gson gson = builder.create();
Type t = new TypeToken<ActionResult<Task>>() {}.getType();
String strJson = new String(jsonData).trim();
ActionResult<Task> result = gson.fromJson(strJson,t);
}catch (Exception e) {
Logger.e(e.getMessage());
}
When i used Gson gson = new Gson(); , i get an error and go to catch(my error is in below) but when i used Gson gson = builder.create();, the program have not any error but can't set data on result object.
Error:
java.lang.SecurityException: Can't make field constructor accessible
The program have error in android 6 but have not any error on below 6.

GSON sends my object as a string not a object

I am using Volley with GSON and I need to send a object as a parameter to my call.
This is how I do the object:
JSONObject params = new JSONObject();
Gson gson = new Gson();
String json = gson.toJson(route);
params.put("route", json);
And then I call my Volley JsonObjectRequest function.
The problem is that the params look like this:
{"route":"{\"bounds\":{\"northeast\":{\"lat\":52.3777194,\"lng\":4.924666999999999},\"southwest\":{\"lat\":52.36881109999999,\"lng\":4.9011479}},\"copyrights\":\"Map data ©2014 Google\", etc...}"
As you can see, instead of sending it as a object, its sending it as a String , and that's why I get the " before the {} (before the object begins). The params should look like:
{"route":{\"bounds\":{\"northeast\":{\"lat\":52.3777194,\"lng\":4.924666999999999},\"southwest\":{\"lat\":52.36881109999999,\"lng\":4.9011479}},\"copyrights\":\"Map data ©2014 Google\", etc...}
So no " before { like this:
{"route":{myObject}
What an I doing wrong here?
You don't want to mix JSONObject and GSON.
That's 2 different libraries.
Use gson.toJsonTree to obtain an element, then use JsonObject instead of JSONObject:
JsonObject params = new JsonObject();
Gson gson = new Gson();
params.add("route", gson.toJsonTree(route));

How change rules for Gson parser?

I want to change rules for Gson parser (json parser wrote by google for Android). For example I have the object of class:
enum Type
{
PRIMARY,
SECONDARY
}
class A
{
public int i = 4;
public Type type = Type.PRIMARY;
}
A a = new A();
Now if I will convert that object via Gson converter:
Gson gson = new Gson();
JsonElement json = gson.toJson(a);
I will get that json element: {"i":4,"type":"PRIMARY"}. Instead of this I want to get: {"i":4,"type":0}, i.e. ordinal of type instead of a name of it.
How can I do that?
Tnx.
Ok, I found the answer:
For doing that need to create Gson object from GsonBuilder. But before that register a new type hierarchy adapter for gson builder for Enum.class type with your own json serializer that will do the work.
// new Json serializer for Enum<?> class
class ObjectTypeSerializer implements JsonSerializer<Enum<?>>
{
private static final JsonParser mParser = new JsonParser();
public JsonElement serialize(Enum<?> object_,
Type type_,
JsonSerializationContext context_)
{
// that will convert enum object to its ordinal value and convert it to json element
return mParser.parse(((Integer)object_.ordinal()).toString());
}
}
// creation of gson builder
GsonBuilder gsonBuilder = new GsonBuilder();
// registration of type hierarchy adapter
gsonBuilder.registerTypeHierarchyAdapter(Enum.class, new ObjectTypeSerializer());
// creation gson from builder
Gson gson = gsonBuilder.create();
Now gson will do the work!

Categories

Resources