每当我点击移动应用程序上的登录按钮时,应用程序就会强制关闭。控制台上有很多错误..
基本上我要做的是将用户在2个文本框中键入的内容发送到.php页面并获取会话密钥,这将用于让应用知道用户已登录。< / p>
然而,这些错误我不知道如何修复,我不知道为什么它只是强行关闭。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#004f00">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:contentDescription="@string/login_logo_desc"
android:layout_marginBottom="10dp"/>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/username"
android:inputType="text"
android:padding="5dp"
android:textColor="#ffffff"
android:textColorHint="#ffffff"
android:layout_marginBottom="10dp"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:inputType="textPassword"
android:padding="5dp"
android:textColor="#ffffff"
android:textColorHint="#ffffff"
android:layout_marginBottom="10dp"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="@string/button_login"
android:textColor="#ffffff"
android:textColorHint="#ffffff"
android:onClick="login"/>
</LinearLayout>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void login(View v){
//Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://sociobubble.com/m/mobilogin.php");
List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(4);
try {
EditText username = (EditText)findViewById(R.id.username);
String Username = username.getText().toString();
EditText password = (EditText)findViewById(R.id.password);
String Password = password.getText().toString();
// Add your data
nameValuePairs.add(new BasicNameValuePair("username", Username));
nameValuePairs.add(new BasicNameValuePair("password", Password));
nameValuePairs.add(new BasicNameValuePair("client", "mobile"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
Log.i("postData", response.getStatusLine().toString());
} catch (IOException e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
}
07-23 18:03:23.833: E/AndroidRuntime(27330): FATAL EXCEPTION: main
07-23 18:03:23.833: E/AndroidRuntime(27330): java.lang.IllegalStateException: Could not execute method of the activity
07-23 18:03:23.833: E/AndroidRuntime(27330): at android.view.View$1.onClick(View.java:3600)
07-23 18:03:23.833: E/AndroidRuntime(27330): at android.view.View.performClick(View.java:4106)
07-23 18:03:23.833: E/AndroidRuntime(27330): at android.view.View$PerformClick.run(View.java:17052)
07-23 18:03:23.833: E/AndroidRuntime(27330): at android.os.Handler.handleCallback(Handler.java:615)
07-23 18:03:23.833: E/AndroidRuntime(27330): at android.os.Handler.dispatchMessage(Handler.java:92)
07-23 18:03:23.833: E/AndroidRuntime(27330): at android.os.Looper.loop(Looper.java:137)
07-23 18:03:23.833: E/AndroidRuntime(27330): at android.app.ActivityThread.main(ActivityThread.java:5059)
07-23 18:03:23.833: E/AndroidRuntime(27330): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 18:03:23.833: E/AndroidRuntime(27330): at java.lang.reflect.Method.invoke(Method.java:511)
07-23 18:03:23.833: E/AndroidRuntime(27330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
07-23 18:03:23.833: E/AndroidRuntime(27330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
07-23 18:03:23.833: E/AndroidRuntime(27330): at dalvik.system.NativeStart.main(Native Method)
07-23 18:03:23.833: E/AndroidRuntime(27330): Caused by: java.lang.reflect.InvocationTargetException
07-23 18:03:23.833: E/AndroidRuntime(27330): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 18:03:23.833: E/AndroidRuntime(27330): at java.lang.reflect.Method.invoke(Method.java:511)
07-23 18:03:23.833: E/AndroidRuntime(27330): at android.view.View$1.onClick(View.java:3595)
07-23 18:03:23.833: E/AndroidRuntime(27330): ... 11 more
07-23 18:03:23.833: E/AndroidRuntime(27330): Caused by: android.os.NetworkOnMainThreadException
07-23 18:03:23.833: E/AndroidRuntime(27330): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1131)
07-23 18:03:23.833: E/AndroidRuntime(27330): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-23 18:03:23.833: E/AndroidRuntime(27330): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-23 18:03:23.833: E/AndroidRuntime(27330): at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-23 18:03:23.833: E/AndroidRuntime(27330): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-23 18:03:23.833: E/AndroidRuntime(27330): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-23 18:03:23.833: E/AndroidRuntime(27330): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-23 18:03:23.833: E/AndroidRuntime(27330): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-23 18:03:23.833: E/AndroidRuntime(27330): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-23 18:03:23.833: E/AndroidRuntime(27330): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-23 18:03:23.833: E/AndroidRuntime(27330): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-23 18:03:23.833: E/AndroidRuntime(27330): at com.sociobubble.app.MainActivity.login(MainActivity.java:64)
07-23 18:03:23.833: E/AndroidRuntime(27330): ... 14 more
答案 0 :(得分:2)
您无法在主线程上进行网络调用。请阅读异常堆栈跟踪和google for NetworkOnMainThreadException
答案 1 :(得分:0)
你必须asynctask这个典型的东西,那篇文章可能会帮助你 http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html