2014-01-07 81 views
0

我想實現的是使用PHP中的JSON發送對象,並將它作爲Java中的對象接收。當我使用下面的代碼時,它會給出來自java的JSON異常,說String不能轉換爲JSONObject。請我會感謝您的幫助。由於使用JSON將服務器(PHP)中的對象發送到客戶端(Java)

PHP代碼

<?php 

    require_once("/classes/Login.class.php");//Class that connect to the database 
    $user = $_GET['ballername']; 
    $pass = $_GET['ballerpassword']; 
    $log = new Login($user, $pass); 
    $log->connect(); 
    //header('Content-type: application/json'); 

    $correct = array('success'=> true) 

    echo json_encode($log); 

?> 

Java代碼

public class UserLoginTask extends AsyncTask<Void, Void, Boolean> { 
ConnectToServer connectToServer = null; 
String urlstring = null; 
Campusian campusian; 
@Override 
protected Boolean doInBackground(Void... params) { 
    // TODO: attempt authentication against a network service. 

    urlstring = CampusPalURL.LOGIN_URL +"ballername=" + mUserName +"&ballerpassword="+mPassword; 


    HttpClient client = new DefaultHttpClient(); 
    HttpGet getMethod = new HttpGet(urlstring); 

    HttpResponse response; 
    boolean confirmation; 
    try { 
     response = client.execute(getMethod); 
     HttpEntity httpEntity = response.getEntity(); 
     String state = EntityUtils.toString(httpEntity); 
     Log.e("STTTTTTTTTTTTTTTTTTTTTTTTTTTT ", state); 
     try { 
      JSONObject jsonObject = new JSONObject(state); 
      //JSONObject confirmation = jsonObject.getJSONObject(""); 
      confirmation = jsonObject.getBoolean("logged_in"); 
      return confirmation; 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      Log.e("Object: ", e.getMessage()); 
      e.printStackTrace(); 
     } 
    } catch (IOException ee) { 
     // TODO Auto-generated catch block 
     Log.e("EXCEPTION: ", ee.getMessage()); 
     ee.printStackTrace(); 
    } 


    // TODO: register the new account here. 
    return true; 
} 

錯誤日誌

01-07 10:54:22.689: E/Object:(1022): Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject 

我決定檢查從顯示EntityUtils.toString服務器接收到的輸入(httpEntity)在日誌這是我得到的。這樣對嗎?

01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <html xmlns="http://www.w3.org/1999/xhtml"> 
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <head> 
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <title>Untitled Document</title> 
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): </head> 
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <body> 
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <html xmlns="http://www.w3.org/1999/xhtml"> 
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <head> 
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <title>Untitled Document</title> 
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): </head> 
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <body> 
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): </body> 
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): </html>{"logged_in":true} </body> 
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): </html> 
+0

您使用哪個庫從JSON獲取對象? – Goikiu

+0

我覺得這個帖子會幫助你:http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android。有一個非常好的指導,幫助我如何獲取和解析您的JSON響應。 – owe

+0

您可以在PHP中回顯$ log的值嗎? –

回答

0

您不應將DOCTYPE或HTML標記添加到PHP輸出中,只是回顯(或死掉)JSON內容。

0

小會話範例:

session_start(); 
    $_SESSION['uid'] = $u_id; 
    $result = mysql_query("UPDATE USER SET USER_SESSION ='".session_id()."' WHERE user_id=$u_id"); 
    $arr = array('Data' => null,'Code' => null); 
    $arr['Code'] = 200; 
    $arr['Data']['Session_ID'] = session_id(); 
    echo json_encode($arr); 
    exit; 

我使用[數據]對於任何數據恢復和[代碼]的錯誤代碼,它非常方便的分析和論證。

相關問題