gpt4 book ai didi

android - HttpUrlConnection getOutputStream 有问题

转载 作者:太空狗 更新时间:2023-10-29 16:20:30 25 4
gpt4 key购买 nike

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

ma​​in.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() << 有问题...

最佳答案

android.os.NetworkOnMainThreadException

您正在尝试在 UI 线程上执行网络操作,Android 3.0+ 设备不允许这样做。他们将抛出此异常,表明您正在 UI 线程上执行网络操作。 尝试使用AsyncTask 或单独的Thread 来执行网络操作

关于android - HttpUrlConnection getOutputStream 有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16305836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com