所以我在我的localhost上测试了我的Android应用程序,没有任何问题。现在我正在尝试上传我的在线网站上的php文件以及我在localhost上使用的数据库。 我已经阅读了以下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
这是我的安卓代码
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 当我得到回复时?
答案 0 :(得分:0)
输入服务器代码Top
ob_start("ob_tidyhandler");
或将其放在您的代码上
header("Content-type: application/json");
echo json_encode($response);
答案 1 :(得分:0)
好吧,我切换到另一个免费托管网站,现在我的php工作,我收到正确答案:
09-08 21:24:01.452 23578-24362/com.example.meer.bustedtracking I/Response Code: ... 200
09-08 21:24:01.452 23578-24362/com.example.meer.bustedtracking I/Response Code: Headers-------start-----
09-08 21:24:01.452 23578-24362/com.example.meer.bustedtracking I/System.out: Server:Apache/2.2.29 (Unix) mod_ssl/2.2.29 OpenSSL/0.9.8e-fips-rhel5 mod_mono/2.6.3 mod_jk/1.2.37 mod_bwlimited/1.4 mod_wsgi/4.4.13 Python/2.4.3
09-08 21:24:01.452 23578-24362/com.example.meer.bustedtracking I/System.out: X-Powered-By:PHP/5.4.41
09-08 21:24:01.452 23578-24362/com.example.meer.bustedtracking I/System.out: Expires:Thu, 19 Nov 1981 08:52:00 GMT
09-08 21:24:01.452 23578-24362/com.example.meer.bustedtracking I/System.out: Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
09-08 21:24:01.453 23578-24362/com.example.meer.bustedtracking I/System.out: Pragma:no-cache
09-08 21:24:01.453 23578-24362/com.example.meer.bustedtracking I/System.out: Set-Cookie:PHPSESSID=c0d87c4d1218264413d57183a4966009; path=/
09-08 21:24:01.453 23578-24362/com.example.meer.bustedtracking I/System.out: Keep-Alive:timeout=5, max=100
09-08 21:24:01.453 23578-24362/com.example.meer.bustedtracking I/System.out: Connection:Keep-Alive
09-08 21:24:01.453 23578-24362/com.example.meer.bustedtracking I/System.out: Transfer-Encoding:chunked
09-08 21:24:01.453 23578-24362/com.example.meer.bustedtracking I/System.out: Content-Type:text/html
09-08 21:24:01.453 23578-24362/com.example.meer.bustedtracking I/System.out: X-Android-Selected-Protocol:http/1.1
09-08 21:24:01.454 23578-24362/com.example.meer.bustedtracking I/System.out: X-Android-Sent-Millis:1473341040653
09-08 21:24:01.454 23578-24362/com.example.meer.bustedtracking I/System.out: X-Android-Received-Millis:1473341041452
09-08 21:24:01.455 23578-24362/com.example.meer.bustedtracking I/System.out: X-Android-Response-Source:NETWORK 200
09-08 21:24:01.455 23578-24362/com.example.meer.bustedtracking I/Response Code: Headers-------end-----
09-08 21:24:01.456 23578-24362/com.example.meer.bustedtracking I/Before_parsing:: {"success":1,"message":"logged in successfully","user_id":6}
09-08 21:24:01.456 23578-24362/com.example.meer.bustedtracking I/log_tag: JSONObject: {"success":1,"message":"logged in successfully","user_id":6}
09-08 21:24:01.457 23578-24362/com.example.meer.bustedtracking I/log_tag:: successResponse: 1
09-08 21:24:01.458 23578-24362/com.example.meer.bustedtracking I/log_tag:: received user id 6
09-08 21:24:01.458 23578-24362/com.example.meer.bustedtracking I/log_tag: returnString: logged in successfully
我的PHP代码仍然相同。 所以托管服务正在发生一些导致不必要的响应的事情。如果有人知道为什么请告诉我。