将图像字节数组作为json从android应用程序传递给c#restful service

时间:2016-08-17 06:59:16

标签: java android web-services

我需要通过在c#中调用restful webservice从我的android应用程序上传图像,但是当我通过向JSONObject添加byte []来尝试它时,它将byte []转换为字符串并且c#service抛出Bad请求(& #34;从命名空间反序列化Object.Element时出错'期望。发现文本' [B @ 22b6cc7f'。

Android Code:
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ttulips);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    bitMapData = stream.toByteArray();

            JSONObject jsonParam = new JSONObject();
            try {
            jsonParam.put("IncomingFile",bitMapData);               
            jsonParam.put("FileName", "name.jpg");

            Log.d("Json",jsonParam+"");
} catch (JSONException e) {
            e.printStackTrace();
        }

JSON请求的日志即将发布        {" IncomingFile":" [B @ 22b67f""文件名":" name.jpg"}

甚至尝试将字节数组转换为Base64编码的字节数组,但在将base64字节数组添加到jsonobject时,它被视为字符串。

我应该如何解决这个问题?提前致谢。

4 个答案:

答案 0 :(得分:1)

尝试将此位图转换为字符串并将此字符串传递给c#server

 if(fileUri1 != null) {
                bitmap1 = BitmapFactory.decodeFile(fileUri1.getPath(),
                        options);
                ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
                if(bitmap1 != null) {
                    bitmap1.compress(Bitmap.CompressFormat.PNG, 50, baos1);
                    byte[] b1 = baos1.toByteArray();
                    bitmapstring1 = Base64.encodeToString(b1, 

                    Base64.DEFAULT);
                }
            }

webservice电话:

 public class CallWebService extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        // Call Webservice for Get Menus
        WebServiceCall webServiceCall = new WebServiceCall(); // Custom class for call webservice
        BitmapFactory.Options options = new BitmapFactory.Options();

        // downsizing image as it throws OutOfMemory Exception for larger
        // images
        options.inSampleSize = 8;


        parameters = new ArrayList<NameValuePair>();

        parameters.add(new BasicNameValuePair("Name",uname12));
        parameters.add(new BasicNameValuePair("Address", uaddr12));
        parameters.add(new BasicNameValuePair("Email", en));
        parameters.add(new BasicNameValuePair("Qualification", uquali12));
        parameters.add(new BasicNameValuePair("Phoneno", ucontactno12));
        parameters.add(new BasicNameValuePair("Appliedfor", uappfor12));
        parameters.add(new BasicNameValuePair("Image", bitmapstring));
        parameters.add(new BasicNameValuePair("Resumeimage", bitmapstring1));
        parameters.add(new BasicNameValuePair("Operation", "i"));
        Log.i("param::",parameters.toString());
        response = webServiceCall.makeServiceCall(mUrlWebServiceLogin, parameters);


        Log.d("ResponseLogin:", response);



        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        if (progressDialog.isShowing())
            progressDialog.dismiss();

        if(response.contains("\"success\"")){
            session.createLoginSession(uname12);
            Toast.makeText(getApplicationContext(),"Successfully inserted",Toast.LENGTH_SHORT).show();
            Intent in = new Intent(getApplicationContext(),InterView.class);
            in.putExtra("Name",uname12);

            startActivity(in);
            finish();


        }else{
            Toast.makeText(getApplicationContext(),"data not inserted",Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("Loading...");
        progressDialog.show();
        progressDialog.setCanceledOnTouchOutside(false);
        super.onPreExecute();
    }
}

答案 1 :(得分:0)

双方使用Base64,实际上将byte[]转换为String不是主要问题 read here

答案 2 :(得分:0)

  

请尝试以下创建JSON:

 String json = "{\"IncomingFile\":\""+ new String(bytesEncoded) +"\",\"FileName\":\""+name.jpg+"\"}";
  

其中bytesEncoded应为Base64编码图像。

希望它有所帮助!

答案 3 :(得分:-1)

这里对象bitMapData是一个bytearray,必须转换为String然后你必须在Json对象中使用 jsonParam.put("IncomingFile",new String(bitMapData));