在android json解析中将图像和文本传输到post表单

时间:2014-09-01 00:50:20

标签: android json post

private class PhotoRegister extends AsyncTask< String, Integer, String >{

    @Override
    protected String doInBackground(String... urls) 
    { 

        String content = executeClient();
        return content;
    }

    @Override
    protected void onPostExecute(String result) {
        try {
        //data lodding
            new CommentLoding().execute(null, null, null);
            commentPutEdit.setText("");
            commentPutEdit.setInputType(0);
        } catch (Exception e) {e.printStackTrace();}
    }
    // sending
    public String executeClient() {
        try {   
            URL url = new URL("json.php");
            String boundary = "SpecificString";
            URLConnection con = url.openConnection();
            con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());

            wr.writeBytes("\r\n--" + boundary + "\r\n");
            wr.writeBytes("Content-Disposition: form-data; name=\"msg\"\r\n\r\n" + commentPutEdit.getText().toString());
            wr.writeBytes("\r\n--" + boundary + "\r\n");
            wr.writeBytes("Content-Disposition: form-data; name=\"suid\"\r\n\r\n" + clickSuid);
            wr.writeBytes("\r\n--" + boundary + "\r\n");
            wr.writeBytes("Content-Disposition: form-data; name=\"uuid\"\r\n\r\n" + uuid);
            wr.writeBytes("\r\n--" + boundary + "\r\n");
            wr.writeBytes("Content-Disposition: form-data; name=\"image\"; filename=\"image.jpg\"\r\n");
            wr.writeBytes("Content-Type: application/octet-stream\r\n\r\n");
            FileInputStream fileInputStream = new FileInputStream(-file name-);
            int bytesAvailable = fileInputStream.available();
            int maxBufferSize = 1024;
            int bufferSize = Math.min(bytesAvailable, maxBufferSize);
            byte[] buffer = new byte[bufferSize];

            int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            while (bytesRead > 0)
            {
                // Upload file part(s)
                DataOutputStream dataWrite = new DataOutputStream(con.getOutputStream());
                dataWrite.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available(); 
                bufferSize = Math.min(bytesAvailable, maxBufferSize); 
                bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
            } 
            fileInputStream.close();
            wr.writeBytes("\r\n--" + boundary + "--\r\n");
            wr.flush();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

你想同时传输图像和文本所以现在更改andoeneyo传输任何东西的代码......这有什么不对吗?

0 个答案:

没有答案