Android应用登录成功却报JSON解析错误:Value Table无法转为JSONObject求助
Hey there! Let's break down this JSON parsing error you're seeing. Even though your login function works, the error E/JSON Parser: Error parsing data org.json.JSONException: Value Table of type java.lang.String cannot be converted to JSONObject tells us exactly what's going wrong: the server isn't sending back a valid JSON object—it's returning the plain string "Table" instead, and your code is trying to turn that string into a JSONObject (which requires {...} formatted content to work).
First, confirm what the server is actually sending
The simplest way to debug this is to log the raw response from the server before attempting to parse it. Add this line right after you assign json = sb.toString(); in your JSONParser class:
json = sb.toString(); Log.d("Raw Server Response", "Received content: " + json); // Add this log
Run your app again and check Logcat for the Raw Server Response message—you’ll almost certainly see the string "Table" there, which confirms the server isn’t sending proper JSON.
Why is this happening?
There are a few common scenarios:
- The server’s login endpoint might be broken, returning an error message (like "Table") instead of the expected JSON success response.
- You could be hitting the wrong URL, or passing incorrect parameters, so the server is returning a non-JSON page (like a debug table or error screen) instead of the login data.
- Even though login appears to work, the server might not be configured to send a JSON success signal—it’s just returning a plain text flag instead.
How to fix it
Option 1: Fix the server (best long-term solution)
Check with your friend who shared the project to confirm what the login endpoint is supposed to return. A proper JSON response for a successful login should look something like this:
{ "success": 1, "message": "Login successful", "user": { "id": 123, "username": "your_name" } }
If the server can be adjusted to send this kind of valid JSON, your existing parsing code will work once that’s fixed.
Option 2: Adjust your client code (quick fix if the server can’t be changed)
If the server intentionally returns "Table" as a success signal, modify your parsing logic to handle plain text instead of forcing it into a JSONObject:
// Replace the parsing section in your makeHttpRequest method try { // Check if the response is a valid JSON object if (json.trim().startsWith("{") && json.trim().endsWith("}")) { jObj = new JSONObject(json); } else { // Wrap plain text response in a custom JSONObject jObj = new JSONObject(); jObj.put("server_response", json.trim()); } } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); }
Then, in your login code, you can check if the server_response field equals "Table" to confirm a successful login.
Bonus: Update your network code (optional but recommended)
Note that DefaultHttpClient is deprecated in newer Android versions. For better stability and performance, consider switching to modern libraries like OkHttp or using HttpURLConnection instead. This is a separate improvement—focus on fixing the JSON parsing issue first.
内容的提问来源于stack exchange,提问作者nur

