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

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接