问题描述
我制作了一个android应用程序,并用于 HttpUrlConnection,POST方法 HoloEverywhere库但我有问题...为什么会发生我不知道的问题...我认为getOutputStream()错误是因为如果用于getOutputStream()方法会显示消息不幸的是,应用程序 * 已停止."并停止了应用程序.
i make a android application and used for HttpUrlConnection, POST method, HoloEverywhere Librarybut i have problem...why happen to problem that i don't know...im think that getOutputStream() error because if used to getOutputStream() method show message " application unfortunately, * has stopped." and stopped application.
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();
}
}
}
Logcat
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()<<有问题...
i think about the getOutputStream() << have a problem...
推荐答案
您正在尝试在UI线程上执行网络操作,而android 3.0+设备不允许.他们将抛出此异常,表明您正在UI线程上执行网络操作. 尝试使用AsyncTask
或单独的Thread
进行网络操作
You are trying to perform network Operation on UI thread, which android 3.0+ devices won't allow. They will throw this exception indicating you are performing network operation on UI thread. Try to use AsyncTask
or Separate Thread
to perform Network Operation
这篇关于HttpUrlConnection getOutputStream有麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!