new Thread() {
@Override
public void run() {
Looper.prepare();
Date date = new Date();
String timeNow = YYMMdd.timestr(date);
String mystr = MD5utils.MD5(timeNow);
String urlPath = "http://" + address + "/service/AfarPosService.svc/Sale/salelist/sa/" + mystr;
URL url;
try {
url = new URL(urlPath);
JSONObject ClientKey = new JSONObject();
JSONArray jsonArray = new JSONArray(liushuis);
ClientKey.put("data", jsonArray);
ClientKey.put("c_cardtype", c_cardtype);
ClientKey.put("message", "");
ClientKey.put("content", "");
contentLiuShui = String.valueOf(ClientKey);
Log.e("发送实时流水contentLiuShui", contentLiuShui + "");
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(20000);
conn.setReadTimeout(20000);
conn.setChunkedStreamingMode(0);
conn.setDoInput(true);// 设置是否从httpUrlConnection读入,默认情况下是true;
conn.setDoOutput(true);//设置是否向httpUrlConnection输出,post请求,参数要放在http正文内,因此需要设为true, 默认情况下是false;
conn.setUseCaches(false);//设置是否用缓存 Post 请求不能使用缓存 ,设置为false
conn.setRequestMethod("POST");//设定请求的方法为"POST",默认是GET
conn.setRequestProperty("ser-Agent", "Fiddler");
conn.setRequestProperty("Content-Type", "application/json");
OutputStream os = conn.getOutputStream();//此处getOutputStream会隐含的进行comm.connect();
Log.e("os", os + "");
os.write(contentLiuShui.getBytes());
os.close();
int code = conn.getResponseCode();
Log.e("发送实时流水code", code + "");
if (code == 200) {
InputStream is = conn.getInputStream();
String json = NetUtils.readString(is);
Log.e("返回实时流水json", json + "");
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(json);
String panduan = (String) jsonObject.get("message");
String yichang = (String) jsonObject.get("content");
if (panduan.equals("0")) {
} else if (panduan.equals("4") || panduan.equals("7")) {
} else {
Toast.makeText(PaymentMoneyActivity1.this, yichang, Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(PaymentMoneyActivity1.this, "与服务器连接失败", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(PaymentMoneyActivity1.this, "网络连接超时,请检查网络", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} finally {
Toast.makeText(PaymentMoneyActivity1.this, "网络连接超时,zouazheloosg", Toast.LENGTH_SHORT).show();
if (conn != null) {
conn.disconnect();
Log.e("断开了", "断开了耶!");
}
}
Looper.loop();
}
}.start();
现存问题:
第一次进行网络请求的时候(时间A,数据data1),如果网络不好超时了,再次接着请求该接口(时间B,数据data2),服务器会收到两次请求的数据:时间A的data1和时间B的data2!
注意:如果多次超时请求,最终后台服务器会接收到多次不同的data!
本人技术小白,还请大神能够指点出错误和解决的办法!
求加QQ在线讨论,在线等解救QQ:379077640