我正在为我遇到的这个问题而抓狂。我试图允许用户将一些数据从他们的 android 应用程序上传到我开发的网站服务。
数据将使用 JSON 和 Android 上传到 PHP Web 服务,然后该服务会将数据“插入”到我的 PostgreSQL 数据库中。
我不确定整个应用程序中的逻辑错误在哪里,因为该应用程序在运行时不会产生错误,但是当我检查我的 PostgreSQL 服务器空间的数据库记录时,没有提交任何数据。
请参阅下面我正在使用的代码,并尝试帮助确定我哪里出错了。我已经在 Google 上寻找教程,但它们都是基于从 PHP Web 服务读取数据到 android 应用程序,但我希望从 android 应用程序发送原始数据。
数据发布活动
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 网络服务
<?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);
?>
任何可以提供一些建议/教程/代码解决方案的人都会是我书中的英雄!