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)
答案 0 :(得分:1)
我找到了解决方案。 我没有用android的内置SSL套接字库打开socket
javax.net.SocketFactory fact=SSLSocketFactory.getDefault();
socket=fact.createSocket(addr, port);
这两行代码浪费了我宝贵的四天:(