2016-09-16 66 views
0

嘿,我希望做這樣的請求,在我的Android應用程序:做一個POST請求與排球

curl --header "Content-Type: text/plain" --request POST --data "ON" http://example.com:8080/rest/items/wakeup 

到目前爲止,我有這樣的:

String url = "http://example.com:8080/rest/items/wakeup"; 

    StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        Toast.makeText(MjpegActivity.this,response,Toast.LENGTH_LONG).show(); 
        //This code is executed if the server responds, whether or not the response contains data. 
        //The String 'response' contains the server's response. 
       } 
     }, 
      new Response.ErrorListener() { //Create an error listener to handle errors appropriately. 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(MjpegActivity.this,error.toString(),Toast.LENGTH_LONG).show(); 
        //This code is executed if there is an error. 
       } 
     }){ 
     @Override 
     protected Map<String, String> getParams() { 
      Map<String, String> MyData = new HashMap<String, String>(); 
      MyData.put("AAAAAA", "BBBBBB"); //Add the data you'd like to send to the server. 
      return MyData; 
     } 
    }; 
    RequestQueue MyRequestQueue = Volley.newRequestQueue(this); 
    MyRequestQueue.add(MyStringRequest); 
} 

這是從這裏取: https://www.simplifiedcoding.net/android-volley-post-request-tutorial/ 有人可以幫我弄清楚如何使這項工作?我知道我發送的數據是在AAAAAA和BBBBBB的地方,但我真的不知道如何發送字符串「ON」。

+0

你是什麼意思字符串「ON」位置(要張貼到您的文件替換i.php)?你能澄清一下嗎?你想知道如何發送參數到這個? –

+1

請參閱[這裏的答案](http://stackoverflow.com/a/26270185/1270789),第二個例子。你可能只想爲'getBody()'返回String(「ON」)。getBytes();'而不是'getParams()'。 –

+0

檢查http://stackoverflow.com/questions/31552242/sending-http-post-request-with-android –

回答

0

打開到服務器的80端口的連接,併發送這條短信

POST /i.php HTTP/1.1 
Host: denta.dev:8081 
User-Agent: curl/7.47.0 
Accept: */* 
Content-Type: text/plain 
Content-Length: 2 

ON 
+0

在Android中使用套接字的代碼無處不在,例如http://androidsrc.net/android-client-server-using-sockets-client-implementation/ –