gpt4 book ai didi

php - 使用 php 文件和 android 将数据添加到 postgreSql 数据库

转载 作者:行者123 更新时间:2023-11-30 01:46:30 24 4
gpt4 key购买 nike

我的 Activity 有问题,必须使用 php 文件将数据从 UI 添加到数据库:这是我的 Activity java 代码:

import HttpManager.HttpManager;
import android.R.string;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RapportActivity extends Activity {

// Progress Dialog
private ProgressDialog pDialog;

String params;
JSONParser jsonParser = new JSONParser();
EditText inputCode;
EditText inputLibelle;
EditText inputDescription;
EditText inputEtat;
EditText inputAction;
// url to create new product
private static String url_create_rapport = "http://10.0.2.2/Scripts/Create_rapport.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);

// Edit Text
inputCode = (EditText) findViewById(R.id.textView1);
inputLibelle = (EditText) findViewById(R.id.textView2);
inputDescription = (EditText) findViewById(R.id.textView3);
inputEtat = (EditText) findViewById(R.id.textView4);
inputAction = (EditText) findViewById(R.id.textView5);
// Create button
Button btnCreateRapport = (Button) findViewById(R.id.button1);

// button click event
btnCreateRapport.setOnClickListener(new View.OnClickListener() {
String a;
String b;
String c;
String d;
String e;
@Override
public void onClick(View view) {

a=inputCode.getText().toString();
b=inputLibelle.getText().toString();
c=inputDescription.getText().toString();
d=inputEtat.getText().toString();
e=inputAction.getText().toString();
params="code="+a
+"&lib="+b
+"&desc="+c
+"&State="+d
+"&action="+c;
CreateNewRapport t1= new CreateNewRapport();
t1.execute(url_create_rapport,params);
}
});
}

/**
* Background Async Task to Create new product
* */
class CreateNewRapport extends AsyncTask<String, String, String> {

/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(RapportActivity.this);
pDialog.setMessage("Creating Rapport..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}


protected String doInBackground(String... params) {
String s = HttpManager.posdata(params[0], params[1]);
return s;

}
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}

}
}

这是 xml 文件的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:background="#99E7E5"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Code"
android:textColor="@android:color/black"
android:textColorHint="@android:color/black"
android:textColorLink="@android:color/black" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Libellé"
android:textColor="@android:color/black"
android:textColorHint="@android:color/black" />

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" android:inputType="text">

<requestFocus android:layout_width="wrap_content" />

</EditText>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description"
android:textColor="@android:color/black" />

<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:lines="5"
android:maxLines="5" android:inputType="text"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Etat"
android:textColor="@android:color/black" />

<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Action corrective"
android:textColor="@android:color/black" />

<EditText
android:id="@+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
/>

</LinearLayout>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ajouter"/>


</LinearLayout>

这是我的 php 文件的代码:

<?php
// array for JSON response
$response = array();
$hostname_localhost ='localhost';
$port_localhost =5432;
$database_localhost ='techmobi';
$username_localhost ='openpg';
$password_localhost ='openpgpwd';
$localhost = "host='$hostname_localhost' port='$port_localhost' dbname='$database_localhost' user='$username_localhost' password='$password_localhost'";
$code_tech = $_REQUEST['code'];
$lib_int = $_REQUEST['lib'];
$desc_int = $_REQUEST['desc'];
$etat_int = $_REQUEST['State'];
$action = $_REQUEST['action'];
$connexion =pg_connect($localhost) or die ("Erreur de connexion ".pg_last_error());
$query_search = "insert into rapport values('$code_tech','$lib_int','$desc_int','$etat_int','$action');";
$query_exec = pg_query($query_search) or die(pg_error());
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "requette successfully created.";

// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";

// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}
?>

当我运行我的应用程序时,我在 logCat 上收到了这些错误:

logcat#1 logcat#2

请帮忙!!

最佳答案

您尝试将 TextView 转换为 EditText for R.id.textView3

它显示在您的 logcat 上,ClassCastException,它是由 RapportActivity 第 38 行引起的

关于php - 使用 php 文件和 android 将数据添加到 postgreSql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33642730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com