Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have Asp.net Api to get Json Response Like This "{\"UserID\":5,\"UserName\":\"asd\",\"Password\":\"asd\",\"Email\":\"ss@asd\",\"PhoneNumber\":\"1213\",\"Logtit\":0.0,\"Latitle\":0.0,\"OfGroup\":\"a \"}"

Haw can i handle this Json response in Android Studio Using Volley Lib This My Code


What I have tried:

Java
<pre>public class MainActivity extends AppCompatActivity {


RequestQueue  requestqueue;
Button start;
TextView textView;
EditText ee;
public String givenValue = ee.getText().toString();
public String URL = "http://localhost:61511:8010/api/Feedback/5" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    requestqueue=Volley.newRequestQueue(this);
   final Button start =(Button)findViewById(R.id.btnget);
   final TextView textView =(TextView)findViewById(R.id.mTextView);
   final EditText ee =(EditText)findViewById(R.id.idtxt) ;
    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("User");
                        for(int i= 0 ;i < jsonArray.length();i++){
                            JSONObject User = jsonArray.getJSONObject(i);

                        }
                    }catch (JSONException e ) {
                        e.printStackTrace();
                    }
                }

            },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e("Volley", "ERROR");
                        }
                    }
            );
            requestqueue.add(jsonObjectRequest);}
    });
}
Posted
Updated 15-Feb-19 2:00am
Comments
David Crow 13-Feb-19 12:39pm    
What's the problem?
Member 14032301 14-Feb-19 4:20am    
Haw can i handle this Json response in Android Studio Using Volley Lib This My Code
and insert Data to Sqlite to Make App work offline

David Crow 14-Feb-19 8:59am    
In the onResponse() method, does response actually contain the backslash characters? If not, then it should look something like:

{
    "UserID":5,
    "UserName":"asd",
    "Password":"asd",
    "Email":"ss@asd",
    "PhoneNumber":"1213",
    "Logtit":0.0,
    "Latitle":0.0,
    "OfGroup":"a "
}
And your code to extract the various keys would look something like:

String userID = response.getString("UserID");
String userName = response.getString("UserName");
String pwd = response.getString("Password");
...
Richard Deeming 14-Feb-19 9:39am    
It looks like you're storing passwords in plain text. Don't do that!

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

There is NEVER a valid reason to return the user's password to them.

And given the other details you're returning, I hope you've added code on the server to ensure that the request is coming from an authenticated user who has permission to view those details. If not, you're going to get hammered with fines for breaching GDPR and other data protection laws.

1 solution

Volley is just an interface to manage the network traffic in a managed way, it won't do much for you to parse and generate the response. You would need to use the library to parse the JSON—see this link on Android Developer[^] website to see how they are demonstrating this—and as for SQLite, Android has another amazing tool, Room database ORM, in this way, you will be using everything by Google starting with request management by Volley, then Gson and then lastly Room to persist the data in the database.

I recommend that you use Gson to parse this String to objects. However this also requires that you first create a class that matches the signature of that JSON document, the following is what you need,
/* 
{"UserID\":5,\"UserName\":\"asd\",\"Password\":\"asd\",\"Email\":\"ss@asd\",\"PhoneNumber\":\"1213\",\"Logtit\":0.0,\"Latitle\":0.0,\"OfGroup\":\"a \"}"
*/
public class User {
    // I won't go in encapsulation, you can set these to private
    // and create getter/setters.
    public int UserId;
    public String UserName;
    public String Password;
    public String Email;
    public String PhoneNumber;
    public double Logtit;
    public double Latitle;
    public String OfGroup;
}
Now you can go ahead and use Gson to parse the JSON document as an object of User type.
Java
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();

// Capture the JSON document as a variable, I do not know where and how you do that.
User user = gson.fromJson(jsonString, User.class);
This way, you can convert the JSON to a runtime object. See here for an example of this, http://tutorials.jenkov.com/java-json/gson.html[^]

GitHub - google/gson: A Java serialization/deserialization library to convert Java Objects into JSON and back[^]
Room Persistence Library  |  Android Developers[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900