问题
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 \"}
How can I handle this JSON response in Android Studio Using Volley Lib?
This is my code:
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);}
});
}
回答1:
Most probably you are getting a string respone. If its a string then firstly conver it to Json Object.. you can do this as following below.
JSONObject jsonObject=new JSONObject(response);
If it is JSONObject already then try to conver it to java object as following:
int userId=jsonObject.getInt("UserID");
String userName=jsonObject.getString("UserName");
String pass=jsonObject.getString("Password");
Put the code in your onResponse. and lastly seems like you are getting an array of object. Then create a POJO class like below:
public class UserData {
@Expose
@SerializedName("UserId")
private int userId;
@Expose
@SerializedName("UserName")
private String userName;
.
.
.
//add the extra attribute and create getter and setter
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Then in declare in your code:
ArrayList<UserData>userDataList=new ArrayList();
Follow the below method to set data to arraylist with json data parsing
int userId=jsonObject.getInt("UserID");
String userName=jsonObject.getString("UserName");
String pass=jsonObject.getString("Password");
UserData userInfo=new UserData();
userInfo.setUserId(userId)
userIfo.setUserName(userName);
//add the other attribute similiarly
userDataList.add(userInfo);
回答2:
You could parse it into a Map using GSON. Please refer to this question and answer for more information.
来源:https://stackoverflow.com/questions/54667199/handling-a-json-response