Sarah Sarah - 4 months ago 54
JSON Question

OkHttp3 doesn't load all json data from server

Im going to take all user data from server in json format. the php generate code cool but i can not get all json in okhttp Response. in fact it just load part of json not all of them.

plus: the reason i use this code is that i want to take all user data from the server in login process.
here is my code in

import android.os.AsyncTask;
import android.util.Log;

import com.example.android.shopping.Helper.Tags;
import com.example.android.shopping.Model.Suggestion;
import com.example.android.shopping.Model.product;
import com.example.android.shopping.Model.user;



import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
* Created by Android on 7/21/2016.
*/
public class SelectAll_INFO_NetworkCallTask extends AsyncTask <String,Void,String>{
public static List<product> listOfProduct=new ArrayList<>();
public static List<Suggestion> listOfSuggestion=new ArrayList<>();
public static user myUser=new user();


public user getUser=new user();
private OkHttpClient client=new OkHttpClient();
String Result="";
public SelectAll_INFO_NetworkCallTask(user user)
{
this.getUser = user;
}

@Override
protected String doInBackground(String... params) {

try {
RequestBody requestBody;
requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("action", getUser.getStatus())
.addFormDataPart("userId",String.valueOf(getUser.getUsId()))
.build();

Request request = new Request.Builder()
.url(Tags.SelectAllAddress)
.post(requestBody)
.build();

Response response = client.newCall(request).execute();

Result=response.body().string();

return Result;

} catch (IOException e) {
e.printStackTrace();
}


return null;
}

@Override
protected void onPostExecute(String o) {
super.onPostExecute(o);

Log.d("Output is:",o);


}
}


and this is how do i call it:

public void RegForm(View view)
{
/* Intent intent=new Intent(this,RegisterUserActivity.class);
startActivity(intent);*/
user myUser=new user();
myUser.setStatus("palt");
myUser.setUsId(22);
SelectAll_INFO_NetworkCallTask infoCallTask=new SelectAll_INFO_NetworkCallTask(myUser);
infoCallTask.execute();
}


and also this is the expected and gotten data i shared on gist

Answer

Well first; you should not be using AsyncTask with OkHttp. OkHttp has its own asynchronous callback:

client.newCall(request).enqueue(new Callback() {

    @Override
    public void onFailure(Call call, IOException exception) {
        exception.printStackTrace();
    }

    @Override
    public void onResponse(Call call, Response response) {
        // Handle response
    }

}

Secondly, to answer your question; logcat has a maximum message output that is device dependent. Therefore, outputting any string that is longer than that maximum will be truncated. So, OkHttp is receiving the entirety of the response, logcat just cannot display all of it in one message.