2014-12-03 127 views
0

我一直在關注一個YouTube視頻,它解釋瞭如何將數據從android應用程序插入到mySQL。 它不適合我,我不知道我做錯了什麼。只要我執行它就會中斷。甚至沒有加載textViews和按鈕從android應用程序插入數據到mySql數據庫

這是我的MainActivity:

package com.example.insertbbdd; 

import java.io.IOException; 
import java.io.InputStream; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 



import android.app.Activity; 
import android.os.Bundle; 
import android.os.StrictMode; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    EditText eName, eAge, eMail; 
    Button bSumbit; 

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

     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 

     eName = (EditText) findViewById(R.id.editName); 
     eAge = (EditText) findViewById(R.id.editAge); 
     eMail = (EditText) findViewById(R.id.editMail); 
     bSumbit = (Button) findViewById(R.id.submitButton); 

     bSumbit.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       InputStream is = null; 

       String name = "" + eName.getText().toString(); 
       String age = "" + eAge.getText().toString(); 
       String email = "" + eMail.getText().toString(); 

       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 

       nameValuePairs.add(new BasicNameValuePair("name", name)); 
       nameValuePairs.add(new BasicNameValuePair("age", age)); 
       nameValuePairs.add(new BasicNameValuePair("email", email)); 


       try{ 
        HttpClient httpClient = new DefaultHttpClient(); 

        HttpPost httpPost = new HttpPost("http://accessibility.es/prueba/script.php"); 

        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

        HttpResponse response = httpClient.execute(httpPost); 

        HttpEntity entity = response.getEntity(); 

        is = entity.getContent(); 

        String msg = "Data entered succesfully"; 
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); 

       } 
       catch(ClientProtocolException e){ 
        Log.e("Client Protocol", "Log_tag"); 
        e.printStackTrace(); 
       } 
       catch(IOException e){ 
        Log.e("Log tag", "IOException"); 
        e.printStackTrace(); 
       } 

      } 
     }); 


    } 


} 

這是我main_activity.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/submitButton" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/editName" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="130dp" 
     android:text="Button" /> 

    <TextView 
     android:id="@+id/editMail" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/submitButton" 
     android:layout_alignRight="@+id/editName" 
     android:layout_marginBottom="54dp" 
     android:layout_marginRight="17dp" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/editAge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/editMail" 
     android:layout_alignLeft="@+id/editMail" 
     android:layout_marginBottom="43dp" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/editName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/editAge" 
     android:layout_alignParentLeft="true" 
     android:layout_marginBottom="35dp" 
     android:layout_marginLeft="90dp" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 

這是我AndroidManifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.insertbbdd" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="14" 
     android:targetSdkVersion="21" /> 
    <uses-permission android:name="android.permission.INTERNET"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+1

可能重複的[android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception ) – 2014-12-03 12:04:06

回答

2

我猜你正在接收ClassCastException,因爲你試圖在TextView上調用(EditText)

改變這一點:

TextView eName, eAge, eMail; 

eName = (TextView) findViewById(R.id.editName); 
eAge = (TextView) findViewById(R.id.editAge); 
eMail = (TextView) findViewById(R.id.editMail); 
bSumbit = (Button) findViewById(R.id.submitButton); 

或TextView的更改XML標籤的EditText。

<EditText /> 

注:什麼是別人指出也是正確的。如果您單擊提交按鈕,您將得到

android.os.NetworkOnMainThreadException

,所以你需要爲使用HandlerAsyncTask

0

試試這個..

try{ 
       new Thread(new Runnable() { 
      public void run() { 

       HttpClient httpClient = new DefaultHttpClient(); 

       HttpPost httpPost = new HttpPost("http://accessibility.es/prueba/script.php"); 

       httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

       HttpResponse response = httpClient.execute(httpPost); 

       HttpEntity entity = response.getEntity(); 

       is = entity.getContent(); 

       String msg = "Data entered succesfully"; 
       Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); 

      } 
      catch(ClientProtocolException e){ 
       Log.e("Client Protocol", "Log_tag"); 
       e.printStackTrace(); 
      } 
      catch(IOException e){ 
       Log.e("Log tag", "IOException"); 
       e.printStackTrace(); 
      } 

       } 
     }).start(); 
+0

你不能在UI線程上執行網絡任務。 – H4SN 2014-12-03 12:42:31

相關問題