服务器响应采用 json 形式



我想从服务器发送和接收数据。为此,我使用了Volley.代码如下。Volley可以接收Json格式的数据。服务器可以Json格式发送和接收数据。如何将此 Json 数据转换为用户可读的JAVA格式?其他类中大约有 10 个方法。下面的类包含网络调用的方法,并且还与MainActivity交互。

public class Api_Volley {
String data;
String flag;
public void my_volley_post (String url , JSONObject jsonObject , final Context context ) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url  , jsonObject , new Response.Listener(){
@Override
public void onResponse(Object response) {
String flag = response.toString();
Toast.makeText( context , flag , Toast.LENGTH_LONG ).show();
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context , "Wrong" , Toast.LENGTH_LONG).show();
error.printStackTrace();
}
});
ApiVolleySingeltonClass.getInstance(context).addToRequestque(jsonObjectRequest);
}
}

另一个类中的方法:

public void showAllOrderByUserId() {
try {
data_args.put("userId", 2);
} catch (JSONException e) {
e.printStackTrace();
}
try {
data_action.put("action", "showAllOrderByUserId");
data_action.put("args", data_args);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
route = "/order";
new Api_Volley().my_volley_post(addUserUrl + route, data_action, context);
} 
  1. 您可以使用 json 数据,例如

    {"userNodes":[{"id":"1","name":"Enamul Haque"}]}

  2. 你可以像波纹管一样使用凌空抽射

    private void doLoginAction() {
    
    String url_login = "http://www.lineitopkal.com/android/login.php";
    
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url_login,
    new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
    //pDialog.dismiss();
    
    try {
    JSONObject jsonObject = new JSONObject(response);
    JSONArray loginNodes = jsonObject.getJSONArray("userNodes");
    for (int i = 0; i < loginNodes.length(); i++) {
    JSONObject jo = loginNodes.getJSONObject(i);
    String id = jo.getString("id");
    Log.e("id ::",id);                                
    String name = jo.getString("name");
    Log.e("name ::",name);
    
    }
    } catch (JSONException e) {
    e.printStackTrace();
    }
    
    }
    },
    new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
    try {
    if (error instanceof TimeoutError ) {
    //Time out error
    }else if(error instanceof NoConnectionError){
    //net work error
    } else if (error instanceof AuthFailureError) {
    //error
    } else if (error instanceof ServerError) {
    //Erroor
    } else if (error instanceof NetworkError) {
    //Error
    } else if (error instanceof ParseError) {
    //Error
    }else{
    //Error
    }
    //End
    
    } catch (Exception e) {
    
    }
    }
    }) {
    @Override
    protected Map<String, String> getParams() {
    Map<String, String> params = new HashMap<>();
    //Post parameter like bellow
    params.put("uname", "era@gmail.com");
    params.put("pass", "123456");
    
    return params;
    }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
    }
    

像这样创建响应格式

public class Post {
long id;
Date dateCreated;
String title;
String author;
String url;
String body;
}

提出如下请求后

public void my_volley_post (String url , JSONObject jsonObject , final Context context ) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url  , jsonObject , new Response.Listener(){
@Override
public void onResponse(String response) {
GsonBuilder gsonBuilder = new GsonBuilder();
gson = gsonBuilder.create();

这将是您的实体

Post post = gson.fromJson(response, Post.class));

},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context , "Wrong" , Toast.LENGTH_LONG).show();
error.printStackTrace();
}
});
ApiVolleySingeltonClass.getInstance(context).addToRequestque(jsonObjectRequest);
}

你必须解析JSON,并且必须根据需要显示:

通过这个,您可以使用GSON(它很容易( 使用此站点将 JSON 转换为 pojo 类。 点击这里

这些是您可以使用和实现的参考站点:

http://www.vogella.com/tutorials/JavaLibrary-Gson/article.html

https://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

https://kylewbanks.com/blog/tutorial-parsing-json-on-android-using-gson-and-volley

最新更新