2017-03-19 88 views
0

我被困在android中的editText消息没有被存储在int数据库中,我找不到问题。我对编程非常陌生,所以这里的任何帮助都会非常有帮助。editText消息没有被保存到数据库中

我的PHP文件:

<?php 

$DB_HOST = "localhost"; 
$DB_USER = "root"; 
$DB_PASSWORD = ""; 
$DB_DATABASE = "vas_uporabniki"; 

$conn = mysqli_connect($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_DATABASE); 

$jakost_bolecine = $_POST["jakost_bolecine_post"]; 

$mysqli_qry ="INSERT into test (jakost_bolecine,datum_in_cas) 
values ('$jakost_bolecine', CURRENT_TIMESTAMP())"; 

if($conn->query($mysqli_qry)=== TRUE){ 

    echo "Uspešno shranjeno"; 
} 
else { 

    echo "Napaka pri shranjevanju".$mysqli_qry."<br>".$conn->error; 
} 
$conn->close(); 

?> 

我mainactivity文件:

public class MainActivity extends ActionBarActivity { 

    // private TextView test; 
    private TextView textView; 
    private ImageView imageView; 
    private SeekBar seekBar; 
    private TextView strI; 

    EditText test; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     seekBar = (SeekBar) findViewById(seek_bar_id); 

     test = (EditText)findViewById(R.id.test_id); 

     initializeVariables(); 

     textView.setText("Jakost bolečine: " + seekBar.getProgress() + "/" + seekBar.getMax()); 

     seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 
      int progress = 0; 

      @Override 
      public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) { 
       progress = progresValue; 
      } 

      @Override 
      public void onStartTrackingTouch(SeekBar seekBar) { 
      } 

      @Override 
      public void onStopTrackingTouch(final SeekBar seekBar) { 

       textView.setText("Jakost bolečine: " + progress + "/" + seekBar.getMax()); 
       if (progress == 0) { 

        imageView.setImageResource(R.drawable.no_pain); 
       } 
       if (progress <= 2 && progress >= 1) { 
        imageView.setImageResource(R.drawable.little_pain); 
        Toast.makeText(getApplicationContext(), "Blaga bolečina!", Toast.LENGTH_SHORT).show(); 
       } 

       if (progress <= 4 && progress >= 3) { 
        imageView.setImageResource(R.drawable.moderata_pain); 
        Toast.makeText(getApplicationContext(), "Zmerna bolečina!", Toast.LENGTH_SHORT).show(); 
       } 

       if (progress <= 6 && progress >= 5) { 
        imageView.setImageResource(R.drawable.severe_pain); 
        Toast.makeText(getApplicationContext(), "Srednja bolečina!", Toast.LENGTH_SHORT).show(); 
       } 

       if (progress <= 8 && progress >= 7) { 
        imageView.setImageResource(R.drawable.very_severe_pain); 
        Toast.makeText(getApplicationContext(), "Dokaj močna bolečina!", Toast.LENGTH_SHORT).show(); 
       } 

       if (progress <= 10 && progress >= 9) { 
        imageView.setImageResource(R.drawable.worst_pain); 
        Toast.makeText(getApplicationContext(), "Zelo močna bolečina!", Toast.LENGTH_SHORT).show(); 
       } 


       String strI = String.valueOf(progress); 


      } 

     }); 


    } 
    private void initializeVariables() { 
     seekBar = (SeekBar) findViewById(R.id.seek_bar_id); 
     textView = (TextView) findViewById(R.id.jakost_bolecin_id); 
     //test = (TextView) findViewById(R.id.test); 
     imageView=(ImageView)findViewById(R.id.slika_bolecine); 

    } 

    public void poslji (View view){ 

     String poslji_data = test.getText().toString().trim(); 
     String type ="Poslji"; 
     BackgroundWorker backgroundWorker = new BackgroundWorker(this); 
     backgroundWorker.execute(type, poslji_data); 
     } 
} 

BackgroundWorker.java

public class BackgroundWorker extends AsyncTask<String,Void,String> { 

