将JSONobjects从android发布到PHP脚本

时间:2013-03-11 12:41:49

标签: php android json postgresql

我希望从Android应用程序中获取一些数据,将其发布到PHP脚本,然后将其写入我的PostGreSQL数据库。我有一些困难,有人可以解释为什么数据没有被转移。我不断收到很多StrictMode次违规行为。我希望当用户点击“上传”按钮时在应用程序上,整个过程是自动化的,数据自动写入我的PGSQL服务器。

Android应用

protected void syncToWebService(final JSONObject json){        
       HttpClient httpclient = new DefaultHttpClient();
       HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000);
       HttpResponse response;
       String httppost = "http://users.aber.ac.uk/dwd/mfb/php/jsonscript.php";    

       try 
       {
           HttpPost post = new HttpPost(httppost);

           Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), i);
           ByteArrayOutputStream bao = new ByteArrayOutputStream();
           bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
           byte[] ba = bao.toByteArray();
           String ba1=Base64.encodeToString(ba, i);

           ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

           postParameters.add(new BasicNameValuePair("photo", ba1.toString()));
           postParameters.add(new BasicNameValuePair("name", resultName.getText().toString()));
           postParameters.add(new BasicNameValuePair("description", resultDescription.getText().toString()));
           postParameters.add(new BasicNameValuePair("latitude", resultLat.getText().toString()));
           postParameters.add(new BasicNameValuePair("longitude", resultLong.getText().toString()));
           postParameters.add(new BasicNameValuePair("project", resultProject.getText().toString()));
           postParameters.add(new BasicNameValuePair("owner", username));
           //response = CustomHttpClient.executeHttpPost(httppost, postParameters);

           post.setEntity(new UrlEncodedFormEntity(postParameters));
           response = httpclient.execute(post);

           /*Checking response variable*/
           if(response!=null){
               InputStream in = response.getEntity().getContent();
           }
       }

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

PHP文件

    $conn = pg_connect("dbconnection_string");

    $jsonString = file_get_contents('php://input');
    $jsonObj = json_decode($jsonString, true);

    if(!empty($jsonObj)) {

            try {
                    $name = jsonObj['name'];
                    $desc = jsonObj['description'];
                    $latitude = jsonObj['latitude'];
                    $longitude = jsonObj['longitude'];
                    $project = jsonObj['project'];
                    $owner = jsonObj['owner'];
            }

    }
    //decode photo string
    $photo = $_REQUEST["photo"];

    echo $photo;
    $binary=base64_decode($photo);
    header('Content-Type: bitmap; charset=utf-8');

    $id = pg_query("SELECT * FROM users WHERE email = $owner");
    $id_assoc = pg_fetch_assoc($id);
    $id_res = $id_assoc['u_id'];

    $res = pg_query("INSERT INTO records (photo, name, description, latitude, longitude, project, owner) VALUES ('$photo', '$name', '$desc', '$latitude', '$longitude', '$project', '$id_res'");

    pg_close($conn);

非常感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

更改此项,您错过了$

中的jsonObj
try {
                $name = jsonObj['name'];
                $desc = jsonObj['description'];
                $latitude = jsonObj['latitude'];
                $longitude = jsonObj['longitude'];
                $project = jsonObj['project'];
                $owner = jsonObj['owner'];
    }

try {
                $name = $jsonObj['name'];
                $desc = $jsonObj['description'];
                $latitude = $jsonObj['latitude'];
                $longitude = $jsonObj['longitude'];
                $project = $jsonObj['project'];
                $owner = $jsonObj['owner'];
    }