当前位置: 首页 > 面试题库 >

ByetHost服务器使用JSON字符串传递html值“检查浏览器”

娄弘
2023-03-14
问题内容

尝试将json字符串解析为android时,会传递HTML值。一天前一切正常,突然间,尝试通过php文件获取数据库时,我的应用突然崩溃了。

当我检查注意到html值时。请参阅logcat

08-10 01:09:55.814: E/result(6744): <html><body><h2>Checking your browser..<h2><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("7965e114a1dccaf35af3756261f75ad8");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";location.href="http://realroom.byethost24.com/medical/stokist.php?ckattempt=1";</script></body></html>
08-10 01:09:55.814: E/JSON Parser(6744): Error parsing data org.json.JSONException: Value <html><body><h2>Checking of type java.lang.String cannot be converted to JSONObject
08-10 01:09:55.816: E/AndroidRuntime(6744): FATAL EXCEPTION: AsyncTask #1
08-10 01:09:55.816: E/AndroidRuntime(6744): Process: com.example.medionline, PID: 6744
08-10 01:09:55.816: E/AndroidRuntime(6744): java.lang.RuntimeException: An error occured while executing doInBackground()
08-10 01:09:55.816: E/AndroidRuntime(6744):     at android.os.AsyncTask$3.done(AsyncTask.java:304)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at java.lang.Thread.run(Thread.java:818)
08-10 01:09:55.816: E/AndroidRuntime(6744): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
08-10 01:09:55.816: E/AndroidRuntime(6744):     at com.example.medionline.All_supplier_list$FetchMFG.doInBackground(All_supplier_list.java:182)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at com.example.medionline.All_supplier_list$FetchMFG.doInBackground(All_supplier_list.java:1)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at android.os.AsyncTask$2.call(AsyncTask.java:292)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-10 01:09:55.816: E/AndroidRuntime(6744):     ... 4 more

我在byethost服务器上托管的所有页面都发生了这种情况,而在其他服务器上托管的页面运行良好。我尝试将我的文件之一移动到其他服务器,并在该文件上返回正确的json字符串。

另外,当我在浏览器中检查url时,它会返回正确的json字符串,且没有byethost的任何异常或错误。

这是我的JSONfunction类

package com.example.medionline;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;    
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONfunctions
{

     static InputStream is = null;
 static String result = "";
 static JSONObject jArray = null;
public static JSONObject getJSONfromURL(String url) 
{
    // Download JSON data from URL
    try 
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        httppost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240 ");
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } 
    catch (Exception e) 
    {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }

    try 
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) 
        {
            Log.e("jsonnnnnnn", line);
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
        Log.e("result", result);
    } 
    catch (Exception e) 
    {
        Log.e("log_tag", "Error converting result " + e.toString());
    }
    try 
    {
        jArray = new JSONObject(result);
    } 
    catch (JSONException e)
    {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
    return jArray;
}

