gpt4 book ai didi

javascript - Android在线服务器接收JavaScript响应而不是Json?

转载 作者:行者123 更新时间:2023-11-28 05:35:41 26 4
gpt4 key购买 nike

所以我已经在本地主机上测试了我的 Android 应用程序,没有任何问题。现在我尝试将 php 文件以及我在本地主机上使用的数据库上传到我的在线网站上。我已经阅读了以下内容 Cookies sent by server但我没有看到发送任何 cookie。

我可以看到以下内容

09-08 12:33:33.637 4555-5690/com.example.meer.bustedtracking I/Response Code:  ... 200
09-08 12:33:33.637 4555-5690/com.example.meer.bustedtracking I/Response Code: Headers-------start-----
09-08 12:33:33.637 4555-5690/com.example.meer.bustedtracking I/System.out: Date:Thu, 08 Sep 2016 04:34:47 GMT
09-08 12:33:33.637 4555-5690/com.example.meer.bustedtracking I/System.out: Content-Type:text/html
09-08 12:33:33.637 4555-5690/com.example.meer.bustedtracking I/System.out: Transfer-Encoding:chunked
09-08 12:33:33.637 4555-5690/com.example.meer.bustedtracking I/System.out: Connection:keep-alive
09-08 12:33:33.637 4555-5690/com.example.meer.bustedtracking I/System.out: Vary:Accept-Encoding
09-08 12:33:33.638 4555-5690/com.example.meer.bustedtracking I/System.out: Expires:Thu, 01 Jan 1970 00:00:01 GMT
09-08 12:33:33.638 4555-5690/com.example.meer.bustedtracking I/System.out: Cache-Control:no-cache
09-08 12:33:33.638 4555-5690/com.example.meer.bustedtracking I/System.out: X-Android-Selected-Protocol:http/1.1
09-08 12:33:33.638 4555-5690/com.example.meer.bustedtracking I/System.out: X-Android-Sent-Millis:1473309212446
09-08 12:33:33.638 4555-5690/com.example.meer.bustedtracking I/System.out: X-Android-Received-Millis:1473309213636
09-08 12:33:33.638 4555-5690/com.example.meer.bustedtracking I/System.out: X-Android-Response-Source:NETWORK 200
09-08 12:33:33.638 4555-5690/com.example.meer.bustedtracking I/Response Code: Headers-------end-----
09-08 12:33:33.640 4555-5690/com.example.meer.bustedtracking I/Before_parsing:: <html><body><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("7c37be3e9d2b34968579877c85060bcb");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://busted.byethost16.com/Andriod/login_with_route.php?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>

然后我收到此错误

 Error parsing data org.json.JSONException: Value <html><body><script of type java.lang.String cannot be converted to JSONObject

这是我的 Android 代码

        if (methodType.equals("login")) {
try {
String user_name = params[1];
String password = params[2];
routeType = params[3];
String lat = params[4];
String lng = params[5];


URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(15000);
httpURLConnection.setRequestMethod("POST");
// httpURLConnection.setInstanceFollowRedirects(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&"
+ URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8") + "&"
+ URLEncoder.encode("type", "UTF-8") + "=" + URLEncoder.encode(routeType, "UTF-8") + "&"
+ URLEncoder.encode("lat", "UTF-8") + "=" + URLEncoder.encode(lat, "UTF-8") + "&"
+ URLEncoder.encode("lng", "UTF-8") + "=" + URLEncoder.encode(lng, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();

// normally, 3xx is redirect
int status = httpURLConnection.getResponseCode();
Log.i("Response Code"," ... " + status);

String key;
Log.i("Response Code","Headers-------start-----");
for (int i = 1; (key = httpURLConnection.getHeaderFieldKey(i)) != null; i++) {
System.out.println(key + ":" + httpURLConnection.getHeaderField(i));
}
Log.i("Response Code","Headers-------end-----");


InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));


String result = "";
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
sb.append(line + "\n");
}

bufferedReader.close();
result = sb.toString();
Log.i("Before_parsing:", result);

// Parsing JSON data
try {
JSONObject object = new JSONObject(result);
Log.i("log_tag", "JSONObject: " + object);
returnString = object.getString("message");
successResponse = object.optInt(TAG_SUCCESS);
Log.i("log_tag:", "successResponse: " + successResponse);
if (successResponse != null && successResponse == 1) {//logged in successfully "
backgroundProcessResult = true;
userId = object.getString("user_id"); //get the user id from the database (so we use it to log out)
Log.i("log_tag:", "received user id " + userId);
}
else{
backgroundProcessResult = false;
Log.i("log_tag:", "something went wrong, ");
}

} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
inputStream.close();
httpURLConnection.disconnect();
Log.i("log_tag", "returnString: " + returnString);


} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

我的在线服务器上的 PHP:

<?php  
require_once('database.php');
// array for JSON response
$response = array();


//check the server method first
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$useremail = null;
$password = null;
//received as string
$unconverted_lat = $_POST['lat'];
$unconverted_lng = $_POST['lng'];
$type =null;


if(empty($_POST["email"]) || empty($_POST["password"]) ) {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);

}
if( empty($_POST["type"]) ){
$response["success"] = 0;
$response["message"] = "Please select a route type";
echo json_encode($response);
}


if(!empty($_POST["email"]) && !empty($_POST["password"])) {
$useremail = $_POST["email"];
$password = $_POST["password"];

$query = $connection->prepare("SELECT `user_id` FROM `users` WHERE `user_email` = ? and `user_password` = PASSWORD(?)");
$query->bind_param("ss", $useremail, $password);
$query->execute();
$query->bind_result($userid);
$query->fetch();
$query->close();

//valid(username and password in db)
if(!empty($userid)) {

$lat = (float)$unconverted_lat;
$lng = (float)$unconverted_lng;
$type =$_POST['type'];

//start session
session_start();
session_regenerate_id(true);
$session_key = session_id();
$query = $connection->prepare("INSERT INTO `sessions` ( `user_id`, `session_key`, `session_address`, `session_useragent`, `session_expires`) VALUES ( ?, ?, ?, ?, DATE_ADD(NOW(),INTERVAL 16 HOUR) );");
$query->bind_param("isss", $userid, $session_key, $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT'] );
$query->execute();
$query->close();

//update the markers table
$query = $connection->prepare("INSERT INTO `markers`(`user_id`, `lat`, `lng`, `type`)
VALUES ( ?, ?, ?, ? )ON DUPLICATE KEY UPDATE lat='$lat', lng='$lng', type='$type'");
$query->bind_param("idds", $userid,$lat, $lng, $type );
$query->execute();
$query->close();

$response["success"] = 1;
$response["message"] = "logged in successfully";
$response["user_id"] = $userid ;
echo json_encode($response);


}
//not valid(email or/and password not in db)
else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "account not found please check login info";

// echoing JSON response
echo json_encode($response);

}
}
}

?>

我正在尝试测试的链接 http://busted.byethost16.com/Andriod/login_with_route.php.side问题,为什么会变成 http://busted.byethost16.com/Andriod/login_with_route.php?i=1当我收到回复时?

最佳答案

输入您的服务器代码顶部

ob_start("ob_tidyhandler");

或者将其放在您的代码中

header("Content-type: application/json");
echo json_encode($response);

关于javascript - Android在线服务器接收JavaScript响应而不是Json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39382765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com