Java 将图像字节数组作为json从android应用程序传递到c#restful服务

Java 将图像字节数组作为json从android应用程序传递到c#restful服务,java,android,web-services,Java,Android,Web Services,我需要通过在c#中调用restful webservice从我的android应用程序上载一个图像,但是当我尝试将byte[]添加到JSONObject时,它会将byte[]转换为string,而c#服务会抛出错误的请求(“反序列化对象时出错。应为命名空间“”中的元素。找到文本”[B@22b6cc7f" Android Code: Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ttulips)

我需要通过在c#中调用restful webservice从我的android应用程序上载一个图像,但是当我尝试将byte[]添加到JSONObject时,它会将byte[]转换为string,而c#服务会抛出错误的请求(“反序列化对象时出错。应为命名空间“”中的元素。找到文本”[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请求的日志如下所示 {“收入档案”:[B@22b67f,“FileName”:“name.jpg”}

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


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

在两侧使用
Base64
,实际上将
字节[]
转换为
字符串并不是主要问题

尝试以下方法创建JSON:

其中
字节编码的
应为Base64编码图像


希望有帮助!

这里的对象
bitMapData
是一个字节数组,必须转换为字符串,然后才能在Json对象中使用
jsonParam.put(“IncomingFile”,新字符串(bitMapData));

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

网络服务呼叫:

 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();
    }
}
公共类CallWebService扩展异步任务{
@凌驾
受保护的Void doInBackground(Void…参数){
//调用Web服务获取菜单
WebServiceCall WebServiceCall=新的WebServiceCall();//调用webservice的自定义类
BitmapFactory.Options=new-BitmapFactory.Options();
//缩小图像,因为它会抛出较大的内存异常
//图像
options.inSampleSize=8;
参数=新的ArrayList();
添加(新的BasicNameValuePair(“名称”,uname12));
添加(新的BasicNameValuePair(“地址”,uaddr12));
添加(新的BasicNameValuePair(“电子邮件”,en));
添加(新的BasicNameValuePair(“资格”,uquali12));
添加(新的BasicNameValuePair(“Phoneno”,ucontactno12));
添加(新的BasicNameValuePair(“Appliedfor”,uappfor12));
添加(新的BasicNameValuePair(“图像”,位图字符串));
添加(新的BasicNameValuePair(“Resumeimage”,bitmapstring1));
添加(新的BasicNameValuePair(“操作”,“i”));
Log.i(“param::”,parameters.toString());
response=webServiceCall.makeServiceCall(mUrlWebServiceLogin,参数);
Log.d(“ResponseLogin:”,response);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
if(progressDialog.isShowing())
progressDialog.disclose();
if(response.contains(“\”success\”)){
会话.createLoginSession(uname12);
Toast.makeText(getApplicationContext(),“已成功插入”,Toast.LENGTH\u SHORT.show();
Intent in=新的Intent(getApplicationContext(),InterView.class);
in.putExtra(“名称”,uname12);
星触觉(in);
完成();
}否则{
Toast.makeText(getApplicationContext(),“未插入数据”,Toast.LENGTH_SHORT.show();
}
}
@凌驾
受保护的void onPreExecute(){
progressDialog=新建progressDialog(MainActivity.this);
progressDialog.setMessage(“加载…”);
progressDialog.show();
progressDialog.setCanceledOnTouchOutside(false);
super.onPreExecute();
}
}

Base64编码字符串有什么问题?这应该是您的选择JSON只支持有限的值类型(字符串、数字、布尔值和null)。因此,您必须将字节数组转换为字符串并进行相应的反序列化。谢谢您的回答。我的c服务需要字节[]。那么是否有发送字节[]的选项而不是字符串?否则需要更改服务?这里不需要JSON数组…字节数组非常适合单个字符串一个小错误bro..ur correct@devnull69感谢您的解决方案。我尝试了此解决方案,但它显示了无效字符“.”。服务是否需要字节数组或字符串?删除“.”我是json,是的,服务应该只需要字符串,因为如何在json中传递字节,最终它将是字符串,对吗?
 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);
                }
            }
 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();
    }
}