dqa35710 2014-11-26 10:25 采纳率: 0%
浏览 37

Android Json to Server不发送数据

I got a problem. I read a lot here but unfortunately I can't get an answer for my problem. I want to send a Json post to my Server. The Server should then save the string in my database.

This is my server code so far:

<?php
include('db_connect.php');
$db = new _DB_Connect();
$db->connect();

if(isset($_POST['regid'])){
    $data = json_decode($_POST['regid']);

    $save_entry = "insert into gcm_users (gcm_regid) values ('$data')";
    mysql_query($save_entry) or die (mysql_error());
}else{
    echo 'no data get';
}

?>

And this is my method on my android phone.

public String sendJson(View view){
        InputStream inputStream = null;
        String result = "";
        String url2 = "http://192.168.0.5/control_center/functions/incomming.php";
        TextView textView_result;
        textView_result = (TextView) findViewById(R.id.textView_result);

        //strict mode for networking
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url2);
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-Type" , "application/json");

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("regid", "blablabla");
            String json = jsonObject.toString();

            StringEntity se = new StringEntity(json);

            httpPost.setEntity(se);
            HttpResponse httpResponse = httpClient.execute(httpPost);

            inputStream = httpResponse.getEntity().getContent();
            if(inputStream != null) {
                System.out.println(convertInputStreamToString(inputStream));
            }
            else {
                result = "Did not work!";
            }

            textView_result.setText(httpResponse.toString());
            System.out.println(jsonObject.toString());

        }catch(JSONException e){
            e.printStackTrace();
        }catch(UnsupportedEncodingException e){
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }

        return result;
    }

Now the

System.out.println(convertInputStreamToString(inputStream));

tells me that I got no data

echo 'no data get';

So I get a response form the server but I could find my error, why the post data could not be retrieved. Would appreciate any help or hints. Thank you.

  • 写回答

1条回答 默认 最新

  • douzhi6160 2014-11-26 10:37
    关注
    try this...
    
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("regid", "blablabla");
    String json = jsonObject.toString();
    
    MultipartEntityBuilder multiPart = MultipartEntityBuilder.create();
    
    multiPart.addTextBody("regid",json);
    or 
    multiPart.addTextBody("regid","blablabla");
    
    httpPost.setEntity(multiPart.build());
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