簡體   English   中英

當我從Android應用程序請求數據時,我的網站停止工作

[英]My website stops working when I request data from my Android application

我創建了一個Android應用程序,該應用程序向網站發出一些請求並以JSON格式獲取數據,但是當我向特定的PHP文件發出請求時,發生了奇怪的事情:整個網站停止工作。 該網頁根本無法加載。 好像該網站不存在,很明顯這是從我的Internet提供商自動發生的。

在錯誤日志中,我得到一條記錄,該記錄中包含有關標題的內容:

[2014年7月11日,星期五,19:17:26] [錯誤] [客戶端79.103.143.40] ModSecurity:[文件“ /etc/httpd/crs/activated_rules/modsecurity_crs_21_protocol_anomalies.conf”] [第“ 84”行[id“ 960904” ] [rev“ 2”] [msg“請求包含內容,但缺少內容類型標頭”] [嚴重性“ NOTICE”] [ver“ OWASP_CRS / 2.2.8”] [成熟度“ 9”] [准確性“ 9”]警告。 必須將“ rx ^ 0 $”與“ REQUEST_HEADERS:Content-Length”相匹配。 [主機名“ www.MYWEBSITW.eu”] [uri“ /webservice/MYPHPFILE.php”] [unique_id“ U8AOFn8AAAEAABq-qs8AAAEt”]

這么嚴重的錯誤使我的提供商在每次請求時都會停止我的網站嗎?

在我的php文件中,我要做的是:

  1. 連接到我的數據庫
  2. 執行選擇語句
  3. 回顯SQL結果(以便將其發送到Android設備),如下所示:

      header('Content-type: application/json'); echo (json_encode(array('notifications'=>$result))); 

嘗試從Web服務器請求數據時,有人遇到過這個問題嗎?

尚不完全清楚,因為您尚未向我們展示如何在Android上從客戶端發送請求,但是我認為問題在於您沒有對發出的請求設置任何Content-Type 假設您正在使用HttpPost類,則需要使用:

httpPost.addHeader("Content-Type", "text/plain");

要么

httpPost.addHeader("Content-Type", "application/json");

或適合於您要發送的任何數據。

實際上,我根本不根據請求使用addHeader,我只使用request.setHeader(“ json”,json.toString()); 您認為這是問題嗎? 進入我的doInBackground函數,我有該代碼:

            JSONObject json = new JSONObject();

            json.put("action", "get");
            json.put("serial", mSerial);
            json.put("appId", AppConstants.APP_ID);
            json.put("dateFrom", MainApplication.dbHelper.getNotificationLastTime());
            Calendar now = Calendar.getInstance();
            json.put("currentDate",sdfyyyyMMdd.format(now.getTime()));

            Log.i(TAG, "GetNotificationTask request = " + json.toString());

            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_MILLISEC);
            HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
            HttpClient client = new DefaultHttpClient(httpParams);

            HttpPost request = new HttpPost(AppConstants.URL_NOTICATION);

            request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF-8")));
            request.setHeader("json", json.toString());

            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();


            if (entity != null) {
                InputStream instream = entity.getContent();

                String result = RestClient.convertStreamToString(instream);
                Log.i(TAG, "GetNotificationTask response = " + result);
                JSONObject jsonRes = new JSONObject(result);
                JSONObject notifications = jsonRes.getJSONObject("notifications");

                String success = notifications.getString("success");
                if (success.equals("1")) {
                    JSONArray messages = notifications.getJSONArray("0");

                    Log.i(TAG, "Notification messages count = " + messages.length());
                    String message = "";
                    ArrayList<NotificationData> msgArray = new ArrayList<NotificationData>();
                    if (messages.length() == 0)
                        return null;

                    for (int i = 0; i < messages.length(); i++) {
                        NotificationData info = new NotificationData();

                        JSONObject object = messages.getJSONObject(i);
                        info.setId(object.getString("msgId"));
                        info.setSerial(object.getString("fkmsgSerial"));
                        info.setTitle(object.getString("msgTitle"));
                        info.setMessage(object.getString("msgMessage"));
                        info.setDate(object.getString("msgDate"));
                        info.setImage(object.getString("msgImage"));
                        msgArray.add(info);

                        if (i == 0)
                            message = info.getMessage();
                        else
                            message = "\n" + info.getMessage();
                    }

                    MainApplication.dbHelper.insertMessages(msgArray);

                    String title = "";
                    if (messages.length() == 1)
                        title = "1 new notificiation";
                    else
                        title = messages.length() + " new notificiations";

                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    intent.putExtra("SHOW_MESSAGES", true);

                    PendingIntent pIntent = PendingIntent.getActivity(
                            getApplicationContext(), 0, intent,
                            PendingIntent.FLAG_UPDATE_CURRENT);
                    Notification noti = new NotificationCompat.Builder(getApplicationContext())
                        .setContentTitle(title)
                        .setContentText(message)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setSound(soundUri)
                        .setContentIntent(pIntent).getNotification();

                    noti.flags |= Notification.FLAG_AUTO_CANCEL;

                    NotificationManager notificationManager = (NotificationManager) getApplicationContext()
                            .getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.notify(R.string.alarm_service_started, noti);
                }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM