Reputation: 1122
I'm looking to take some data from an android application, post it to a PHP script and then write it into my PostGreSQL database. I'm having some difficulty, could anyone explain why the data is not being transferred. I keep getting a lot of StrictMode
violations. I'm hoping that when the user clicks 'Upload' on the app the whole process is automated and the data automatically written to my PGSQL server.
Android application
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 file
$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);
Any help you can give would be much appreciated.
Upvotes: 0
Views: 464
Reputation: 30488
change this, you missed $
in jsonObj
try {
$name = jsonObj['name'];
$desc = jsonObj['description'];
$latitude = jsonObj['latitude'];
$longitude = jsonObj['longitude'];
$project = jsonObj['project'];
$owner = jsonObj['owner'];
}
to
try {
$name = $jsonObj['name'];
$desc = $jsonObj['description'];
$latitude = $jsonObj['latitude'];
$longitude = $jsonObj['longitude'];
$project = $jsonObj['project'];
$owner = $jsonObj['owner'];
}
Upvotes: 1