java - HTTP Post 不工作 JAVA

标签 java android eclipse http

问题

每当我单击移动应用程序上的登录按钮时,该应用程序就会强制关闭。控制台中有大量错误。

基本上我想做的是将用户在两个文本框中输入的内容发送到 .php 页面并获取 session key ,这将用于让应用程序知道用户已登录。

但是,这些错误我不知道如何修复,也不知道为什么它只是强制关闭。

activity_main.xml

<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>

MainActivity.java

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

最佳答案

您无法在主线程上进行网络调用。请阅读异常堆栈跟踪并谷歌搜索 NetworkOnMainThreadException

关于java - HTTP Post 不工作 JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17822542/

相关文章:

java - 方法声明中的泛型类型和对象有什么区别?

java - 如何在不提示用户名/密码的情况下访问 Windows 上的共享文件夹?

java - 更改默认布局时应用程序崩溃说,不幸的是应用程序已停止

java - 使用 Maven 在内部使用 native 库构建 Android 项目

android - 升级到 10.10 后在 Ubuntu 中无法在 Eclipse 中配置 Android SDK

eclipse - Nsight Eclipse Cuda + opencv

java - IBM 沃森 : Unauthorized

android - 如何将cookies与android webview同步?

android - 如何取消应用程序与 Admob 的关联

java - LibGDX - Camera.update() 调用不执行任何操作