如何从android代码调用php文件来上传图像和字符串。基本上我想将该图像上传到本地主机并将字符串上传到 MySQL .. 我需要在 php 文件中写入以存储在 MySql db 中的内容
			
			1209 次
		
2 回答
            1        
        
		
在客户端
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;    
String pathToOurFile = "path of the image.jpeg";
String urlServer = "http://xxx.xxx.xxx.xxx/uploader.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary =  "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
try {
        FileInputStream fileInStream = new FileInputStream(new File(pathToOurFile) );
        URL url = new URL(urlServer);
        connection = (HttpURLConnection) url.openConnection();
       // Allow Inputs & Outputs
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        // Enable POST method
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
        outputStream = new DataOutputStream( connection.getOutputStream() );
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        bytesAvailable = fileInStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];
        // Read file
        bytesRead = fileInStream.read(buffer, 0, bufferSize);
        while (bytesRead > 0)
        {
            outputStream.write(buffer, 0, bufferSize);
            bytesAvailable = fileInStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInStream.read(buffer, 0, bufferSize);
        }
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
        // Responses from the server (code and message)
        serverResponseCode = connection.getResponseCode();
        serverResponseMessage = connection.getResponseMessage();
        fileInputStream.close();
        outputStream.flush();
        outputStream.close();
    }
    catch (Exception ex)
    {
        //Exception handling
    }
在服务器端,
<?php
    $target_path  = "./";
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
         echo "Success";
    } else{
        echo "Error";
    }
?>
    于 2013-04-11T07:31:57.323   回答
    
    
            0        
        
		
Hope the below will help,
private class MyPost extends AsyncTask<Void,Void,Void>{
            @Override
            protected Void doInBackground(Void... arg0) {
                // TODO Auto-generated method stub
                // Create a new HttpClient and Post Header
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://yoursite.com/page.php");
            try {
                // Add your data
                EditText txtName = (EditText)findViewById(R.id.txtBusinessName);
                EditText txtDesc = (EditText)findViewById(R.id.txtDescription);
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("frmName",txtName.getText().toString() ));
                nameValuePairs.add(new BasicNameValuePair("frmDesc", txtDesc.getText().toString()));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                Log.v("Post Status","Code: "+response.getStatusLine().getStatusCode());
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
            return null;
        }
    }
    于 2013-04-11T07:28:16.713   回答