2016-09-16 72 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/ –