Android-PHP错误引发undefined index错误。
创始人
2024-08-19 20:00:44
0次

在Android开发中,当与PHP后端交互时,可能会遇到“undefined index”错误,这通常是由于未正确获取或解析返回的JSON数据所导致的。以下是解决该问题的代码示例:

import android.os.AsyncTask;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class PHPConnection extends AsyncTask {

    private static final String TAG = "PHPConnection";

    @Override
    protected String doInBackground(String... params) {
        String urlString = params[0];
        String jsonInputString = params[1];
        String result = "";

        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");

            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(jsonInputString.getBytes("UTF-8"));
            outputStream.close();

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = connection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String line;
                StringBuilder stringBuilder = new StringBuilder();

                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append(line);
                }

                result = stringBuilder.toString();
                bufferedReader.close();
                inputStream.close();
            } else {
                result = "Error: " + responseCode;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    @Override
    protected void onPostExecute(String result) {
        try {
            JSONObject jsonObject = new JSONObject(result);
            String value = jsonObject.getString("key"); // 替换"key"为你期望获取的键名
            // 处理获取到的数据
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e(TAG, "JSONException: " + e.getMessage());
        }
    }
}

在调用PHPConnection类的地方,传递URL和JSON字符串作为参数。确保替换“key”为你期望获取的键名。通过检查返回的JSON数据是否存在该键名,可以避免“undefined index”错误。

相关内容

热门资讯

专业讨论!wepoker模拟器... wepoker模拟器哪个好用AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏...
重大消息!wpk有那种辅助吗,... 重大消息!wpk有那种辅助吗,WPK有挂吗教程(先前有开挂)AI 智能体黑科技再上新,自动化能力开挂...
推荐几款新版!wepoker底... 您好:wepoker底牌透视这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
总算了解!wpk有作弊吗,德普... 总算了解!wpk有作弊吗,德普之星外卦神器软件教程(竟然有开挂)AI 智能体黑科技再上新,自动化能力...
大神推荐!wepoker透视脚... wepoker透视脚本免费AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻...
避坑细节!如何下载德普之星辅助... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
一分钟了解!!wepoker辅... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
玩家科普!wpk真的有透视嘛,... 玩家科普!wpk真的有透视嘛,WPK透视脚本工具(好像存在有辅助挂)AI 智能体黑科技再上新,自动化...
一分钟揭秘!德普之星辅助工具如... 您好:德普之星辅助工具如何打开这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
大家学习交流!wepoker游... 您好:wepoker游戏安装教程这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很...