gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:47:01 25 4
gpt4 key购买 nike

我需要通过在 c# 中调用 restful webservice 从我的 android 应用程序上传图像,但是当我通过将 byte[] 添加到 JSONObject 来尝试它时,它会将 byte[] 转换为字符串并且 c# 服务抛出 Bad request("反序列化命名空间“expected.Found text”[B@22b6cc7f”中的 Object.Element 时出错。

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","FileName":"name.jpg"}

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

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

最佳答案

尝试将位图转换为字符串并将此字符串传递给 C# 服务器

 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();
}
}

关于java - 将图像字节数组作为 json 从 android 应用程序传递到 c# restful 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38990050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com