gpt4 book ai didi

java - 使用 HttpsURLConnection 的 InputStream 读取 xml 数据时出现意外的流结束错误

转载 作者:行者123 更新时间:2023-11-30 02:20:53 29 4
gpt4 key购买 nike

public class MainActivity extends Activity 
{
Button submit;
EditText textBox;

String xmlRequest = "<Request><Merchant_Number>111111111111</Merchant_Number><Terminal_ID>001</Terminal_ID><Action_Code>05</Action_Code><Trans_Type>N</Trans_Type><POS_Entry_Mode>S</POS_Entry_Mode><Track_Data2>71019520xxxxxx=</Track_Data2></Request>";

String domain = "www.somedomain.com/gateway.php";
String domainForSocket = "https://" + domain;
URL a;
String sucess = "sucess";
HttpsURLConnection urlConnection;
String header;
String writeData; // data which will be sent to server

byte[] outputInBytes;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (Button) findViewById(R.id.button1);
textBox = (EditText) findViewById(R.id.editText1);

writeData = xmlRequest;
textBox.setText(writeData);

// creating thread to run on background for network connection

final Thread mThread = new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub

try {
a = new URL(domainForSocket);

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {

outputInBytes = xmlRequest.getBytes("UTF-8");

urlConnection = (HttpsURLConnection) a.openConnection();
urlConnection
.setFixedLengthStreamingMode(outputInBytes.length);
urlConnection.setRequestMethod("POST");

urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");

urlConnection.setRequestProperty("Content-Length", ""
+ outputInBytes.length);
urlConnection.setRequestProperty("Accept-Encoding", "");

urlConnection.setRequestProperty("Content-Language",
"en-US");

urlConnection.setUseCaches(false);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
// urlConnection.setDoInput(true);
urlConnection.setInstanceFollowRedirects(false);

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

// Log.v(sucess, "connectio open");

try {

OutputStream out = urlConnection.getOutputStream();

out.write(outputInBytes, 0, outputInBytes.length);
out.close();

// Send request

Log.v(sucess, "output sent");
} catch (Exception e) {
e.printStackTrace();
Log.e("Output Error", "error in sending data to server");
}

try {

InputStream a = urlConnection.getInputStream();
InputStreamReader read = new InputStreamReader(a);

BufferedReader br = new BufferedReader(read);

String data = "";
// Log.v("code execute", "i am here");
StringBuilder total = new StringBuilder();
StringBuffer ab = new StringBuffer();
Log.v("result", "if you dont see then no data");

while ((data = br.readLine()) != null) {
total.append(data);
Log.v("data", data.toString());
}

a.close(); // closing input string


} catch (Exception e) {
e.printStackTrace();
Log.e("Data Receiving Error",
"Error in receiving dat from server");
}

finally {
// urlConnection.disconnect();
try {

// mSocket.close();
} catch (Exception e) {
e.printStackTrace();
Log.e("Socket close Error", "cant close socket");
}
}

// sending output

}
});

submit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

try {

mThread.start();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

});



}

以上是我的 XML HTTP 请求的完整源代码。我从服务器获得 XML 响应。但是每当我向服务器发送请求时,我都会在 logcat 中收到以下错误。谁能帮我解决这个问题。我们将不胜感激。

这是日志:

02-17 11:09:33.684: W/System.err(6446): java.net.ProtocolException: unexpected end of stream
02-17 11:09:33.689: W/System.err(6446): at com.android.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:445)
02-17 11:09:33.690: W/System.err(6446): at com.android.okio.RealBufferedSource$1.read(RealBufferedSource.java:168)

最佳答案

我找到了解决方案。我没有使用 android 的内置 SSL 套接字库打开套接字

javax.net.SocketFactory fact=SSLSocketFactory.getDefault();
socket=fact.createSocket(addr, port);

这两行代码浪费了我宝贵的四天:(

关于java - 使用 HttpsURLConnection 的 InputStream 读取 xml 数据时出现意外的流结束错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28560981/

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