我正在撕裂我的頭髮,因爲我遇到了這個問題。我試圖讓用戶從他們的android應用程序上傳一些數據到我開發的網站服務。使用PHP Web服務插入數據從Android應用程序到PostgreSQL數據庫問題
的數據是使用JSON和Android的PHP Web服務上載由它來「插入」數據到我的PostgreSQL數據庫。
我不清楚其中的邏輯錯誤是在我的整個應用程序的應用程序產生在運行時沒有錯誤,但是當我檢查我的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);
?>
任何人誰可以提供一些建議/教程/代碼解決方案將是在我的書英雄!
也許一個愚蠢的建議...但交易? 'INSERT'語句是否被提交或回滾到'pg_close'? – mthmulders 2013-03-23 16:12:25
你會建議擺脫'pg_close'並再次測試提交嗎? – WebDevDanno 2013-03-23 16:53:02
@mthmulders擺脫'pg_close();' – WebDevDanno 2013-03-23 17:26:20