将图像从soap Web服务获取到Android应用程序时出错

时间:2016-05-25 15:06:15

标签: android web-services soap imageview

我试图通过SOAP Web服务从服务器获取图像到android应用程序。我收到了一些错误。 我试图从Web服务接收字节数组中的图像。 我的代码是 -

网络服务代码是 -

@WebMethod(operationName = "GetDiseasePic")
public byte[] picFromServer(@WebParam(name = "file_name") String filename) {
    String filePath = "C:/Users/Ashraful/Desktop/layout/icons/" + filename;
    System.out.println("Sending file: " + filePath);

    try {
        File file = new File(filePath);
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream inputStream = new BufferedInputStream(fis);
        byte[] fileBytes = new byte[(int) file.length()];
        inputStream.read(fileBytes);
        inputStream.close();

        return fileBytes;
    } catch (IOException ex) {
        System.err.println(ex);
        throw new WebServiceException(ex);
    }

}

我的Android应用程序的活动是 -

private void imageSelect() {

final String NAMESPACE_D_PIC = "http://DiseaseInformationWS/"; // target namespace of the Web service available

final String METHOD_NAME_D_PIC = "GetDiseasePic";// Name of the method in the Webservice file

final String SOAP_ACTION_D_PIC = "http://DiseaseInformationWS/GetDiseasePic"; // SoapAction that consists of the NAMESPACE+ METHOD_NAME1

final String URL_D_PIC = "http://"+IpchangeInteface.IPADDRESS+":8080/AshrafThesisWebService/DiseaseWebSer vice?wsdl";

    new AsyncTask<String,Void, Bitmap>(){
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }


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

            SoapObject request_d_pic = new SoapObject(NAMESPACE_D_PIC,METHOD_NAME_D_PIC);
            request_d_pic.addProperty("file_name",imagefilename);

            SoapSerializationEnvelope envelop_d_pic = new SoapSerializationEnvelope(SoapEnvelope.VER10);
            envelop_d_pic.setOutputSoapObject(request_d_pic);

            HttpTransportSE androidHttpTransport_d_pic = new HttpTransportSE(URL_D_PIC);

            try {
                androidHttpTransport_d_pic.call(SOAP_ACTION_D_PIC,envelop_d_pic);

                SoapObject result_d_pic = (SoapObject) envelop_d_pic.bodyIn;
                SoapObject result_d_picSP = (SoapObject) envelop_d_pic.bodyIn;
                String getresult = result_d_pic.toString();
                Log.d("IMAGEFILE",getresult);
                if (getresult!=""){
                 filebyte = Base64.decode(getresult,Base64.DEFAULT);
                    bitmapImage = BitmapFactory.decodeByteArray(filebyte,0,filebyte.length);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            }
            return bitmapImage;
        }
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if(bitmapImage!=null) {
                diseasePic.setImageBitmap(bitmapImage);
            }
        }

    }.execute();
}

我得到的错误是 -

05-25 20:33:47.282 3184-3249/com.thesiswork.ashraf.ashrafresearchwork E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
                                                                                    java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                                        at android.os.AsyncTask$3.done(AsyncTask.java:299)
                                                                                        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
                                                                                        at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
                                                                                        at java.util.concurrent.FutureTask.run(FutureTask.java:239)
                                                                                        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
                                                                                        at java.lang.Thread.run(Thread.java:838)
                                                                                     Caused by: java.lang.IllegalArgumentException: bad base-64
                                                                                        at android.util.Base64.decode(Base64.java:161)
                                                                                        at android.util.Base64.decode(Base64.java:136)
                                                                                        at android.util.Base64.decode(Base64.java:118)
                                                                                        at com.thesiswork.ashraf.ashrafresearchwork.Disease_InformationScreen$5.doInBackground(Disease_InformationScreen.java:410)
                                                                                        at com.thesiswork.ashraf.ashrafresearchwork.Disease_InformationScreen$5.doInBackground(Disease_InformationScreen.java:384)
                                                                                        at android.os.AsyncTask$2.call(AsyncTask.java:287)
                                                                                        at java.util.concurrent.FutureTask.run(FutureTask.java:234)
                                                                                        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
                                                                                        at java.lang.Thread.run(Thread.java:838) 

请建议我现在该怎么办?

1 个答案:

答案 0 :(得分:0)

使用此:

filebyte = Base64.decode(getresult, Base64.NO_PADDING);