добавить данные в базу данных postgreSql с помощью файлов php и Android

1

У меня проблема с моей деятельностью, которая должна добавлять данные из пользовательского интерфейса в базу данных с использованием php файлов: это мой 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:

Изображение 174551 Изображение 174551

Пожалуйста, помогите!

Теги:

1 ответ

1
Лучший ответ

Вы пытаетесь использовать TextView для EditText для R.id.textView3

Он отображается на вашем логарифме, ClassCastException и вызван линией 38 RapportActivity

Ещё вопросы

Сообщество Overcoder
Наверх
Меню