我被困在android中的editText消息没有存储在数据库中,我找不到问题。我对编程很陌生,所以这里的任何帮助都会受到很多赞赏。
MY 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();
?>
我的主要活动文件:
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>
答案 0 :(得分:1)
问题出在BackgroundWorker doInBackgroundMethod中。 您无法通过简单地使用localhost从android连接到localhost。
问题在于:
String poslji_url= "http://localhost/vas_lestvica_login/posiljanje_podatkov/poslji_podatke.php";
您需要确保您的测试机和开发机器在同一网络上。您可以通过wifi和/或usb thetering连接移动设备。 取决于您的设置。
如果您正在使用Android模拟器进行测试,请将其更改为:
String poslji_url= "http://10.0.2.2/vas_lestvica_login/posiljanje_podatkov/poslji_podatke.php";
如果您正在使用像Genymotion这样的第三方模拟器,您可以尝试:
String poslji_url= "http://10.0.3.2/vas_lestvica_login/posiljanje_podatkov/poslji_podatke.php";
否则,例如在真实设备上进行测试时,请尝试以下操作:
ipconfig
,在mac和linux系统上为ifconfig
。它应该给你一些像192.168.43.129
等......
然后将行更改为:
String poslji_url =&#34; http://192.168.43.129/vas_lestvica_login/posiljanje_podatkov/poslji_podatke.php&#34;;
用192.168.43.129
或ipconfig
替换ifconfig
给你。