0

我正在尝试使用 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);
    }

有什么建议么?任何帮助都非常可观。


是否可以在一个离线 Web 应用程序中链接多个 .html 页面(无需重定向到浏览器)?

我目前正在努力在一个离线 Web 应用程序中组合多个 .html 页面。我以前使用过用于平板电脑的离线 Web 应用程序,并且知道如何使用 .manifest 文件的基础知识。

在此示例中,我将所有 .html 文件添加到我的主 .mainfest 文件中。现在的问题是,当我在 iPad 上保存我的应用程序以供离线使用并单击指向其他 .html 文件之一的链接时,Safari 会打开以显示链接的页面。

我现在的问题是,是否可以在一个离线网络应用程序中将页面链接在一起,或者是否有必要将我希望在一个单页解决方案中显示的每个项目联合起来。

最好的问候, ik

4

1 回答 1

0

我已经使用以下代码解决了我的问题:

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

这是网络服务代码

  [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 类,你可以在这里找到

谢谢。

于 2013-04-29T12:31:01.627 回答