我有簡單的HttpUrlConnection發佈請求。它去一個PHP文件,然後查找數據,如果數據不存在,PHP文件回聲「N」,如果它在那裏的PHP文件回聲數據..而在我的Java代碼中,我使用檢查,看看是否在處理數據之前響應爲「N」。但是,即使N ..中的響應導致應用程序崩潰,數據也始終得到處理。繼承人我的代碼和錯誤的logcat:請幫助:)HttpUrlConnection響應比較失敗
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.android.appname E/RESPONSE STATUS: ITS HTTP_OK
11-29 16:17:30.398 17160-31330/com.android.appname E/Res: N
11-29 16:17:30.399 17160-31330/com.android.appname E/AndroidRuntime: FATAL EXCEPTION: Thread-7023
11-29 16:17:30.399 17160-31330/com.android.appname E/AndroidRuntime: Process: com.android.appname, PID: 17160
11-29 16:17:30.399 17160-31330/com.android.appname E/AndroidRuntime: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
11-29 16:17:30.399 17160-31330/com.android.appname E/AndroidRuntime: at com.android.appname.Pages.Articles.AllArticlesFragment$3.run(AllArticlesFragment.java:302)
11-29 16:17:30.399 17160-31330/com.android.appname E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818)