繁体   English   中英

要求中有Android Volley Body

[英]Android volley Body in post request

我没有找到如何在我的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);
}

这是我的代码,下面是我的身体,问题是我不知道如何在我的帖子请求中翻译我的身体

<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>

希望能有所帮助!

您需要实现这样的事情:

    String var="xmlns:m2m=\"http://www.onem2m.org/xml/protocols\" rn=\"MY_SENSOR";

在这里,“ \\”可帮助您在字符串值中插入更多的双引号。

让我知道,如果我想念什么,或者您需要更多帮助。 如果您觉得有用,请喜欢我的回答。

使用齐射发布内容的简便,高效方式,

 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){
    }

或以更好的方式,有一个我个人推荐给喜欢Light Volley for Api Integrations的Android开发人员使用的库。

https://github.com/Lib-Jamun/Volley

dependencies {
  compile 'tk.jamun:volley:0.0.4'
}

使用此版本或更新版本可以更有效地使用Volley。

检查互联网连接:

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;
  }
}

然后只需要像

   If (CheckConnection.checkCon(this)){
       Call Volley;
   }else{
       Log.d("Tag","No internet");
   }

让我知道进一步的回应

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM