java HttpUrlConnection响应比较失败
我有一个简单的HttpUrlConnection Post请求。它进入一个php文件,然后查找数据,如果数据不在那里,php文件会回显“N”,如果在那里,php文件会回显数据。。在我的java代码中,在处理数据之前,我会检查响应是否为“N”。但是数据总是被处理的,即使响应是N。。这会导致应用程序崩溃。这是我的代码和logcat错误:请帮助:)
Java代码
new Thread(new Runnable() {
@Override
public void run() {
OutputStream os = null;
HttpURLConnection con = null;
String cRE;
try {
Log.e("DATA", getArti);
//constants
URL url = new URL("http:www.url.com/php/getarticles.php");
JSONObject jsonObject = new JSONObject();
jsonObject.put("req", type + ";" + getArti + extraNetworkData);
String message = jsonObject.toString();
con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(10000);
con.setConnectTimeout(15000);
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setFixedLengthStreamingMode(message.getBytes().length);
//make some HTTP header nicety
con.setRequestProperty("Content-Type", "application/json;charset=utf-8");
con.setRequestProperty("X-Requested-With", "XMLHttpRequest");
//open
con.connect();
//setup send
os = new BufferedOutputStream(con.getOutputStream());
os.write(message.getBytes());
//clean up
os.flush();
int responseCode = con.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
Log.e("RESPONSE STATUS", "ITS HTTP_OK");
String line;
InputStream iStream = con.getInputStream();
BufferedReader brr = new BufferedReader(new InputStreamReader(iStream));
brr.mark(0);
while ((line = brr.readLine()) != null) {
cRE = line;
Log.e("Res", cRE); // RESPONSE
if (type.equals("FT")) {
String data[] = cRE.split("-");
title = data[0];
image = data[1];
id = data[2];
dateNumber = data[3];
} else if (type.equals("R")) { // R = REFRESH DATA
if(cRE.equals("N")) {
Log.e("REQUEST DATA RESPONSE", "R Data = NULL");
} else {
String data[] = cRE.split("-");
title = data[0];
image = data[1];
id = data[2];
dateNumber = data[3];
}
} else if(cRE.equals("SC")) { // SC = SCROLL DOWN DATA
if(cRE.equals("N")) {
} else {
String data[] = cRE.split("-");
title = data[0];
image = data[1];
id = data[2];
dateNumber = data[3];
}
}
}
processNetworkData();
} else {
Log.e("RESPONSE ERR", con.getResponseMessage());
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
con.disconnect();
}
}
}).start();
php代码:
mysqli_stmt_bind_result($stmt, $aDate, $id, $title, $image);
$ad = "";
$d = "";
$tit = "";
$ima = "";
if(is_null(mysqli_stmt_fetch($stmt))) {
echo "N";
} else {
/* Fetch value */
while(mysqli_stmt_fetch($stmt)) {
$ad = $ad ."". "$aDate;";
$d = $d ."". "$id;";
$tit = $tit ."". "$title;";
$ima = $ima ."". "$image;";
}
$aDa = substr($ad, 0, -1);
$aId = substr($d, 0, -1);
$aTitle = substr($tit, 0, -1);
$aImage = substr($ima, 0, -1);
echo "$aTitle-$aImage-$aId-$aDa";
/* close statement */
mysqli_stmt_close($stmt);
}
Logcat错误:
11-29 16:17:30.397 17160-31330/com.安卓.appname E/RESPONSE STATUS: ITS HTTP_OK
11-29 16:17:30.398 17160-31330/com.安卓.appname E/Res: N
11-29 16:17:30.399 17160-31330/com.安卓.appname E/AndroidRuntime: FATAL EXCEPTION: Thread-7023
11-29 16:17:30.399 17160-31330/com.安卓.appname E/AndroidRuntime: Process: com.安卓.appname, PID: 17160
11-29 16:17:30.399 17160-31330/com.安卓.appname E/AndroidRuntime: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
11-29 16:17:30.399 17160-31330/com.安卓.appname E/AndroidRuntime: at com.安卓.appname.Pages.Articles.AllArticlesFragment$3.run(AllArticlesFragment.java:302)
11-29 16:17:30.399 17160-31330/com.安卓.appname E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818)
# 1 楼答案
该框架可以为您处理"emptyNess"
当你可以配置视图层来处理“空”时,为什么还要参与边缘案例和所有这些应用程序级逻辑呢
你可能会认为这是一种普遍的模式
example of above
考虑一下PHP重构
在我看来,如果服务器返回json对象,其中边缘大小写(空)将只是一个空json。根“results”中的数组?与在客户端上呈现JSON作为处理http响应的一部分相比,这似乎更容易、更好地分离关注点
如果你免除了客户端使用低级别http的责任,测试就会更容易、更干净。响应并从中创建JSON
如果您这样做,那么您的客户端代码可以更短/更简单。ie在android中,您可以根据PHP返回的Json构建视图