2013-10-17 158 views
2

这是我的Android代码如何在Volley中发送数据请求?

  JSONObject params = new JSONObject(); 
      params.put("username","[email protected]"); 
      params.put("password","hellothere"); 

      JsonObjectRequest loginRequest = new JsonObjectRequest(
        Request.Method.POST, 
        "http://192.168.2.67/tmp/test.php", 
        params, 
        new Response.Listener<JSONObject>() { 
         @Override 
         public void onResponse(JSONObject jsonObject) { 
         Log.d("",""); 

         } 
        }, 
        new Response.ErrorListener() { 
         @Override 
         public void onErrorResponse(VolleyError volleyError) { 
         Log.d("",""); 
         } 
        }); 


      requestQueue.add(loginRequest); 

在PHP服务器代码

  <?php 
       $usr = $_REQUEST['username']; 
       $pwd = $_REQUEST['password']; 
       $resp[$usr]=$pwd; 
       echo json_encode($resp); 
       ?> 

我得到的回应是 { “”:空}

我试着用的Apache HTTP cleint并且它在prybctly 有什么办法我可以用凌空做到这一点?

+0

请参阅这样的回答: http://stackoverflow.com/questions/19837820/volley-jsonobjectrequest-post-request-not-working/19945676#19945676只 – user

回答

2
@Override 
protected Map<String, String> getParams() 
{ 
Map<String, String> params = new HashMap<String, String>(); 
    params.put("username", "SomeUser"); 
    params.put("password", "blabla"); 



return params; 
} 

这是您应该重写getParams()方法的方式。

+1

适用于GET不是POST –

+2

你确定我的朋友?我一直在使用这种方法进行大量的POST请求。它工作正常。 – Orcun

+0

您是否返回JSONOrject或String?我使用由stackoverflow中的某个人发布的CustomVolleyRequest。它工作正常 –

相关问题