价值& lt; ?java.lang.String类型的xml不能转换为JSONObject
本文关键字:xml 不能 转换 JSONObject 类型 String lt lang java 价值 | 更新日期: 2023-09-27 18:11:16
我正在尝试使用android上传图像到c# web服务
Web服务接受这些参数:
byte[] data, string strFileName
但是每次我运行应用程序!它给了我这个错误:
Value <?xml of type java.lang.String cannot be converted to JSONObject
这是代码:
class ImageUploadTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... unsued) {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(
"http://192.168.1.100:8080/ScanFiles.asmx?op=vFileUpload");
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.JPEG, 80, bos);
byte[] data = bos.toByteArray();
entity.addPart("fileName", new StringBody(fnameglob));
entity.addPart("data", new ByteArrayBody(data, "image/jpeg", fnameglob));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,
localContext);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent(), "iso-8859-1"));
String sResponse = reader.readLine();
return sResponse;
} catch (Exception e) {
if (dialog.isShowing())
dialog.dismiss();
Toast.makeText(getApplicationContext(), e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
return null;
}
// (null);
}
有什么建议吗?如有任何帮助,不胜感激。
我用下面的代码解决了这个问题:
protected String doInBackground(Void... unsued) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.JPEG, 80, bos);
byte[] byte_arr = bos.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("data", image_str));
nameValuePairs.add(new BasicNameValuePair("strFileName",
fnameglob));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://192.168.1.100:1234/ScanFiles.asmx/vFileUpload");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getEntity()
.getContent(), "UTF-8"));
String sResponse = reader.readLine();
return sResponse;
} catch (Exception e) {
Log.e("HTTP", "Error in http connection " + e.toString());
} finally {
}
} finally {
}
return "done";
// (null);
}
这是web服务代码
[WebMethod]
public void vFileUpload(string data, string strFileName)
{
try
{
string str = data.Length.ToString();
string sDestinationScannedFiles = ConfigurationManager.AppSettings["DestinationScannedFiles"].ToString();
//string filePath = Server.MapPath(sDestinationScannedFiles + strFileName);
string filePath = sDestinationScannedFiles + strFileName;
File.WriteAllBytes(filePath, Convert.FromBase64String(data));
}
如果你想找到base64类,你可以在这里找到它
谢谢。