2

我制作了一个 android 应用程序并用于HttpUrlConnection、POST 方法、 HoloEverywhere 库,但我有问题......为什么会发生我不知道的问题......我认为 getOutputStream() 错误,因为如果用于 getOutputStream() 方法显示消息“应用程序不幸,*已停止。” 并停止申请。

main.java

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;

public class Main extends SherlockActivity {
    EditText ID, PS;
    String myId, myPs, myResult;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViewById(R.id.login_button).setOnClickListener(mClickListener);
    }

    Button.OnClickListener mClickListener = new Button.OnClickListener() {
        public void onClick(View v) {
            ID = (EditText)findViewById(R.id.id_text);
            PS = (EditText)findViewById(R.id.ps_text);
            myId = ID.getText().toString();
            myPs = PS.getText().toString();
            HttpPostData();
        }
    };

    public void HttpPostData() {
        try {
            URL communion_login = new URL("http://communion.gntc.net/Default.aspx");
            HttpURLConnection connection = (HttpURLConnection) communion_login.openConnection();
            connection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            connection.setRequestMethod("POST");

            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            connection.setDefaultUseCaches(false);

            StringBuffer buffer = new StringBuffer(); 
            buffer.append("LoginView1$txtID").append("=").append(myId).append("&");
            buffer.append("LoginView1$txtPW").append("=").append(myPs);

            OutputStream opstrm = new BufferedOutputStream(connection.getOutputStream());
            opstrm.write(buffer.toString().getBytes());
            opstrm.flush(); 
            opstrm.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

日志猫

05-01 02:15:20.618: D/AndroidRuntime(5632): Shutting down VM
05-01 02:15:20.618: W/dalvikvm(5632): threadid=1: thread exiting with uncaught exception (group=0x416dc2a0)
05-01 02:15:20.633: E/AndroidRuntime(5632): FATAL EXCEPTION: main
05-01 02:15:20.633: E/AndroidRuntime(5632): android.os.NetworkOnMainThreadException
05-01 02:15:20.633: E/AndroidRuntime(5632):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at com.nathaniel.gntc_communion.Main.HttpPostData(Main.java:67)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at com.nathaniel.gntc_communion.Main$1.onClick(Main.java:40)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at android.view.View.performClick(View.java:4261)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at android.view.View$PerformClick.run(View.java:17356)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at android.os.Handler.handleCallback(Handler.java:615)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at android.os.Looper.loop(Looper.java:137)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at android.app.ActivityThread.main(ActivityThread.java:4921)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at java.lang.reflect.Method.invokeNative(Native Method)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at java.lang.reflect.Method.invoke(Method.java:511)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
05-01 02:15:20.633: E/AndroidRuntime(5632):     at dalvik.system.NativeStart.main(Native Method)

我想 getOutputStream() << 有问题...

4

1 回答 1

6

android.os.NetworkOnMainThreadException

您正在尝试在 UI 线程上执行网络操作,这是 android 3.0+ 设备不允许的。他们将抛出此异常,表明您正在 UI 线程上执行网络操作。尝试使用AsyncTask或分离Thread进行网络操作

于 2013-04-30T17:43:10.593 回答