Вот мой RegisterActivity:
public class RegisterActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
}
public void sendRegistration(View view) {
TextView txtName = (TextView) findViewById(R.id.txtName);
TextView txtEmail = (TextView) findViewById(R.id.txtEmail);
TextView txtPassword = (TextView) findViewById(R.id.txtPassword);
String strName = txtName.getText().toString();
String strEmail = txtEmail.getText().toString();
String strPassword = txtPassword.getText().toString();
if (strName.isEmpty() || strEmail.isEmpty() || strPassword.isEmpty()) {
// One or more fields are empty
Toast toast = Toast
.makeText(
getApplicationContext(),
"One or more fields have been left blank. Please fill them in.",
Toast.LENGTH_SHORT);
toast.show();
} else {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.******.**/register.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("name", strName));
nameValuePairs.add(new BasicNameValuePair("email", strEmail));
nameValuePairs.add(new BasicNameValuePair("password", strPassword));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
startActivityForResult(new Intent(view.getContext(),
RegFinishActivity.class), 0);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Моя цель состоит в том, чтобы проверить, что электронная почта, имя, пароль не пустые, а затем отправить запрос на мой сервер, но когда я запускаю этот код на своем устройстве, он говорит: «Applciation потерпел крах». Я использую Android 4.0.3, и в манифесте есть разрешение на доступ в Интернет. Что еще может вызвать эту проблему? Без http-запроса это работает нормально, и отображается следующее представление.
Журнал LogCat от начала до конца приложения:
01-08 08:58:06.793: D/dalvikvm(20672): GC_CONCURRENT freed 60K, 3% free 9439K/9671K, paused 4ms+6ms
01-08 08:58:06.929: D/OpenGLRenderer(20672): Flushing caches (mode 0)
01-08 08:58:15.597: D/AndroidRuntime(20672): Shutting down VM
01-08 08:58:15.597: W/dalvikvm(20672): threadid=1: thread exiting with uncaught exception (group=0x40a2b1f8)
01-08 08:58:15.609: E/AndroidRuntime(20672): FATAL EXCEPTION: main
01-08 08:58:15.609: E/AndroidRuntime(20672): java.lang.IllegalStateException: Could not execute method of the activity
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.view.View$1.onClick(View.java:3044)
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.view.View.performClick(View.java:3511)
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.view.View$PerformClick.run(View.java:14105)
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.os.Handler.handleCallback(Handler.java:605)
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.os.Handler.dispatchMessage(Handler.java:92)
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.os.Looper.loop(Looper.java:137)
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-08 08:58:15.609: E/AndroidRuntime(20672): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 08:58:15.609: E/AndroidRuntime(20672): at java.lang.reflect.Method.invoke(Method.java:511)
01-08 08:58:15.609: E/AndroidRuntime(20672): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-08 08:58:15.609: E/AndroidRuntime(20672): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-08 08:58:15.609: E/AndroidRuntime(20672): at dalvik.system.NativeStart.main(Native Method)
01-08 08:58:15.609: E/AndroidRuntime(20672): Caused by: java.lang.reflect.InvocationTargetException
01-08 08:58:15.609: E/AndroidRuntime(20672): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 08:58:15.609: E/AndroidRuntime(20672): at java.lang.reflect.Method.invoke(Method.java:511)
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.view.View$1.onClick(View.java:3039)
01-08 08:58:15.609: E/AndroidRuntime(20672): ... 11 more
01-08 08:58:15.609: E/AndroidRuntime(20672): Caused by: android.os.NetworkOnMainThreadException
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-08 08:58:15.609: E/AndroidRuntime(20672): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-08 08:58:15.609: E/AndroidRuntime(20672): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-08 08:58:15.609: E/AndroidRuntime(20672): at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-08 08:58:15.609: E/AndroidRuntime(20672): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-08 08:58:15.609: E/AndroidRuntime(20672): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-08 08:58:15.609: E/AndroidRuntime(20672): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-08 08:58:15.609: E/AndroidRuntime(20672): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-08 08:58:15.609: E/AndroidRuntime(20672): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-08 08:58:15.609: E/AndroidRuntime(20672): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-08 08:58:15.609: E/AndroidRuntime(20672): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-08 08:58:15.609: E/AndroidRuntime(20672): at sms.web.RegisterActivity.sendRegistration(RegisterActivity.java:58)
01-08 08:58:15.609: E/AndroidRuntime(20672): ... 14 more
01-08 08:58:18.336: I/Process(20672): Sending signal. PID: 20672 SIG: 9