gpt4 book ai didi

php - JSON 异常没有返回结果

转载 作者:行者123 更新时间:2023-11-29 00:23:10 24 4
gpt4 key购买 nike

Android 应用程序从 PHP 文件中接收 ID、名称和电子邮件 ID、名称、电子邮件作为 JSON 响应发送到 Android 应用程序,问题是当我单击“获取数据”时没有收到数据并且我在 LogCat 中收到异常这是我的代码、PHP 文件和堆栈跟踪主要 Activity

   package com.example.colcheck;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.colcheck.Library.*;

public class MainActivity extends Activity {
TextView uid;
TextView name1;
TextView email1;
Button Btngetdata;

//URL to get JSON Array
private static String url = "http://localhost/new/check2.php";

//JSON Node Names
private static final String TAG_USER = "user";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";

JSONArray user = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
Btngetdata = (Button)findViewById(R.id.getdata);
Btngetdata.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
new JSONParse().execute();

}
});

}

private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
uid = (TextView)findViewById(R.id.uid);
name1 = (TextView)findViewById(R.id.name);
email1 = (TextView)findViewById(R.id.email);
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();

}

@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();

// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {

System.out.println("JSON Response : "+json.toString().trim());

String id="";
String name="";
String email="";
if(json.has("id"))
id = json.getString("id");

if(json.has("name"))
name = json.getString("name");

if(json.has("TAG_EMAIL"))
email = json.getString("TAG_EMAIL");

//Set JSON Data in TextView
uid.setText(id);
name1.setText(name);
email1.setText(email);

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

}
}

}

带有 json 响应的 PHP 文件

 <?php

$con=mysqli_connect("localhost","root","123","pet_home");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM users WHERE username='test' AND password='123'");
$row_cnt = mysqli_num_rows($result);

if($row_cnt>0){
$row = mysqli_fetch_array($result);
$data = array('success'=>true, 'user'=>array("TAG_ID" => $row['id'], "TAG_NAME"=> $row['name'], "TAG_EMAIL" =>$row['email']));
}else{
$data = array('success'=>false);
}

header('Content-Type: application/json');
echo json_encode($data);



mysqli_close($con);


?>

执行此操作后没有响应返回到 android 应用程序这里是堆栈跟踪似乎有异常

11-30 17:06:13.679: I/System.out(3474): JSON Response : {"TAG_USER":{"id":"OFF","TAG_EMAIL":"OFF","name":"OFF"},"success":true}

最佳答案

试试这个..

如果你的json是这样的

{"id":"OFF","TAG_EMAIL":"OFF","name":"OFF"}

然后

您可以检查该标签名称是否存在

System.out.println("JSON Response : "+json.toString().trim());

if(json.has("id"))
String id = json.getString("id");

if(json.has("name"))
String name = json.getString("name");

if(json.has("TAG_EMAIL"))
String email = json.getString("TAG_EMAIL");

编辑:

你的解析如下

user = json.getJSONArray(TAG_USER);

没有 JSONArray 只有 JSONObject 所以你不能将 JSONObject 解析为 JSONArray 然后没有 用户作为JSONArray

编辑 1:

try {

System.out.println("JSON Response : "+json.toString().trim());

if(json.has("id"))
String id = json.getString("id");

if(json.has("name"))
String name = json.getString("name");

if(json.has("TAG_EMAIL"))
String email = json.getString("TAG_EMAIL");

//Set JSON Data in TextView
uid.setText(id);
name1.setText(name);
email1.setText(email);

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

编辑 2:

try {

System.out.println("JSON Response : "+json.toString().trim());

JSONObject c = json.getJSONObject("TAG_USER");

if(c.has("id"))
String id = c.getString("id");

if(c.has("name"))
String name = c.getString("name");

if(c.has("TAG_EMAIL"))
String email = c.getString("TAG_EMAIL");

//Set JSON Data in TextView
uid.setText(id);
name1.setText(name);
email1.setText(email);

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

关于php - JSON 异常没有返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20302621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com