服务器上的 HttpPost 检索,使用凌空库并覆盖 getParams 不会检索



根据http://www.androidhive.info/2014/05/android-working-with-volley-library-1/第6部分,我要发送用户名和密码,并检索登录状态;和第6部分的描述一样!

我正在使用volley库,和重写getParams函数来设置键值。并作为POST发送到php页面。

这是来自volley库的代码片段:

url = "http://www.myurl.com/app/page.php";
TAG = "tagToCancelReq";
JsonObjectRequest postRequest = new JsonObjectRequest (Request.Method.POST, url, 
    new Response.Listener<String>() 
    {
        @Override
        public void onResponse(JsonObject response) {
            // response
            Log.d("Response", response.toString());
        }
    }, 
    new Response.ErrorListener() 
    {
         @Override
         public void onErrorResponse(VolleyError error) {
             // error
            Log.d("Error.Response", response);
       }
    }
) {     
    @Override
    protected Map<String, String> getParams() 
    {  
            Map<String, String>  params = new HashMap<String, String>();  
            params.put("user", "client1");  
            params.put("pass", "123");
            return params;  
    }
};
queue.addToRequest(postRequest,TAG);

这是php代码:

**<?php
/* Encode array to JSON string */
function encodearray($posts) {
    header('Content-type: application/json');
    echo json_encode(array('posts'=>$posts));
}
$response = array();
if (isset($_POST['p']) && isset($_POST['v'])) {
    $response["success"] = 1;
    foreach ($_POST as $key => $value)
    {
        $response[$key] = $value;
    }
    $con = mysql_connect("localhost", "username", "password") or die(mysql_error());
    $db = mysql_select_db("db") or die(mysql_error());
    $strsql = "INSERT INTO `users` (";
    $strsql .= implode(",", array_keys($response));
    $strsql .= ") VALUES ('";
    $strsql .= implode("','", array_values($response));
    $strsql .= "')";
    mysql_query($strsql) or die($db->error());
    /* encode the JSON post from the array */
    encodearray($response);
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
    // echoing JSON response
    echo json_encode($response);
}
?>**

这样,服务器响应success = 0.

但是当使用httpPost和Asynctask这样时,服务器正确检索请求和响应。代码:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.myurl.com/app/page.php");
// Add your data   
List < NameValuePair > nameValuePairs = new ArrayList < NameValuePair > (5);
nameValuePairs.add(new BasicNameValuePair("user", "client1"));
nameValuePairs.add(new BasicNameValuePair("pass", "123"));

try {
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    Log.d("myapp", "works till here. 2");
        try {
        HttpResponse response = httpclient.execute(httppost);
        Log.d("myapp", "response " + response.getEntity());
        } catch (ClientProtocolException e) {
            e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
 e.printStackTrace();

}

第一种方式有什么问题?

甚至当我使用本地web服务器(wamp服务器)时,将URL更改为

http://10.0.2.2:80/adnroid_server/get_All.php";

并在模拟器上运行应用程序,第一种方式(volley),服务器不响应(也没有错误),第二种方式(httPost)每件事都是正确的和响应检索。

使用不同类型的请求。

5. Making String request.

最新更新