Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中上传文件_Java_Android_File Upload - Fatal编程技术网

Java 在android中上传文件

Java 在android中上传文件,java,android,file-upload,Java,Android,File Upload,我需要将一个xml文件从android应用程序上传到远程主机。我找到了代码并从中获取了库。但我有一个错误: 02-17 22:06:41.144: E/AndroidRuntime(21644): FATAL EXCEPTION: main 02-17 22:06:41.144: E/AndroidRuntime(21644): java.lang.NoClassDefFoundError: org.apache.http.entity.mime.HttpMultipart 02-17

我需要将一个xml文件从android应用程序上传到远程主机。我找到了代码并从中获取了库。但我有一个错误:

02-17 22:06:41.144: E/AndroidRuntime(21644): FATAL EXCEPTION: main
02-17 22:06:41.144: E/AndroidRuntime(21644): java.lang.NoClassDefFoundError:     org.apache.http.entity.mime.HttpMultipart
02-17 22:06:41.144: E/AndroidRuntime(21644):    at org.apache.http.entity.mime.MultipartEntity.<init>(MultipartEntity.java:77)
02-17 22:06:41.144: E/AndroidRuntime(21644):    at org.apache.http.entity.mime.MultipartEntity.<init>(MultipartEntity.java:100)
我是否使用了错误的mime类型或下载了错误的库? 有解决办法吗? 谢谢

我发现了一个新的示例,它使用php脚本将图像上传到php服务器。所以我把它改为上传xml文件

Android活动:UploadXmlFile.java

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class UploadXmlFile extends Activity {
InputStream inputStream;
ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();
String the_string_response;
int contentLength;
String res = "";

    @Override
public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_upload_image);

        // create xml content
        String xmldata="<?xml version=\"1.0\" encoding=\"UTF-8\"?><langs><lan><id>1</id><name>java</name></lan><lan><id>2</id><name>c++</name></lan><lan><id>3</id><name>python</name></lan><lan><id>4</id><name>php</name></lan></langs>";

        nameValuePairs.add(new BasicNameValuePair("xmldata",xmldata));
        nameValuePairs.add(new BasicNameValuePair("filename","myfile.xml"));

         Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
              try{
                     HttpClient httpclient = new DefaultHttpClient();
                     HttpPost httppost = new HttpPost("http://your-url.com/upload_xml_file.php");
                     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                     HttpResponse response = httpclient.execute(httppost);
                     the_string_response = convertResponseToString(response);
                     runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(UploadXmlFile.this, "Response " + the_string_response, Toast.LENGTH_LONG).show();                         
                            }
                        });

                 }catch(final Exception e){
                      runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Toast.makeText(UploadXmlFile.this, "Error " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    });
                       Log.d("Error in http connection ",e.toString());
                 } 
        }
    });
     t.start();
    }

    public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{

         StringBuffer buffer = new StringBuffer();
         inputStream = response.getEntity().getContent();
         contentLength = (int) response.getEntity().getContentLength(); //getting content length…..
          runOnUiThread(new Runnable() {

        @Override
        public void run() {
            Toast.makeText(UploadXmlFile.this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show();                    
        }
    });

         if (contentLength < 0){
         }
         else{
                byte[] data = new byte[512];
                int len = 0;
                try
                {
                    while (-1 != (len = inputStream.read(data)) )
                    {
                        buffer.append(new String(data, 0, len)); //converting to string and appending  to stringbuffer…..
                    }
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                try
                {
                    inputStream.close(); // closing the stream…..
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                res = buffer.toString();     // converting stringbuffer to string…..

                runOnUiThread(new Runnable() {

                @Override
                public void run() {
                   Toast.makeText(UploadXmlFile.this, "Result : " + res, Toast.LENGTH_LONG).show();
                }
            });
                //System.out.println("Response => " +  EntityUtils.toString(response.getEntity()));
         }
         return res;
    }
}
PHP文件:upload_xml_file.PHP

<?php
    $base=$_REQUEST['xmldata'];
    $fname=$_REQUEST['filename'];
    header("Content-Type: text/xml; charset=utf-8");
    $file = fopen($fname, 'wb');
    fwrite($file, $base);
    fclose($file);
    echo 'Image upload complete!!, Please check your php file directory……';
?>
在服务器上上传php文件,并为android应用程序设置INTERNET权限


希望对其他人有所帮助。

将jar放在您的libs文件夹中。我做了。并添加到生成路径,然后从生成路径中删除。如果放在libs文件夹中,android SDK会自动添加。
<?php
    $base=$_REQUEST['xmldata'];
    $fname=$_REQUEST['filename'];
    header("Content-Type: text/xml; charset=utf-8");
    $file = fopen($fname, 'wb');
    fwrite($file, $base);
    fclose($file);
    echo 'Image upload complete!!, Please check your php file directory……';
?>