繁体   English   中英

使用JSON将对象从服务器(PHP)发送到客户端(Java)

[英]Sending Object from Server (PHP) to client (Java) using JSON

我想要实现的是从PHP使用JSON发送对象,并将其也作为Java中的对象接收。 每当我使用以下代码时,它都会从Java发出JSON异常,表明String无法转换为JSONObject。 请帮助您。 谢谢

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>

您不应该在PHP输出中添加DOCTYPE或HTML标记,而只是回显(或消亡)JSON内容。

小会话示例:

    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; 

我将[Data]用于返回的任何数据,并将[Code]用于错误代码,它非常易于解析和证明。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM