dongxizhe9755 2014-07-16 18:07
浏览 64
已采纳

使用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 !

  • 写回答

2条回答 默认 最新

  • douye5949 2014-07-16 18:17
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序