I am testing an android to PHP interface and PHP to android reply system using JSON. From the Android app it contacts my PHP page and creates an entry into my database as should but the reply from the PHP to android is full of garbage. I was wondering where the problem is:

我正在使用JSON测试android到PHP接口和PHP到android回复系统。从Android应用程序它接触我的PHP页面并创建一个进入我的数据库的条目,但从PHP到android的回复充满了垃圾。我想知道问题出在哪里:

//PHP
<?php 

require_once('functions.php');

$json = file_get_contents('php://input');
$obj = json_decode($json,true);



execute("insert into android_test (USERNAME,FULLNAME) values ('".$obj->{'UserName'}."', '".$obj->{'FullName'}."')"); //android_test (ID,IDUSERS,USERNAME,FULLNAME)
execute("commit");

  $posts = array($json);
  //$posts = array(1);

    header('Content-type: application/json');
    echo json_encode(array('posts'=>$posts));


?>

the android app:

Android应用程序:

public void clickbuttonRecieve(View v) { //send to server
        try {
            JSONObject json = new JSONObject();
            json.put("UserName", "test2");
            json.put("FullName", "1234567");


            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams,
                    TIMEOUT_MILLISEC);
            HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
            HttpClient client = new DefaultHttpClient(httpParams);

            String url = "https://www.mypage.com/scripts/androidportalinput.php"; 

            HttpPost request = new HttpPost(url);
            request.setEntity(new ByteArrayEntity(json.toString().getBytes(
                    "UTF8")));
            request.setHeader("json", json.toString());


            HttpResponse response = client.execute(request); //get feedback
            HttpEntity entity = response.getEntity();
            // If the response does not enclose an entity, there is no need
            if (entity != null) {
                InputStream instream = entity.getContent();

                String result = RestClient.convertStreamToString(instream); //get feedback
                Log.i("Read from server", result);
                Toast.makeText(this,  result,
                        Toast.LENGTH_LONG).show();
            }
        } catch (Throwable t) {
            Toast.makeText(this, "Request failed: " + t.toString(),
                    Toast.LENGTH_LONG).show();
        }
    }

The popup I receive back is the 'posts' but contain the pages html code as well so the json encoded data is full garbage that is sent to my android app.

我收到的弹出窗口是“帖子”,但包含页面的html代码,所以json编码的数据是完整的垃圾,发送到我的Android应用程序。

Any resolving this?

有没有解决这个问题?

Help please

2 个解决方案

#1


0

I could be wrong but I see this being the following possibilities.

我错了,但我发现这有以下几种可能性。

1) You are making a bad request. Try testing the request in a browser to make sure it reply properly there.

1)你提出了一个错误的请求。尝试在浏览器中测试请求,以确保它在那里正确回复。

2) You serving the request improperly. Is there any reason you would be including a head/foot in the reply. Perhaps some how your webserver is adding content/redirecting??

2)您未正确地提供请求。你有什么理由在回复中包括头/脚。也许你的网络服务器如何添加内容/重定向?

3)The data is getting corrupted in route...probably not...not sure what you could do about this.

3)数据在路线中被破坏......可能不是......不确定你能做些什么。

4)Your app is corrupting data. Again, unlikely..you can try to display the data once retrieve to minimize the chance of something effecting the data.

4)您的应用正在破坏数据。同样,不太可能......您可以尝试在检索后显示数据,以最大限度地减少影响数据的可能性。

If its including html that belongs to your website I'm pretty sure its #2.

如果它包含属于您网站的HTML,我很确定它的#2。

更多相关文章

  1. php 封装原生数据导出的方法
  2. 当将带有撇号的单词保存到数据库时,mysql_real_escape_string会添
  3. 从php中的单行获取数据[重复]
  4. php扫马、内网查mysql数据库账号密码(内网webshell渗透)
  5. thinkphp5 数据库和模型详解 之1 数据库基础
  6. 一些PHP相关的数据比较
  7. Wordpress查询另一个wordpress数据库for循环
  8. 数据库分卷备份 thinkphp3.2版
  9. 速率结构的数据库/算法

随机推荐

  1. eclipse中写python文件找不到
  2. openpyxl读取excel中公式的结果值
  3. caffe中各语言预处理对应方式
  4. Python 3基础教程1-环境安装和运行环境
  5. joblib跟踪进展。并行执行
  6. 如何停止Py_Initialise应用程序的崩溃?
  7. X和y有不相容的形状
  8. Cloud Pub / Sub Demo:403未授权用户执行
  9. 解决Linux下运行Python脚本显示“: 没有
  10. Python学习日记20180413