0
I am using an Async task below to call the webservice helper.

public class Send_reportclaim_Async extends AsyncTask<String, Void, String> {

    private ProgressDialog Dialog;

    @Override
    protected void onPreExecute() {
        Dialog = new ProgressDialog(Reportclaim.this);
        Dialog.setMessage(Reportclaim.this.getResources().getString(
                R.string.loading));
        Dialog.setCancelable(false);
        Dialog.show();
        // Dialog.setContentView(R.layout.progress_dialog);
    }

    @Override
    protected String doInBackground(String... params) {
        String result = "";
        try {


            if (isInternetPresent) {

                 WebserviceHelper.sendreportclaimdetails(civilid,
                        imei, policynum, caseno.getText().toString(), dateloss.getText().toString(),policy_rep.getText().toString());
                result = "Success";
            } else {
                result = "Failure";
            }
        } catch (Exception e) {
            result = "Failure";
            e.printStackTrace();

        }



        return result;

    }

@Override
    protected void onPostExecute(String result) {

        // Toast.makeText(getApplicationContext(), "post execute",
        // 1).show();

        if (result.contentEquals("Success")) {

            if (AAAparams.sendmsg.contentEquals("Success")) {

                //subview.setVisibility(View.INVISIBLE);
                Toast.makeText(getApplicationContext(),
                        AAAparams.sendmsg.toString(), 1).show();

            }

            else if (AAAparams.sendmsg.contentEquals("Failure")) {
                if ((language.contentEquals(""))
                        || (language.contentEquals("en"))) {
                    // /to do
                    Toast.makeText(getApplicationContext(), "You FAILED", 1)
                            .show();

                } else if (language.contentEquals("ar")) {

                    // to do
                    Toast.makeText(getApplicationContext(), "You FAILED", 1)
                            .show();

                }
            }

        } else {
            showAlert(Reportclaim.this,
                    getResources().getString(R.string.no_internet), "3");
        }

        Dialog.dismiss();

    }

}

The WebserviceHelper class given below.When i debugged while geting the result ie after getting the JSON object it goes to the exception.

public static void sendreportclaimdetails(String civil_id, String imei,
        String polno, String caseno,  String lossdate,String fileuploads)
        throws ClientProtocolException, IOException, JSONException {

    String result = null;

    JSONObject jObject = null;

    try {

        METHOD_NAME = "/Dtls";

        HttpPost request = new HttpPost(URL + METHOD_NAME);
        HttpClient httpclient = new DefaultHttpClient();
        List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        /*postParameters.add(new BasicNameValuePair("pImeiNumber",
                BaseActivity.doEncrypt(imei)));*/
String[] separated = fileuploads.split(":");
        String policy_rep= separated[0];
        String reg_book=separated[1];
        String Dri_lic=separated[2];



        File file = new File(fileuploads);
        FileBody fileboady = new FileBody(file); 



        MultipartEntity reqEntity = new MultipartEntity();
        // add file

        reqEntity.addPart("pImeiNumber", new StringBody(String.valueOf(BaseActivity.doEncrypt(imei))));
        reqEntity.addPart("pPolNo", new StringBody(String.valueOf(polno)));
        reqEntity.addPart("pCaseNo", new StringBody(String.valueOf(caseno)));
        reqEntity.addPart("pLossDat:", new StringBody(String.valueOf(lossdate)));
        reqEntity.addPart("pFileUploads", fileboady);
        request.setEntity(reqEntity);
         HttpResponse response = httpclient.execute(request);

        /*postParameters.add(new BasicNameValuePair("pCivilId", civil_id));

        postParameters.add(new BasicNameValuePair("pPolNo:", polno));
        postParameters.add(new BasicNameValuePair("pCaseNo:", caseno));
        postParameters.add(new BasicNameValuePair("pLossDate", lossdate));

        postParameters.add(new BasicNameValuePair("pFileUploads", fileuploads));*/

        /*
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
                postParameters);
        request.setEntity(entity);
        HttpResponse response = getThreadSafeClient().execute(request);*/
        entityResponse = response.getEntity();


        result = EntityUtils.toString(entityResponse, HTTP.UTF_8);
        //Log.e("resultttttttttttttttt", result.toString());
        JSONObject object = (JSONObject) new JSONTokener(result)
                .nextValue();




    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("svfsdgdfjnhgfkjghkg");

    }


}

String pol_rep,reg_book,dri_lic contains the path of the file to be uploaded.How to convert it to BASE 64 and then to JSON array.

zyonneo
  • 1,319
  • 5
  • 25
  • 63

1 Answers1

0

Use MultipartEntity for sending file and parameters on server:

HttpClient httpclient = new DefaultHttpClient();
HttpPost request = new HttpPost(URL + METHOD_NAME);
File file = new File(fileuploads);
FileBody fileboady = new FileBody(file); 
MultipartEntity reqEntity = new MultipartEntity();
// add file
reqEntity.addPart("uploadedfile", fileboady);
// add JSON String 
reqEntity.addPart("CaseNo:", new StringBody(String.valueOf(caseno)));
reqEntity.addPart("LossDate:", new StringBody(String.valueOf(lossdate)));
request.setEntity(reqEntity);
 HttpResponse response = httpclient.execute(request);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213