我一直在关注YouTube视频,该视频解释了如何将数据从Android应用程序插入到mySQL中。 它对我不起作用,我也不知道自己做错了什么。它只是在我执行它时就会中断。甚至没有加载textViews和Buttons
这是我的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>
答案 0 :(得分:2)
我猜你得到的是ClassCastException,因为你试图在(EditText)
上拨打TextView
。
改变这个:
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);
或将xml标记从TextView更改为EditText。
<EditText />
NB:其他人指出的内容也是正确的。如果您点击提交按钮,您将获得
android.os.NetworkOnMainThreadException
答案 1 :(得分:0)
您需要在与主UI线程分开的线程中执行任何网络操作,例如HttpPost(或者实际上任何其他慢速操作),然后使用回调来更新主线程上的UI。
有关主要文档,请参阅http://developer.android.com/guide/components/processes-and-threads.html和http://developer.android.com/reference/android/os/AsyncTask.html。网上还有许多指南,例如http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html
答案 2 :(得分: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();