【问题标题】:Android - How do you pass a byte[] to a .net Webservice using HttpClient / HttpPost?Android - 如何使用 HttpClient / HttpPost 将字节 [] 传递给 .net Web 服务?
【发布时间】:2010-10-14 21:12:01
【问题描述】:

如何使用 byte[] 参数调用 Web 服务?尝试使用命名值对,但它们只接受字符串。我没有使用Ksoap2,我们决定使用HttpClient / HttpPost方法。

有人有什么想法吗?

这是我的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    // WORKS WITH JPEG FILE -- String existingFileName = "/sdcard/dcim/Camera/1225231027592.jpg";
    String existingFileName = "/sdcard/dcim/0217175.jpg";
    String mMyFilename = "0217175.jpg";

    //  /sdcard/Music/kryptonite.mp3"; //DOES NOT WORK WITH MP3 FILE

    File uploadFile = new File(existingFileName);
    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream(uploadFile);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";

    String mTest;
    String Method = null;

    try
    {

        //********************************************************************
        //  Create the HttpClient and the HttpPost
        //      Note:  Need to use HttpPost because of arguments/parameters
        //********************************************************************
        HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost();


        fileInputStream = new FileInputStream(uploadFile);

        // create a buffer of maximum size
        int bytesAvailable = fileInputStream.available();
        int maxBufferSize = 1024;
        int bufferSize = Math.min(bytesAvailable, maxBufferSize);
        byte[] buffer = new byte[bufferSize];

        // read file and write it into form...
        int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        //*************************************************************
        //Add Value Pairs for Parameters to pass to the Webservice
        //*************************************************************
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 



        //nameValuePairs.add(new BasicNameValuePair(buffer)); 

        nameValuePairs.add(new BasicNameValuePair("filename", mMyFilename)); 






    }
    catch (MalformedURLException ex) {
        // Log.e(Tag, "error: " + ex.getMessage(), ex);
    }
    catch (IOException ioe) {
        String s = "";

        //Log.e(Tag, "error: " + ioe.getMessage(), ioe);
    }
}

}

网络服务:

[WebMethod]
public void PutFile(byte[] buffer, string filename)
{
    string serverpath;

    //serverpath = @"\\63.237.52.201\UploadFiles\Incoming\";
    serverpath = @"C:\Temp\";

    //BinaryWriter binWriter = new BinaryWriter(File.Open(Server.MapPath(filename), FileMode.CreateNew, FileAccess.ReadWrite));
    BinaryWriter binWriter = new BinaryWriter(File.Open(serverpath + filename, FileMode.CreateNew, FileAccess.ReadWrite));
    binWriter.Write(buffer);
    binWriter.Close();
}

【问题讨论】:

    标签: android web-services arrays httpclient http-post


    【解决方案1】:

    您可以将其转换为 base64 表示并将 byte[] 作为字符串发送。然后在服务器端恢复它。以防万一您找不到更好的方法。

    【讨论】:

    • 好的 - 我正在尝试使用 base64 到 web 服务并创建文件,但是当您尝试打开 jpg 文件时什么都没有。
    • 代码:String Base64EncodedData = Base64.encodeToString(buffer, 0) List nameValuePairs = new ArrayList(2); nameValuePairs.add(new BasicNameValuePair("base64data",Base64EncodedData)); nameValuePairs.add(new BasicNameValuePair("filename", mMyFilename));
    • Webservice: byte[] buffer = Convert.FromBase64String(base64data); File.WriteAllBytes(fullfilename_and_path, buffer);
    • 我做错了什么吗?在 android 代码或 Webservice 上?
    • 我不能说。对我来说似乎没问题。这两个问题密切相关:stackoverflow.com/questions/1067655/…stackoverflow.com/questions/2105364/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多