0

我希望从 android 应用程序中获取一些数据,将其发布到 PHP 脚本,然后将其写入我的 PostGreSQL 数据库。我遇到了一些困难,谁能解释为什么没有传输数据。我不断收到很多StrictMode违规行为。我希望当用户在应用程序上单击“上传”时,整个过程是自动化的,并且数据会自动写入我的 PGSQL 服务器。

安卓应用

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);

您可以提供的任何帮助将不胜感激。

4

1 回答 1

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'];
    }
于 2013-03-11T12:44:35.847 回答