2015-09-20 3 views
-1

Здравствуйте, я пытаюсь вставить данные в Mysql База данных на основе User Rating. Но я получаю 1 Ошибка:Вставить данные в MysqlDatabase через запрос JSON

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(); 
     } 
    } 

Метод вставки:

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(); 
    } 

.php файл, в котором я вставить данные

<?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)); 
?> 

Ошибка:

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 

Получаю пас 1 и прохожу 2, но с этим не получается. Может кто-нибудь мне помочь?

Спасибо

+0

Какая строка получает 'result = sb.toString();' line? –

+0

Что значит? –

+2

Просто добавьте 'Log.e (« pass 2 »,« connection success »+ result);' и проверьте, какая строка получена с сервера –

ответ

1

Изменить код так:

.php файл:

<?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 = $_POST['magazi_id']; 
    $rating=$_POST['ratingNumber']; 
    $comment = $_POST['comment']; 
    $handler->query("INSERT INTO ratings(id, magazi_id, ratingNumber, comment) VALUES('', '$magaziId','$rating','$comment')"); 
    die(); 
?> 

Вашей Асинхронный Задача, как это:

private class MyInsertDataTask extends AsyncTask<String, Void, Void>{ 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(UserProfile.this); 
      pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
      pDialog.setIndeterminate(true); 
      pDialog.setMessage(getString(R.string.dialog_rate_data_submit)); 
      pDialog.setCancelable(false); 
      pDialog.setInverseBackgroundForced(true); 
      pDialog.show(); 
     } 

     @Override 
     protected Void doInBackground(String... params) { 
      nameValuePairs = new ArrayList<>(); 
      nameValuePairs.add(new BasicNameValuePair("magazi_id", "" + 3)); 
      nameValuePairs.add(new BasicNameValuePair("ratingNumber", String.valueOf(percent))); 
      nameValuePairs.add(new BasicNameValuePair("comment", comTxt)); 
      try 
      { 
       httpClient = new DefaultHttpClient(); 
       httpPost = new HttpPost("http://my.chatapp.info/order_api/insertData/insert.php"); 
       httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       response = httpClient.execute(httpPost); 
       httpEntity = response.getEntity(); 
       is = httpEntity.getContent(); 
      } 
      catch(Exception e) 
      { 
       Log.e("Fail 1", e.toString()); 
      } 
      return null; 
     } 
     @Override 
     protected void onPostExecute(Void aVoid) { 
      super.onPostExecute(aVoid); 
      pDialog.dismiss(); 
     } 
    } 
} 

Надеется, что это помогает !! !

+0

спасибо большое! –