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有没有挂(透视)原来真的有辅助开挂(哔哩哔哩)1、wepoker有没有挂免...
第五分钟指南!hhpkoer辅... 第五分钟指南!hhpkoer辅助器视频(透视)原来真的是有辅助插件(哔哩哔哩)1、hhpkoer辅助...
第2分钟办法!wepoker模... 第2分钟办法!wepoker模拟器哪个(透视)好像真的有辅助脚本(哔哩哔哩)1、下载好wepoker...
第3分钟绝活!sohoo po... 第3分钟绝活!sohoo poker辅助器(透视)一直是有辅助插件(哔哩哔哩)1、在sohoo po...
三分钟机巧!德州机器人代打脚本... 三分钟机巧!德州机器人代打脚本(透视)其实真的有辅助透视(哔哩哔哩)1、全新机制【德州机器人代打脚本...
三分钟积累!wepoker数据... 三分钟积累!wepoker数据分析(透视)果然真的是有辅助神器(哔哩哔哩)1、wepoker数据分析...
一分钟方案!we-poker辅... 一分钟方案!we-poker辅助器(透视)好像是真的有辅助教程(哔哩哔哩)1、we-poker辅助器...
8分钟诀窍!wepoker辅助... 您好,wepoker辅助器有哪些功能这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275...
四分钟法子!wpk透视是真的假... 四分钟法子!wpk透视是真的假的(透视)都是是有辅助神器(哔哩哔哩)wpk透视是真的假的辅助器是一种...
第七分钟总结!hhpoker有... 第七分钟总结!hhpoker有后台操作吗(透视)果然是有辅助教程(哔哩哔哩)1、该软件可以轻松地帮助...