HttpPost检索服务器,采用凌空库和压倒一切的getParams不检索 [英] HttpPost retrieve on server, using volley library and overriding getParams doesn't retrieve

查看:127
本文介绍了HttpPost检索服务器,采用凌空库和压倒一切的getParams不检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 HTTP .INFO / 2014/05 / Android的劳动与凌空库-1 / ,第6部分,我想送用户名和密码,并获取登录的状态;作为同为第6部分描述!

According to http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ , part 6, I want to send user and password, and retrieve status of login; as same as description of part 6!

我用凌空库和覆盖getParams 功能设置键的值。并派为上传以PHP页面。

I'm using volley library, and override getParams function to set Keys Values. And sent as POST to php page.

这是通过大力抽射库段code:

This is snippet code via volley library:

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 code:

This is php code:

**<?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);
}
?>**

在这种方式,服务器响应成功= 0

In this way, server response success = 0.

但是,当使用 httpPost和AsyncTask的这样,服务器检索请求和响应正确。 code是:

But when using httpPost and Asynctask like this, server retrieve request and response correctly. Code is:

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();

}

什么是错的第一种方式?

What is wrong with first way?

均匀当我使用本地Web服务器(WAMP服务器),并更改网址

Even when i use local web server(wamp server), and change URL to

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

和运行模拟器的应用程序,在第一种方式(凌空),服务器没有响应(没有错误太多),而在第二种方式(httPost)每一件事情是正确的,反应检索。

and run app on emulator, in first way (volley), server doesn't response (no error too), and in second way(httPost) every thing is correct and response retrieved.

推荐答案

使用不同类型的请求。

5. Making String request.

这篇关于HttpPost检索服务器,采用凌空库和压倒一切的getParams不检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