【问题标题】:how to call php file from android code to upload image and string. Basically i want to upload that image to localhost & string to MySQL [closed]如何从 android 代码调用 php 文件来上传图像和字符串。基本上我想将该图像上传到本地主机和字符串到 MySQL [关闭]
【发布时间】:2013-04-11 07:18:14
【问题描述】:

如何从android代码调用php文件来上传图片和字符串。基本上我想将该图像上传到本地主机和字符串到 MySQL .. 我需要在 php 文件中写入以存储在 MySql db 中的内容

【问题讨论】:

  • 谷歌搜索。你肯定会得到的。 stackoverflow 的帖子也很多
  • 到目前为止你尝试过什么?
  • 我不得不说你的问题缺乏很多相关信息。请尝试更好地描述您的系统。您是否正在考虑让手机直接插入您的数据库?如果是,则完全不建议这样做,除非您的应用用于 DB Administration。

标签: php android mysql


【解决方案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";
    }
?>

【讨论】:

    【解决方案2】:

    希望以下内容对您有所帮助,

    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-07-23
      • 2015-07-07
      • 1970-01-01
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多