Jsonexception: Value of Type Java.Lang.String Cannot Be Converted to Jsonobject

JSONException: Value of type java.lang.String cannot be converted to JSONObject

see this
http://stleary.github.io/JSON-java/org/json/JSONObject.html#JSONObject-java.lang.String-

JSONObject

public JSONObject(java.lang.String source)
throws JSONException

Construct a JSONObject from a source JSON text string. This is the most commonly used` JSONObject constructor.

Parameters:
source - `A string beginning with { (left brace) and ending with } (right brace).`
Throws:
JSONException - If there is a syntax error in the source string or a duplicated key.

you try to use some thing like:

new JSONObject("{your string}")

org.json.JSONException value Connection of type java.lang.String cannot be converted to JSONArray

result is a JSONObject not a JSONArray

   // fetch the object      
JSONObject obj = new JSONObject(result);

// fetch the data array
JSONArray jArray = obj.getJSONArray("data");

// Extract data from json and store into ArrayList
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
dataList.add(json_data.getString("name"));
}

{                     // beginning of JSONObject
"data": [ // nested JSONArray
{ // nested JSONObject inside JSONArray
"id": 5,
"name": "Deal One"
},
{
"id": 6,
"name": "Deal Two"
},
{
"id": 7,
"name": "Deal Three"
}
]
} // ending of JSONObject

Info : The other issue was due to Runtime-Permission implementation with flow of execution mean permission was not given at the time of establishing the internet connection.

org.json.JSONException: Value Connected of type java.lang.String cannot be converted to JSONObject

When instantiating a JSONObject you need to pass the actual string response:

ex: JSONObject obj = new JSONObject("result":[{"unique_id":"57933c641237d4.96974874","title":"23072016"},{"unique_id":"57901472464398.57585757","title":"hayley"}]");

Given that response.toString() gives you the string you should do the following:

JSONObject j = new JSONObject(response.toString());

P.S: I think you need this if you don't set the content type in your php :

<?
header('Content-Type: application/json');
$servername = "localhost";
$username = "USER";
$password = "PASS";
$dbname ="android_api";
....

JSONException: Value of type java.lang.String cannot be converted to JSONObject (Project Eclipse To Android Studio)

The server's response cannot cannot be parsed as JSON because the server returns an error message (in HTML) instead of JSON data.

It's a HTTP 404 Not found error, indicating that the URL was probably invalid.

It would also be good practice to check if the HTTP request was successful at all. If it wasn't, don't even try to parse the response as JSON.

JSON Exception value connection of type java.lang.String cannot be converted to JSONArray

I think the problem is in the content type of response. By default, the content type of any response from the PHP server is plain/text. Due to this, the android library considers it as plain text response instead of JSON response.

To fix your problem, you might need to set a response content type to application/json by adding following before print statement.

header('Content-Type: application/json');

Edit:
As I can see in the output and your code, you are printing few extra strings with the output like Connection Successful and echo"in DBadapter"; (in DBadaper.php). Due to this, the output becomes invalid JSON. Just remove those echo statements and your code should work



Related Topics



Leave a reply



Submit