public static JSONObject makeHttpRequest(String loginUrl, String post, List<NameValuePair> para)
{
    try 
    {
        if(post == "POST")
        {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(loginUrl);
            httpPost.setEntity(new UrlEncodedFormEntity(para));
            httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240 ");
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        }
        else if(post == "GET")
        {
            HttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(para, "utf-8");
            loginUrl += "?" + paramString;
            HttpGet httpGet = new HttpGet(loginUrl);
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }
    }
    catch (UnsupportedEncodingException e)
    {
        e.printStackTrace();
    }
    catch (ClientProtocolException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    try 
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        if (is != null) 
        {
            while ((line = reader.readLine()) != null) 
            {
                //Log.e("jsonnnnnnn", line);
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
            Log.e("result", result);
        }
    }
    catch (Exception e)
    {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }
    try
    {
        jArray = new JSONObject(result);
    }
    catch (JSONException e)
    {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
    return jArray;
}
}

这是我的PHP文件

<?php
include('config.php');
date_default_timezone_set("Asia/Calcutta");

$result1 = mysqli_query($con,"SELECT  *
                                    FROM  `pj_medionline_mst_stockist` 
                                    ORDER BY  `pj_medionline_mst_stockist`.`ID` ASC ");


$response = array();
$posts = array();
while($row=mysqli_fetch_array($result1))
{
    $id =$row["ID"];
    $stkcode =$row["stkcode"];
    $comName =$row["ComName"];
    $operatorid =$row["operatorid"];
    $password =$row["Password"];


    $posts[] = array('id'=>$id, 'stkcode'=>$stkcode, 'stkname'=>$comName, 'operatorid'=>$operatorid, 'password'=>$password);
}

$response['stokist'] = $posts;
print(json_encode($response));
?>

问题答案:

我应该添加以下内容作为答案,因为这是绕过上述问题的解决方法。发布可能会帮助某人:

这个问题可以通过简单地编写exit()来解决。在最后一个可执行语句的php文件中。这将退出php文件,并且不会附加文本。例如。

<?php
include('config.php');
date_default_timezone_set("Asia/Calcutta");

$result1 = mysqli_query($con,"SELECT  *
                                FROM  `pj_medionline_mst_stockist` 
                                ORDER BY  `pj_medionline_mst_stockist`.`ID` ASC ");


$response = array();
$posts = array();
while($row=mysqli_fetch_array($result1))
{
    $id =$row["ID"];
    $stkcode =$row["stkcode"];
    $comName =$row["ComName"];
    $operatorid =$row["operatorid"];
    $password =$row["Password"];


    $posts[] = array('id'=>$id, 'stkcode'=>$stkcode, 'stkname'=>$comName, 'operatorid'=>$operatorid, 'password'=>$password);
}

$response['stokist'] = $posts;
print(json_encode($response));
exit();
?>


 类似资料:
  • 首先,我要说的是,我正在学习如何使用jquery/javascript编程,并且在使用JSON时遇到了一个问题。parse()。我了解格式,以及人们为什么使用它。。。但是在我的任何代码项目中都无法使用它。 我在书上/网上读过关于如何使用它的内容,但我觉得我读得太多了。我现在很困惑,并且在猜测我对它的了解。 话虽如此,我正在学习的jquery/javascript类要求我通过AJAX使用MAMP/l

  • 问题内容: 我见过的大多数实现都是在客户端进行浏览器检测。我只是想知道是否有可能在将任何资源发送到客户端之前进行浏览器检测。 谢谢。 问题答案: 那应该为您工作。只需将其放在响应处理程序中即可。

  • 问题内容: 我正在将javascript / php / ajax应用程序转换为使用jQuery,以确保与Firefox以外的浏览器兼容。 我在使用jQuery的ajax函数传递true,false和null值时遇到麻烦。 JavaScript代码: PHP代码: 服务器输出: 问题在于null,true和false值正在转换为字符串。 当前使用的Javascript AJAX代码正确传递了nul

  • 我对递归非常陌生(我需要使用它),并且在使用我的搜索方法时遇到了一些严重的逻辑问题。请看下面: 输出是长度为1的“nodeOne null,返回null节点数据”打印。我不明白最后一个else if语句中的递归语句是如何到达的,而第一个if语句中的null语句似乎也在到达。 我尝试重新排列if语句,以便是第一个,但这给了我一个。不知道我做错了什么。特别是因为我可以使用方法打印节点中的数据,所以我知

  • 我试图将JSON作为字符串(而不是对象)发送到服务器(在本例中,它是一个WebAPI)。我总是得到一个错误代码500。 当请求被获取时,我成功地从服务器获得响应,并且没有向服务器发送数据。这是通过JsonObjectRequest实现的。 现在,我尝试发送一个以JSON作为字符串的POST请求。为此,我尽力了 JSONObject请求 StringRequest GsonRequest JsonR

  • 我应该编写一个程序,它接受表示整数字符串作为输入,如果每个字符都是数字0-9,则输出是。 我已经回到我的章节阅读和谷歌,但仍然有问题。我知道我的代码一团糟,但我迷路了。我可能会有一些正确的或错误的东西,但这就是我所拥有的。 我哪里错了??