2016-08-10 10 views
0

私はJSONObjectを私のPHPスクリプトからアンドロイドに取得しようとしています。すべてうまくいっていますが、ユーザーがスペースで姓または名字を入力するとアプリがクラッシュします。スペースでJsonObjectRequestを取得できません

exmple: JACK //そのOK ジュニアスミスは//これは私のPHPスクリプトです

をつぶす:

<?php 
require('connection.php'); 

$phone = $_GET['phone']; 
$password = $_GET['password']; 

$sql = ("select * from Users where phone = '$phone' and password = '$password' "); 

$check = mysqli_fetch_array(mysqli_query($con,$sql)); 

if(isset($check)){ 
    $hasApartment = mysqli_query($con,$sql); 
    $row = mysqli_fetch_assoc($hasApartment); 
    $apartmentNumber = $row['apartmentNum']; 
     $firstName = $row['firstName']; 
     $lastName = $row['lastname']; 

     $image = $row['image']; 

    if($apartmentNumber > 0){ 
    echo '{"query_result":"SUCCESS", "apartmentNumber":' . $apartmentNumber.' , "firstName":' . trim($firstName) .' ,"lastName":' . trim($lastName).', "image":"' . $image.'"}'; 

     } 
else{ 
echo '{"query_result":"noAparetemet"}'; 
} 
} 
else{ 
    echo '{"query_result":"FAILURE"}'; 
} 

mysqli_close($con); 

?> 

これは私のアンドロイドです:

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(final JSONObject response) { 

        try { 
         String query_result = response.getString("query_result"); 
         switch (query_result){ 
          case "SUCCESS": // user name and password exists and mathces and user associated with apartment 
           SharedPreferences.Editor editor = preferences.edit(); 
           RoomateModel roomateModel = new RoomateModel(response); 
           Gson gson = new Gson(); 
           String json = gson.toJson(roomateModel); 
           editor.putString("USER", json); 
           editor.commit(); 
           Intent intent = new Intent(LoginActivityScreen.this, HomeActivityScreen.class); 
           editor.putBoolean("loggedIn", true); 
           editor.apply(); 
           startActivity(intent); 
           break; 

          case "FAILURE": //user name or password are incorrect 
           loginLayout.setVisibility(View.VISIBLE); 
           logging.setVisibility(View.GONE); 
           Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show(); 
           break; 
          default: 
           break; 
         } 

         } catch (JSONException e) { //this exception is caught 
         e.printStackTrace(); 
         loginLayout.setVisibility(View.VISIBLE); 
         logging.setVisibility(View.GONE); 
         Log.e("e" , e.toString()+e.getMessage()); 
        } 
       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      loginLayout.setVisibility(View.VISIBLE); 
      logging.setVisibility(View.GONE); 
      Log.e("e" , error.toString()+error.getMessage()); 
     } 

    }); 
    MySingleton.getInstance(context).addToRquestQueue(jsonObjectRequest); 
} 

これが私ですログ:

com.android.volley.ParseError: org.json.JSONException: Unterminated object at character 85 of 
{"query_result":"SUCCESS", "apartmentNumber":63 , "firstName":JACK¨ ,"lastName":junior smith, "image":"url.jpeg"} 

答えて

1

問題は、JSONレスポンスの名前と姓に引用符がないことです。あなたは

"lastName": "junior smith" 

代わりの

echo '{"query_result":"SUCCESS", "apartmentNumber":' . $apartmentNumber.' , "firstName":"' . trim($firstName) .'" ,"lastName":"' . trim($lastName).'", "image":"' . $image.'"}'; 
+0

おかげで

echo '{"query_result":"SUCCESS", "apartmentNumber":' . $apartmentNumber.' , "firstName":' . trim($firstName) .' ,"lastName":' . trim($lastName).', "image":"' . $image.'"}'; 

を交換する

"lastName":junior smith 

試してみるべきです! @fiddler –

+0

あなたは大歓迎です。偽者が答えを受け入れることはありません。 – fiddler

関連する問題