使用PHP Web服务问题将数据从Android应用程序插入PostgreSQL数据库

时间:2013-03-23 15:52:02

标签: java php android json postgresql

我正在解决我遇到的这个问题。我试图允许用户将他们的Android应用程序中的一些数据上传到我开发的网站服务。

使用JSON和Android将数据上传到PHP Web服务,然后将数据“插入”我的PostgreSQL数据库。

由于应用程序在运行时没有产生错

请参阅下面我正在使用的代码,并尝试帮助确定我的错误。我在Google上寻找过教程,但它们都是基于从PHP Web服务读取数据到Android应用程序,但我希望从Android应用程序发送原始数据。

DataPost活动

public void postData() throws JSONException{
       Toast.makeText(DataSummary.this, "Done! Check your profile online to see your record.", Toast.LENGTH_LONG).show();

       Thread trd = new Thread(new Runnable(){
           public void run(){
             //Create a new HttpClient and Post Header
               HttpClient httpclient = new DefaultHttpClient();
               HttpPost httppost = new HttpPost("http://users.aber.ac.uk/dwd/mfb/php/jsonscript.php");
               JSONObject json = new JSONObject();

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

               try {
                   //JSON data:
                   json.put("photo", ba1.toString());
                   json.put("name", name);
                   json.put("description", description);
                   json.put("latitude", latitude);
                   json.put("longitude", longitude);
                   json.put("project", project);
                   json.put("owner", username);

                   JSONArray postjson = new JSONArray();
                   postjson.put(json);

                   //Post the data
                   httppost.setHeader("json", json.toString());
                   httppost.getParams().setParameter("jsonpost", postjson);

                   //Execute HTTP Post Request
                   System.out.println(json);
                   HttpResponse response = httpclient.execute(httppost);

                   //for JSON
                   if(response != null)
                   {
                       InputStream is = response.getEntity().getContent();

                       BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                       StringBuilder sb = new StringBuilder();

                       String line = null;
                       try{
                           while((line = reader.readLine()) != null){
                               sb.append(line + "\n");
                           }
                       } catch (IOException e){
                           e.printStackTrace();
                       } finally {
                           try {
                               is.close();
                           } catch(IOException e){
                               e.printStackTrace();
                           }
                       }
                   }

               } catch(ClientProtocolException e){
                   e.printStackTrace();
               } catch (IOException e){
                   e.printStackTrace();
               } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           }
       });
       trd.start();
   }

PHP Webservice

<?php
session_start();
$conn = pg_connect("database_string");

//VARIABLES TO BE WRITTEN TO THE DATABASE
$photo = $_REQUEST["photo"];

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

$name = json_decode(stripslashes($_POST["name"]));
$safe_name = pg_escape_string($name);
$desc = json_decode(stripslashes($_POST["description"]));
$safe_desc = pg_escape_string($desc);
$latitude = json_decode(stripslashes($_POST["latitude"]));
$longitude = json_decode(stripslashes($_POST["longitude"]));
$project = json_decode(stripslashes($_POST["project"]));
$owner = json_decode(stripslashes($_POST["owner"]));

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

//SQL STATEMENT HERE FOR INSERT

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

pg_close($conn);

?>

任何能提供一些建议/教程/代码解决方案的人都会成为我书中的英雄!

1 个答案:

答案 0 :(得分:1)

SELECT查询是否会返回任何内容?我不是PHP专家,但对我来说,看起来你发送的变量是错误的,所以不应该:

$id = pg_query("SELECT * FROM users WHERE email = $owner");

但是

$id = pg_query("SELECT * FROM users WHERE email ='".$owner."'");

类似于INSERT查询。 其他想法:

  • 当你只想要一列时,不要做SELECT *它会慢一些。例如,在9.2中使用仅索引扫描,您可以直接从索引(电子邮件,ID)
  • 返回id
  • 如果你只想使用用户的id,最好把它放在insert查询的子查询中 INSERT INTO records ( ... ,owner) VALUES (... ,(SELECT id FROM users WHERE email='".$owner."')") 您甚至可以在最后添加RETURNING owner,以便在插入查询中获取所有者ID,如果您在其他地方需要它。