Fast-android-networking: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

Created on 18 Apr 2018  路  10Comments  路  Source: amitshekhariitbhu/Fast-Android-Networking

I get this error when sending a get request

this is my code:

* build.gradle *
`apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "ir.supersoft.androidfastnetworking"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.amitshekhar.android:android-networking:1.0.0'
compile 'com.library.tangxiaolv:telegramgallery:1.0.3'
}
`

User.java
`public class User {

public int id;
public String first_name;
public String last_name;
public int age;
}
`

MainActivity.java
AndroidNetworking.initialize(getApplicationContext());

`AndroidNetworking.get("http://sekeh.gigfa.com/getUsers.php")
.setTag("test")
.setPriority(Priority.LOW)
.build()
.getAsOkHttpResponseAndObjectList(User.class, new OkHttpResponseAndParsedRequestListener>() {
@Override
public void onResponse(Response okHttpResponse, List users) {
// do anything with response
Log.d("amirhosein", "userList size : " + users.size());
for (User user : users) {
Log.d("amirhosein", "id : " + user.id);
Log.d("amirhosein", "firstname : " + user.first_name);
Log.d("amirhosein", "lastname : " + user.last_name);
Log.d("amirhosein", "age : " + user.age);
}
}

        @Override
        public void onError(ANError anError) {
          Log.e("amirhosein", "error: " + anError.getLocalizedMessage());
        }
      });

`

i test that in localhost(xampp) but it`s worked!

keep in mind that my androidnetworking version is 1.0.0
because 1.0.1 need to appcompatv7:27.0.2

Most helpful comment

Your response is not json. It is text/html so it is unable to parse. Send application/json from the server as the content-type.

All 10 comments

Do like this

AndroidNetworking.initialize(getApplicationContext());

Gson gson = new GsonBuilder()
        .setLenient()
        .create();

AndroidNetworking.setParserFactory(new GsonParserFactory(gson));

And also refer this

Also check your response correctly, is it maps to the object correctly?

Use JsonReader.setLen....... is fixed. thank you
but i get this error: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
What is its cause?
my json: http://sekeh.gigfa.com/getUsers.php

Please check the response. Is it matching with the Object?

yes

Provide the dummy response.

Your response is not json. It is text/html so it is unable to parse. Send application/json from the server as the content-type.

it`s worked
the reason is content-type!
Thank you for your prompt answer

Your response is not json. It is text/html so it is unable to parse. Send application/json from the server as the content-type.

very thank you!!!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rezasadeghi93 picture rezasadeghi93  路  8Comments

MohadFarhan picture MohadFarhan  路  5Comments

AymanAbouzeid picture AymanAbouzeid  路  4Comments

alitong picture alitong  路  8Comments

ExploiTR picture ExploiTR  路  7Comments