[英]Android volley Body in post request
i dont found how i can configure a body in my Post request, im using android studio 我没有找到如何在我的POST请求中配置主体,即时消息使用Android Studio
` `
private RequestQueue mQueue;
private void send() {
Log.i("start", "start");
final String url1 = "http://127.0.0.1:8080/~/in-cse";
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url1, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("Response", response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", String.valueOf(error));
}
}
) {
@Override
public Map getHeaders() throws AuthFailureError {
Log.i("rentrer dedans", "Headers");
HashMap headers = new HashMap();
headers.put("X-M2M-Origin", "admin:admin");
headers.put("Accept", "application/xml");
return headers;
}
@Override
public Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("m2m:ae", "xmlns:m2m="http://www.onem2m.org/xml/protocols" rn="MY_SENSOR");
params.put("api", "app-sensor");
params.put("lbl", "Type/sensor Category/temperature Location/home");
params.put("ff", "false");
return params;
}
};
// add it to the RequestQueue
mQueue.add(getRequest);
}
this is my code, and below there is my body, the thing is i dont know how to translate my body in my post request 这是我的代码,下面是我的身体,问题是我不知道如何在我的帖子请求中翻译我的身体
<m2m:ae xmlns:m2m="http://www.onem2m.org/xml/protocols" rn="MY_SENSOR" > <api>app-sensor</api> <lbl>Type/sensor Category/temperature Location/home</lbl> <rr>false</rr> </m2m:ae>
Would appreciate some help! 希望能有所帮助!
You need to implement things like this :- 您需要实现这样的事情:
String var="xmlns:m2m=\"http://www.onem2m.org/xml/protocols\" rn=\"MY_SENSOR";
Here, '\\' help you inserting more Double Quotes in String values. 在这里,“ \\”可帮助您在字符串值中插入更多的双引号。
Let me know, if anything i miss or you need more help. 让我知道,如果我想念什么,或者您需要更多帮助。 Please like my answer if you find it useful. 如果您觉得有用,请喜欢我的回答。
Easy and more efficent way to post things using volley, 使用齐射发布内容的简便,高效方式,
JSONObject jsonObject= new JSONObject();
try{
jsonObject.put("m2m:ae", "xmlns:m2m=\"http://www.onem2m.org/xml/protocols\" rn=\"MY_SENSOR");
jsonObject.put("api", "app-sensor");
jsonObject.put("lbl", "Type/sensor Category/temperature Location/home");
jsonObject.put("ff", "false");
}catch(JSONException e){
}
Or for More Better ways, There is an Library which i personally recommend to any android developer who love Light weight Volley for Api Integrations. 或以更好的方式,有一个我个人推荐给喜欢Light Volley for Api Integrations的Android开发人员使用的库。
https://github.com/Lib-Jamun/Volley https://github.com/Lib-Jamun/Volley
dependencies {
compile 'tk.jamun:volley:0.0.4'
}
Use this or updated version of this for more efficent use of Volley. 使用此版本或更新版本可以更有效地使用Volley。
Check internet Connectivity : 检查互联网连接:
public class CheckConnection {
public static boolean checkConnection(Activity activity) {
ConnectivityManager cm =
(ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = null;
if (cm != null) {
activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null &&
activeNetwork.isConnected()) {
return true;
} else {
MySnackBar.getInstance().showSnackBarForMessage(activity, R.string.connection_check_no_internet);
}
}
return false;
}
public static boolean checkCon(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = null;
if (cm != null) {
activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null &&
activeNetwork.isConnected();
}
return false;
}
}
then just need to code like 然后只需要像
If (CheckConnection.checkCon(this)){
Call Volley;
}else{
Log.d("Tag","No internet");
}
Let me know further responses 让我知道进一步的回应
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.