crlf expected at end of chunk httpclient

5.4k Views Asked by At

I am trying to perse data from my server. I am using HttpClient to get my data. But sometime the data is not fetched and i am shown the error called crlf expected at the end of chunk.I have tried to Change buffer size in jmeter properties following this link but the issue is not solved. I am giving my code below.Cant find the solution. Need help.

FavouriteCategoriesJsonParser.java

import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;


public class FavouriteCategoriesJsonParser {
    public static ArrayList<String> selectedCategories = new ArrayList<>();

    public ArrayList<Category> getParsedCategories() {
        String JsonFavouriteCategories = "";
        ArrayList<Category> MyArraylist = new ArrayList<>();

        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("http://xxxxx.com.yy/test_file/get_value.php");



        try {


            //  ServiceHandler jsonParser = new ServiceHandler();
            //      String json = jsonParser.makeServiceCall(campaign_credit,ServiceHandler.GET,params);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            JsonFavouriteCategories = EntityUtils.toString(httpResponse.getEntity());
            JSONArray jsonArray = new JSONArray(JsonFavouriteCategories);





            for (int i = 0; i < jsonArray.length(); i++) {
                Category genres = new Category();
                JSONObject MyJsonObject = jsonArray.getJSONObject(i);
                genres.setCateogry_id(MyJsonObject.getString("DOC_CODE"));
                genres.setCategory_Name(MyJsonObject.getString("DOC_CODE"));
                genres.setCategory_Name2(MyJsonObject.getString("DOC_NAME"));

                genres.setSelected(Boolean.parseBoolean(MyJsonObject.getString("SELECTED")));
                MyArraylist.add(genres);


                if (MyJsonObject.getString("SELECTED").equals("true")) {
                    selectedCategories.add(MyJsonObject.getString("DOC_CODE"));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return MyArraylist;
    }
}

FatchData.java

    import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;



import com.myproject.demo.adapter.CategoryAdapter;
import com.myproject.demo.model.Category;
import com.myproject.demo.FavouriteCategoriesJsonParser;


//PcProposalDoc
public class PcProposalDoc extends AppCompatActivity {
    Context context;
    ArrayList<Category> array_list;
    FavouriteCategoriesJsonParser categoryJsonParser;
    String categoriesCsv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.proposal_activity_main);
        Typeface fontFamily = Typeface.createFromAsset(getAssets(), "fonts/fontawesome.ttf");
        Button button = (Button) findViewById(R.id.selectCategoryButton);
        context = this;

        new asyncTask_getCategories().execute();




        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                categoriesCsv = FavouriteCategoriesJsonParser.selectedCategories.toString();
                categoriesCsv = categoriesCsv.substring(1, categoriesCsv.length() - 1);

                if (categoriesCsv.length() > 0) {
                    new asyncTask_insertUpdatefavouriteCategories().execute();
                } else {
                    Toast.makeText(context, "Please Select Doctor", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }





    public class asyncTask_getCategories extends AsyncTask<Void, Void, Void> {
        ProgressDialog dialog = new ProgressDialog(context);

        @Override
        protected void onPreExecute() {
            dialog.setTitle("Please wait...");
            dialog.setMessage("Loading Doctors!");
            dialog.show();
            array_list = new ArrayList<>();
            categoryJsonParser = new FavouriteCategoriesJsonParser();
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            array_list = categoryJsonParser.getParsedCategories();
            return null;
        }

        @Override
        protected void onPostExecute(Void s) {

            ListView mListViewBooks = (ListView) findViewById(R.id.category_listView);
            final CategoryAdapter categoryAdapter = new CategoryAdapter(context, R.layout.row_category, array_list);
            mListViewBooks.setAdapter(categoryAdapter);
            super.onPostExecute(s);
            dialog.dismiss();
        }

    }







    public class asyncTask_insertUpdatefavouriteCategories extends AsyncTask<Void, Void, Void> {

        String response;

        @Override
        protected Void doInBackground(Void... params) {
            response = InsertUpdateFavouriteCategories.insertUpdateCall(categoriesCsv);
            return null;
        }

        @Override
        protected void onPostExecute(Void s) {


            Toast.makeText(context, response, Toast.LENGTH_SHORT).show();
            super.onPostExecute(s);




        }
    }















}
1

There are 1 best solutions below

0
On

May be old, Can save some time..... I got this error where Server is in Python and Clinet is Java.

1st Error from Java Client

Error while sending data over http java.io.IOException: CRLF expected at end of chunk: 79/82
java.io.IOException: CRLF expected at end of chunk: 79/82

2nd Error from Java Clinet

Error while sending data over http java.io.IOException: chunked stream ended unexpectedly
java.io.IOException: chunked stream ended unexpectedly"

Both the errors got resolved by changing the ok response with chunked stream size

One with issues

HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nTransfer-Encoding: chunked\r\nServer: Jetty(6.1.26)\r\n\r\nDE\r\n"

Resolved with

HTTP/1.1 200 OK\r\nContent-Length: 20000\r\nContent-Type: application/json\r\nTransfer-Encoding: chunked\r\nServer: Jetty(6.1.26)\r\n\r\n229\r\n"

Note = nDE is replaced with n229