"Expected Begin_Object But Was String At Line 1 Column 1"

Expected BEGIN_OBJECT but was STRING at line 1 column 1

Even without seeing your JSON string you can tell from the error message that it is not the correct structure to be parsed into an instance of your class.

Gson is expecting your JSON string to begin with an object opening brace. e.g.

{

But the string you have passed to it starts with an open quotes

"

Gson: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 184 path $.data

I copied the above and it works for me. I suggest making sure that the string in data is identical to the JSON provided above.

JsonSyntaxException Gson: Expected BEGIN_OBJECT but was STRING

This is because your JSON payload looks like:

"{"username": "abc@gmail.com","address": "!Earth"}

Notice " at the beginning. When you remove first " it will start working. Notice what below lines:

System.out.println("\"{}\"");
System.out.println("{}");

print:

"{}"
{}

EDIT

System.out.println("Start point: " + json);
json = json.substring(1, json.length() - 1);
System.out.println("Get without \": " + json);
json = json.replaceAll("\\\\\"", "\"");
System.out.println("Valid: " + json);

Gson gson = new Gson();
User user = gson.fromJson(json, User.class);

System.out.println(user);

Above code prints:

Start point: "{\"username\": \"abc@gmail.com\",\"address\": \"!Earth\"}"
Get without ": {\"username\": \"abc@gmail.com\",\"address\": \"!Earth\"}
Valid: {"username": "abc@gmail.com","address": "!Earth"}
User{username='abc@gmail.com', address='!Earth'}

See also:

  • “Expected BEGIN_OBJECT but was STRING at line 1 column 1”

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 2 path $ while UPDATE

Finally solved my Issue, and the problem was kinda some lack of attention of my part. Since my data was getting updated that mean that i was gettng a response from my webservice, however at some point it caused that error, so i guessed maybe my code on the app was all correct and the problem was inside the webservice, and Bingo

Since i was putting in my OutputEditar the variables MSG and status, it was expecting to find them on my webservice. I have them defined in all my posts and on my delete, but i forgot to add them on my put so i add them like this on the ending of my endpoint, and now it is working properly as i wanted it.

$app->post('/api/editar_anuncios/{id}', function( $request, $response){
require_once('db/dbconnection.php');
$id = $request->getAttribute('id');
$users_id = $request->getParsedBody()["users_id"];
$morada = $request->getParsedBody()["morada"];
$n_quartos = $request->getParsedBody()["n_quartos"];
$latitude = $request->getParsedBody()["latitude"];
$longitude = $request->getParsedBody()["longitude"];
$fotografia = $request->getParsedBody()["fotografia"];
$preco = $request->getParsedBody()["preco"];
$ncasas_banho = $request->getParsedBody()["ncasas_banho"];
$telemovel = $request->getParsedBody()["telemovel"];
$mobilado= $request->getParsedBody()["mobilado"];
$outros_atributos = $request->getParsedBody()["outros_atributos"];
$qrcode = $request->getParsedBody()["qrcode"];

$data = array(
"users_id"=> $users_id,
"morada" => $morada,
"n_quartos" => $n_quartos,
"latitude" => $latitude,
"longitude" => $longitude,
"fotografia"=> $fotografia,
"preco" => $preco,
"ncasas_banho" => $ncasas_banho,
"telemovel" => $telemovel,
"mobilado" => $mobilado,
"outros_atributos" => $outros_atributos,
"qrcode" => $qrcode
);

if(isset($db->anuncios[$id])){
$result = $db->anuncios[$id]->update($data);

if($result){
$result = ['status' => true, 'MSG' => "Anúncio atualizado!"];
echo json_encode($result, JSON_UNESCAPED_UNICODE);
return $response;
} else
$result = ['status' => false, 'MSG' => "Erro na atualizacao do anúncio!"];
echo json_encode($result, JSON_UNESCAPED_UNICODE);
return $response;
} else
$result = ['status' => false, 'MSG' => "O Anúncio nao existe!"];
echo json_encode($result, JSON_UNESCAPED_UNICODE);
return $response;

});

Retrofit 2.0: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

This usually happens when you're receiving something other than the expected response from the server.

To understand what's wrong try imitate your request in Postman and see what you receive from server.

Moreover you can turn on OkHttp's Logging Interceptor to see exactly what the server returns in your Logcat.



Related Topics



Leave a reply



Submit