使用Android发布变量

I use my code to upload data in 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);
            }

This code give me a 500 Error

In PHP, I receive my variable with $_REQUEST, so when I debug my app, copy json variable and put it in the full URL, there is no problem.

This show my json variable is OK, as URL_POST_TIENDAS.

Why is there a problem with using POST??? This is not the first time I find this problem. I always change it to GET, but this time, I want to understand why it fails, because I could have a lot of information to upload, so GET is not very appropriated!

EDIT : When seeing logs server, I don't see anything about my 500 error.

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)  

Any Help will be appreciated !

dpp10181
dpp10181 我正在检查它,但我不知道哪个参数检查!我将使用该信息更新我的问题:
大约 6 年之前 回复
douju8113
douju8113 如果没有收到,则不会收到任何内容。有一个原因。你有没有检查过httppost实体?
大约 6 年之前 回复
duanboshe0001
duanboshe0001 我在行中确认:nameValuePairs.add(newBasicNameValuePair(“data”,json));json不是null
大约 6 年之前 回复
dqtu14636
dqtu14636 似乎$_POST['data']是NULL...但为什么????
大约 6 年之前 回复
dou5454954610
dou5454954610 我没有考虑日志,而是考虑实际的请求。如果你只是<?phpecho$_POST,你会怎么看??>?
大约 6 年之前 回复
doujie9882
doujie9882 我检查了日志,没有出现......
大约 6 年之前 回复
dongxiao9583
dongxiao9583 你在服务器端实际收到了什么?你必须收到一些东西,因为错误500来自PHP服务器。
大约 6 年之前 回复

2个回答



在httpPost中设置内容类型</ p>

  httpPost.setHeader(“Accept”,  “application / json”); 
httpPost.setHeader(“Content-type”,“application / json”);
</ code> </ pre>

更新</ h1>
\ n

这是博客做同样的事情。</ p>

将数据作为json从android发送到PHP服务器 </ p>
</ div>

展开原文

原文

Set content type in your httpPost

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

Update

Here is the blog which is doing the same thing.

Send data as json from android to a PHP server

drfcaw7460
drfcaw7460 我想你应该按照上面的链接
大约 6 年之前 回复
dpnw86361
dpnw86361 不,还有问题。
大约 6 年之前 回复



使用时应使用 $ _ POST </ code>数组而不是 $ _ REQUEST </ code> 用于发送参数的 POST </ em>方法。</ p>

我认为这样可以解决您的问题。</ p>
</ div>

展开原文

原文

You should use $_POST array instead of $_REQUEST when you are using POST method for sending params.

I think this will resolve your problem.

douju5933
douju5933 我使用$ _REQUEST来测试GET。 当Bug解决时,我将使用POST
大约 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问