![](/img/trans.png)
[英]POST request with Volley with headers and body (java, android studio)
[英]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.