2017-06-27 78 views
0

數據沒有從android中的MySql中插入沒有錯誤的代碼,我已經使用AsyncThread定義了它們 - doInBackground和onPostExecute..Its顯示了我所擁有的東西在Json對象中定義如果未插入數據,請在其他條件中嘗試Catch方法'Sorry Try Again'。沒有錯誤:數據沒有從Android中插入PHP MySql

InputStream is = null; 
String res = null; 
String line = null; 
int code; 

的onClick代碼

  AsyncTaskRunner runner = new AsyncTaskRunner(); 
      runner.execute(); 

      question = addque.getText().toString(); 
      choice1 = addc1.getText().toString(); 
      choice2 = addc2.getText().toString(); 
      choice3 = addc3.getText().toString(); 
      answer = addanswer.getText().toString(); 
      Explanation = addexplan.getText().toString(); 

AsyncTaskRunner類

private class AsyncTaskRunner extends AsyncTask<String, String, String> { 
    @Override 
    protected String doInBackground(String... arg0) { 
     // TODO Auto-generated method stub 

     ArrayList<NameValuePair> nameValuePairs = new 
ArrayList<NameValuePair>(); 

     nameValuePairs.add(new BasicNameValuePair("question", question)); 
     nameValuePairs.add(new BasicNameValuePair("choice1", choice1)); 
     nameValuePairs.add(new BasicNameValuePair("choice2", choice2)); 
     nameValuePairs.add(new BasicNameValuePair("choice3", choice3)); 
     nameValuePairs.add(new BasicNameValuePair("answer", answer)); 
     nameValuePairs.add(new BasicNameValuePair("Explanation", 
Explanation)); 

     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new 
HttpPost("http://192.168.43.4/insert/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()); 

     } 

     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(); 
      res = sb.toString(); 
      Log.e("pass 2", "connection success "); 
     } catch (Exception e) { 
      Log.e("Fail 2", e.toString()); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 

     try 
     { 
       JSONObject json_data; 
       json_data = new JSONObject(res); 
       code = json_data.getInt("code"); 

       if(code==1) 
       { 
      Toast.makeText(getActivity(), "Inserted Successfully", 
       Toast.LENGTH_SHORT).show(); 
       } 
       else 
       { 
      Toast.makeText(getActivity(), "Sorry, Try Again", 
       Toast.LENGTH_LONG).show(); 
       } 
     } 
     catch(Exception e) 
     { 
       Log.e("Fail 3", e.toString()); 
     } 
     } 

    } 

PHP代碼

<?php 
$host='localhost'; 
$uname='root'; 
$pwd=''; 
$db="quizDB"; 

$con = mysql_connect($host,$uname,$pwd) or die("connection failed"); 
mysql_select_db($db,$con) or die("db selection failed"); 

$question=$_REQUEST['question']; 
$choice1=$_REQUEST['choice1']; 
$choice2=$_REQUEST['choice2']; 
$choice3=$_REQUEST['choice3']; 
$answer=$_REQUEST['answer']; 
$Explanation=$_REQUEST['Explanation']; 

$flag['code']=0; 

if($r=mysql_query("insert into quizquestion values('$question','$choice1', 
$choice2, '$choice3', '$answer', '$Explanation') ",$con)) 
{ 
    $flag['code']=1; 
    echo"hi"; 
} 

print(json_encode($flag)); 
mysql_close($con); 
?> 

糾正我,如果我錯了。

回答

1

首先獲取要插入的所有字符串,然後調用AsyncTask。可能是這個解決方案。

question = addque.getText().toString(); 
    choice1 = addc1.getText().toString(); 
    choice2 = addc2.getText().toString(); 
    choice3 = addc3.getText().toString(); 
    answer = addanswer.getText().toString(); 
    Explanation = addexplan.getText().toString(); 

    AsyncTaskRunner runner = new AsyncTaskRunner(); 
    runner.execute(); 

並且也改變這2條線。通過UTF-8 UrlEncodeFormEntity

UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8"); 
    httppost.setEntity(formEntity); 

編輯

我想你忘記添加到concatination $選擇2,並改變這樣的查詢。傳遞第一個fieldName,然後傳遞值。

$sql = "INSERT INTO MyGuests (fieldName1, fieldName2, fieldName3,fieldName4,fieldName5,fieldName6) 
VALUES ('$question','$choice1','$choice2', '$choice3', '$answer', '$Explanation')"; 

if (mysqli_query($conn, $sql)) { 
    echo "New record created successfully"; 
    $flag['code']=1; 
    echo"hi"; 
} else { 
    echo "Error: " . $sql . "<br>" . mysqli_error($conn); 
} 
+0

從何處以及如何改變這種一個** UrlEncodedFormEntity formEntity =新UrlEncodedFormEntity(namevaluepairs中, 「UTF-8」); httppost.setEntity(formEntity); ** – LazyLearner

+0

在你的asyncTask中。 – Dharmishtha

+0

不工作,顯示相同的吐司..這是在其他條件的jSON – LazyLearner

相關問題