2016-09-08 5 views
1

私はlocalhost上でAndroidアプリケーションをテストしました。全く問題はありません。私は自分のlocalhost上で使用したデータベースと一緒に私のオンラインWebサイトにphpファイルをアップロードしようとしています。 私はすでに以下を読んでいますCookies sent by server しかし、私は送られたクッキーを見ません。AndroidオンラインサーバーはJsonの代わりにJavaScript応答を受信して​​いますか?

私はその後、私はこのエラー

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のget以下

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> 

を見ることができます:

<?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"); 

+0

サーバーコードまたはjson_encode($ response、true)で返されるjson応答のたびにexit()を挿入します。 –

+0

助けてくれてありがとう、私はexit()を試みた。応答もエラーも変化しません。 json_encode($ response、true)と同じことです。 –

答えて

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コードはまだ同じです。 あなたが私に知らせる理由を知っていれば、望ましくない応答を引き起こしたホスティングサービスで何か起こっていることがあります。

0

プットまたは

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

それは両方の応答を変更しなかった、それはまだ200とContent-Type:text/html –

+0

Webブラウザ上で 'Content-Type:application/json' –

関連する問題