Hello I am trying to insert Data into Mysql
Database based on User Rating. But I get 1 Error:
The AsyncTask:
private class MyInsertDataTask extends AsyncTask<String, Void, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(UserProfile.this);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setIndeterminate(true);
pDialog.setMessage("Please Wait...");
pDialog.setCancelable(false);
pDialog.setInverseBackgroundForced(true);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("magazi_id", "" + 3));
nameValuePairs.add(new BasicNameValuePair("ratingNumber", String.valueOf(percent)));
nameValuePairs.add(new BasicNameValuePair("comment",comTxt));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://my.chatapp.info/order_api/insertData/insert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONObject json_data = new JSONObject(result);
code=(json_data.getInt("code"));
if(code==1)
{
Toast.makeText(UserProfile.this, "Inserted Successfully",
Toast.LENGTH_SHORT).show();
return String.valueOf(code);
}
else
{
Toast.makeText(UserProfile.this, "Sorry, Try Again",
Toast.LENGTH_LONG).show();
return String.valueOf(code);
}
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
return null;
}
@Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
pDialog.dismiss();
}
}
The insertion method:
private void insertDataToDatabaseFromRating() {
ratingBar = (RatingBar) newLayout.findViewById(R.id.ratingBar);
ratingComment = (EditText) newLayout.findViewById(R.id.ratingComment);
percent = ratingBar.getRating();
comTxt = ratingComment.getText().toString();
if (comTxt.isEmpty()) {
comTxt = " ";
}
MyInsertDataTask task = new MyInsertDataTask();
task.execute();
}
The .php file where i insert the data
<?php
try {
$handler = new PDO('mysql:host=localhost;dbname=project', 'username', 'password');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
echo $e->getMessage();
die();
}
$magaziId = $_REQUEST['magazi_id'];
$rating=$_REQUEST['ratingNumber'];
$comment = $_REQUEST['comment'];
$flag['code']=0;
if($handler->query('INSERT INTO ratings VALUES(, $magaziId, $rating, $comment)'){
$flag['code']=1;
}
print(json_encode($flag));
?>
The Error:
09-20 04:43:11.470 7318-7350/com.order.app.order E/pass 1﹕ connection success
09-20 04:43:11.470 7318-7350/com.order.app.order E/pass 2﹕ connection success
09-20 04:43:11.470 7318-7350/com.order.app.order E/Fail 3﹕ org.json.JSONException: End of input at character 0 of
I get pass 1 and pass 2 but it fails with this. Can anyone help me out?
Thank you