2012-07-31 3 views
3

android에서 도움이 필요합니다. 이 오류가 발생했습니다 Error Parsing data org.json.JSONException: Value <html> of type java.lang.String cannot be converted to JSONObjectString을 JSON으로 변환하는 중 오류가 발생했습니다.

다음은 내가 실행할 때 오류가 발생하는 페이지입니다.

public class Login extends Activity { 

//URL to get User Data 
private static String URL_GET = "http://10.0.2.2/android_connect/get_authentication.php"; 

// JSON Node names 
private static final String TAG_SUCCESS = "success"; 
private static final String TAG_USERS = "Users"; 
private static final String TAG_EMAIL = "Email"; 
private static final String TAG_PASSWORD = "Password"; 

JSONParser jParser = new JSONParser(); 
String dbPassword = null; 
// users JSONArray 
JSONArray Users = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // setting default screen to login.xml 
    setContentView(R.layout.login); 

    Button btn = (Button) findViewById(R.id.btnLogin); 


    // Listening to register new account link 
    btn.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      String password = ((EditText) findViewById(R.id.password)).getText().toString(); 
      new getAuthentication().execute(); 

      if (password.equals(dbPassword)) { 
       // Switching to Main screen 
       Intent i = new Intent(getApplicationContext(), EchoSphere.class); 
       startActivity(i); 
      } 
     } 
    }); 
} 
class getAuthentication extends AsyncTask<String, String, String>{ 

    @Override 
    protected String doInBackground(String... params) { 

     runOnUiThread(new Runnable() { 
      public void run() { 
       // Check for success tag 
       int success; 

       try { 
        String email = ((EditText) findViewById(R.id.email)).getText().toString(); 

        Log.d("email:", email); 

        List<NameValuePair> params = new ArrayList<NameValuePair>(); 
        params.add(new BasicNameValuePair(TAG_EMAIL, email)); 

        JSONObject json = jParser.makeHttpRequest(URL_GET, "GET", params); 
        Log.d("Password:", json.toString()); 

        success = json.getInt(TAG_SUCCESS);  

        if (success == 1) { 
         Users = json.getJSONArray(TAG_USERS); 
         JSONObject c = Users.getJSONObject(0); 
         dbPassword = c.getString(TAG_PASSWORD); 
         Log.d("DBPW:", dbPassword); 
        } 

        } catch (JSONException e) { 
        e.printStackTrace(); 
        } 

        } 

       }); 
     return null; 
    } 

} 
} 

이것은 PHP 스크립트이며, PHP의 유효성을 검사했으며 작동하는 것으로 나타났습니다.

<?php 
// array for JSON response 
$response = array(); 

// include db connect class 
require_once __DIR__ . '/db_connect.php'; 

// connecting to db 
$db = new DB_CONNECT(); 

// check for post data 
if (isset($_GET["Email"])) { 
$email = $_GET['Email']; 

// get a product from products table 
$result = mysql_query("SELECT Email, Password FROM Users WHERE Email = '$email'"); 

if (!empty($result)) { 
    // check for empty result 
    if (mysql_num_rows($result) > 0) { 

     $result = mysql_fetch_array($result); 

     $user[] = array(); 
     $user["Email"] = $result["Email"]; 
     $user["Password"] = $result["Password"]; 

     // user node 
     $response["Users"] = array(); 

     array_push($response["Users"], $user); 

     // success 
     $response["success"] = 1; 

     // echoing JSON response 
     echo json_encode($response); 
    } else { 
     // no product found 
     $response["success"] = 0; 
     $response["message"] = "No User found"; 

     // echo no users JSON 
     echo json_encode($response); 
    } 
} else { 
    // no product found 
    $response["success"] = 0; 
    $response["message"] = "No User found"; 

    // echo no users JSON 
    echo json_encode($response); 
} 
} else { 

// required field is missing 

$response["success"] = 0; 
$response["message"] = "Required field(s) is missing"; 

// echoing JSON response 
echo json_encode($response); 
} 
?> 

결과로

{"Users":[{"0":[],"Email":"1","Password":"123"}],"success":1} 

오류가에 자리하고있는 곳 모르겠어요 표시됩니다.

+0

소스를보고 http://10.0.2.2/android_connect/get_authentication.php의 결과를 인쇄하면 JSON 외에 다른 것이 있습니까? – henrikpersson

+0

그 밖의 것은 JSON과 별개로 나타납니다. 결과는 –

+2

입니다. 서버의 응답은 html 오류 페이지입니다. 이유는 json 구문 분석 오류가 발생하는 것입니다. –

답변

0

오류를 해결했습니다.

도움 주셔서 감사합니다.

나는 PHP 스크립트에

header('Content-type: application/json'); 

을 추가하고 응용 프로그램을 실행할 수 있습니다.

관련 문제