使用Android发布变量

时间:2014-07-16 18:07:51

标签: php android mysql

我使用我的代码在MySQL上传数据。

        HttpPost httppost = new HttpPost(URL_POST_TIENDAS);
        HttpClient client = new DefaultHttpClient();

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("data", json));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        // Log.e(TAG, "Ejecutando POST: Mandando tiendas");
        HttpResponse httpResponse = client.execute(httppost);
        if (httpResponse != null) {
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = httpResponse.getEntity();
                if (entity != null) {
                    message = NetworkUtils.Entity2String(httpResponse);
                    Log.e(TAG, "Respuesta del Post Tienda:" + message);
                }
            } else {
                Err error = new Err(statusCode, message, "upload_tiendas");
                MyApplication.lErrors.add(error);                   
                this.cancel(true);
            }

此代码给我一个500错误

在PHP中,我使用$ _REQUEST接收变量,因此当我调试我的应用程序时,复制json变量并将其放在完整的URL中,没有问题。

这显示我的json变量是正常的,如URL_POST_TIENDAS。

使用POST时为什么会出现问题?这不是我第一次发现这个问题。 我总是把它改成GET,但是这一次,我想了解它失败的原因,因为我可以上传很多信息,所以GET不是很合适!

编辑:当看到日志服务器时,我看不到任何有关我的500错误的信息。

EDIT2:httpost:

httppost    HttpPost  (id=830032727152) 
aborted false   
abortLock   ReentrantLock  (id=830032727328)    
connRequest null    
entity  UrlEncodedFormEntity  (id=830032731528) 
    chunked false   
    content (id=830032746160)   
        [0...99]    
        [100...199] 
        [200...299] 
        [300...399] 
        [400...499] 
        [500...599] 
        [600...699] 
        [700...724] 
    contentEncoding null    
    contentType BasicHeader  (id=830032747320)  
headergroup HeaderGroup  (id=830032727200)  
    headers ArrayList  (id=830032727216)    
        array   Object[16]  (id=830032727240)   
        modCount    0   
        size    0   
params  BasicHttpParams  (id=830032789608)  
    parameters  null    
releaseTrigger  SingleClientConnManager$ConnAdapter  (id=830032798576)  
uri URI  (id=830032727376)  

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

当您使用 POST 方法发送参数时,您应该使用$_POST数组而不是$_REQUEST

我认为这可以解决您的问题。

答案 1 :(得分:0)

在httpPost中设置内容类型

httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

更新

以下是正在做同样事情的博客。

Send data as json from android to a PHP server