2012-01-08 47 views
1

这里是我的RegisterActivity:的HTTPRequest似乎不工作(在Web应用程序中注册的用户)

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(); 
     } 

    } 
} 

}

我的目标是什么,是为了验证电子邮件,姓名,密码不是空的,然后向我的服务器发送请求,但是当我在设备上运行此代码时,它会显示“应用程序已崩溃”。我正在运行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 

回答

0

你有要求的网络许可清单档案中的?

清单档案中的添加

<uses-permission android:name="android.permission.INTERNET" /> 

应用程序标记

以上请发表您的logcat日志,如果你这样做,你仍然会遇到的错误。

至于说在我的意见,你应该使用的AsyncTask到主线程之外执行此任务。就像这样,但是然后使用正确的变量作用域。这个类应该作为内部类添加到你的RegisterActivity类中。然后,您可以通过启动任务:

new SendRegisterationTask().execute(); 



private class SendRegistrationTask extends AsyncTask<String, String, String> { 

    protected void onPreExecute() { 
     // prepare the request 
    } 

    protected String doInBackground(String... aurl) { 
     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(); 
     } 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // Do what you want after the request has been handled 
    } 
} 

一些在doInBackground东西可以在postExecute或preExecute例如活动推出来完成。

+0

我有清单中的许可,但它仍然不工作。现在更新的问题与logcat的,从开始到应用程序的结束,这是3次:第一个是简单的点击继续,第二个是3个textviews被验证,那么第三个/要/的请求后表演结束,但在可能之前崩溃。 – Qasim 2012-01-08 14:01:15

+0

这有助于很多,哪一行是你的registerActivity类中的第58行?我在这里看不到进口。 – 2012-01-08 14:03:10

+0

第58行是'HttpResponse response = httpclient.execute(httppost);'。感谢您的帮助,非常感谢。以下是完整的RegisterActivity.java:http://pastebin.com/mzxTwma2 – Qasim 2012-01-08 14:08:38

相关问题