2015-11-11 37 views
1

我有我的活動一個問題,即必須從UI添加數據使用PHP文件數據的基礎上: 這是我的活動Java代碼:使用PHP文件,將數據添加到一個PostgreSQL數據庫的Android

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

請幫助!

回答

1

您嘗試投TextViewEditTextR.id.textView3

它顯示了你的logcat的,ClassCastException異常,它是由RapportActivity線造成38

相關問題