当我从Android应用程序请求数据时,我的网站停止工作

时间:2014-07-11 16:31:07

标签: java mysql apache android-webservice

我创建了一个Android应用程序,它向网站发出一些请求并以JSON格式获取数据,但是当我向特定的PHP文件发出请求时,会发生一些奇怪的事情:整个网站停止工作。网页根本不加载。它就像网站不存在一样,显然是从我的网络提供商那里自动发生的。

在错误日志中,我得到一条说明标题的记录:

[2014年7月11日星期五19:17:26] [错误] [客户端79.103.143.40] ModSecurity:[file" /etc/httpd/crs/activated_rules/modsecurity_crs_21_protocol_anomalies.conf"] [line&line #34; 84"] [id" 960904"] [rev" 2"] [msg"请求包含内容,但缺少内容类型标题"] [严重性"通知"] [ver" OWASP_CRS / 2.2.8"] [成熟度&# 34; 9"] [准确度" 9"]警告。 匹配" rx ^ 0 $"反对" REQUEST_HEADERS:Content-Length"需要。 [主机名" www.MYWEBSITW.eu"] [uri" /webservice/MYPHPFILE.php"] [unique_id" U8AOFn8AAAEAABq-qs8AAAEt"]

这是一个如此严重的错误,我的提供商会在每次请求时停止我的网站吗?

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

  1. 连接到我的数据库
  2. 执行select语句
  3. 回显SQL结果(以便将它们发送到Android设备),如下所示:

       header('Content-type: application/json');
       echo (json_encode(array('notifications'=>$result)));
    
  4. 尝试从网络服务器请求数据时是否有人遇到过这个问题?

2 个答案:

答案 0 :(得分:0)

目前还不完全清楚,因为您没有向我们展示如何在Android上向客户端发送请求,但我认为问题是您没有在请求中设置任何Content-Type使。假设您使用的是HttpPost类,您可以使用:

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

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

或适用于您发送的任何数据的任何内容。

答案 1 :(得分:0)

实际上,根据我的要求,我根本不使用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);
                }