0
我期待從android應用程序獲取一些數據,將其發佈到PHP腳本,然後將其寫入到PostGreSQL數據庫中。我有一些困難,有誰能解釋爲什麼數據沒有被傳輸。我不斷收到很多StrictMode
違規。我希望當用戶點擊應用上的「上傳」時,整個過程就會自動完成,數據會自動寫入我的PGSQL服務器。將JSONobjects從Android發佈到PHP腳本
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);
任何幫助您能給將不勝感激。
你在做的UI線程的網絡連接?如是;那麼這就是爲什麼你會得到'StrictMode'違規... – 2013-03-11 12:43:54
在客戶端沒有json ... – njzk2 2013-03-11 12:49:56
我在'onCreate()'方法中構建'StrictMode',但網絡連接是在上面的代碼不是嗎? – WebDevDanno 2013-03-11 12:50:23