    Context context; 
    AlertDialog alertDialog; 
    BackgroundWorker (Context ctx) { 
     context=ctx; 
    } 
    @Override 
    protected String doInBackground (String... params){ 
     String type = params [0]; 
     String poslji_url= "http://localhost/vas_lestvica_login/posiljanje_podatkov/poslji_podatke.php"; 
     if(type.equals("Poslji")) { 
      try { 
       String jakost_bolecine = params[1]; 
       URL url = new URL(poslji_url); 
       HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
       httpURLConnection.setRequestMethod("POST"); 
       httpURLConnection.setDoOutput(true); 
       httpURLConnection.setDoInput(true); 
       OutputStream outputStream = httpURLConnection.getOutputStream(); 
       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); 
       String post_data = URLEncoder.encode("jakost_bolecine_post", "UTF-8") + "=" + URLEncoder.encode(jakost_bolecine, "UTF-8"); 
       bufferedWriter.write(post_data); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 
       InputStream inputStream = httpURLConnection.getInputStream(); 
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1")); 
       String result = ""; 
       String line = ""; 
       while ((line = bufferedReader.readLine()) != null) { 

        result += line; 
       } 
       bufferedReader.close(); 
       inputStream.close(); 
       httpURLConnection.disconnect(); 
       return result; 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
      return null; 
     } 

    } 

和XML main_Act:

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.rok.myapplication.MainActivity"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:paddingTop="50dp" 
     android:id="@+id/krog" 
     android:textSize="20dp" 
     android:text="@string/povleci_krot" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:textAlignment="center" 
     android:textColor="#000"/> 

    <ImageView 
     android:paddingTop="40dp" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/slika_bolecine" 
     android:src="@drawable/no_pain" 
     /> 

    <SeekBar 
     android:id="@+id/seek_bar_id" 
     android:thumb="@drawable/seekbar" 
     android:paddingTop="40dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:max="10" 
     android:progress="0" 
     /> 

    <TextView 
     android:paddingTop="40dp" 
     android:id="@+id/jakost_bolecin_id" 
     android:textSize="20dp" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:textAlignment="center" 
     android:textColor="@color/TextMainColor"/> 



    <Button 
     android:id="@+id/gumb_poslji" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#008000" 
     android:layout_marginTop="50dp" 
     android:text="@string/gumb_shrani" 
     android:textAllCaps="false" 
     android:textColor="@color/white" 
     android:textSize="15dp" 
     android:onClick="poslji"/> 

    <EditText 
     android:id="@+id/test_id" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingTop="40dp" 
     android:hint="vnesi nekaj" 
     android:textAlignment="center" 
     android:textColor="@color/TextMainColor" 
     android:textSize="20dp"/> 

</LinearLayout> 
</ScrollView> 

回答

1

问题出在BackgroundWorker doInBackgroundMethod中。 只能使用本地主机,无法从android连接到本地主机。

这里的问题是:

String poslji_url= "http://localhost/vas_lestvica_login/posiljanje_podatkov/poslji_podatke.php"; 

你需要确保你的试验机和开发机在同一个网络上。您可以通过wifi和/或USB连接移动设备。 取决于您的设置。否则

String poslji_url= "http://10.0.3.2/vas_lestvica_login/posiljanje_podatkov/poslji_podatke.php"; 

,:

如果您正在使用Android模拟器测试,将其更改为:

String poslji_url= "http://10.0.2.2/vas_lestvica_login/posiljanje_podatkov/poslji_podatke.php"; 

如果您使用的是第三方仿真像Genymotion例如,您可以尝试例如在真实设备上进行测试时,请尝试以下操作:

  1. 获取IP地址: fo r窗口,在命令提示符下为简单类型ipconfig或在mac和linux系统上为ifconfig

它应该给你类似192.168.43.129等...

  • 然后将该行更改为:

    字符串poslji_url = “http://192.168.43.129/vas_lestvica_login/posiljanje_podatkov/poslji_podatke.php”;

  • 与任何ipconfigifconfig给你更换192.168.43.129

    +0

    谢谢!我在android studio中使用模拟器,我不知道本地主机不工作。 –

    相关问题