OkHttp3不会从服务器加载所有json数据

时间:2016-07-21 16:15:13

标签: android json android-json okhttp3

我将以json格式从服务器获取所有用户数据。 php生成代码很酷,但我无法在okhttp响应中获取所有json。实际上它只是加载了json的一部分而不是全部。

plus:我使用此代码的原因是我想在登录过程中从服务器获取所有用户数据。 这是我的代码

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);


    }
}

这就是我怎么称呼它:

   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();
    }

以及this is the expected and gotten data i shared on gist

1 个答案:

答案 0 :(得分:1)

首先;你应not使用AsyncTask和OkHttp。 OkHttp有自己的异步回调:

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
    }

}

其次,回答你的问题; logcat具有与设备相关的最大消息输出。因此,输出任何超过该最大值的字符串将被截断。所以,OkHttp正在接收整个响应,logcat只是无法在一条消息中显示所有响应。