Android的后处理文件和文本 [英] Android post file and text

查看:106
本文介绍了Android的后处理文件和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2种方法目前1发布一个文件,而另一个发布一些文字,它们低于

I have 2 methods at the moment 1 to post a file, and another to post some text, they are below

发布的数据...

public void postData() {  
    // Create a new HttpClient and Post Header  

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    EditText et = (EditText) findViewById(R.id.entry);
    String enteredName = et.getText().toString();
    gender();
    category();
    nameValuePairs.add(new BasicNameValuePair("name",enteredName));
    nameValuePairs.add(new BasicNameValuePair("gender",radio));
    nameValuePairs.add(new BasicNameValuePair("cat",radio2));

    //http post
    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2:90/upload.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {  
        // TODO Auto-generated catch block      
    } catch (IOException e) {  
        // TODO Auto-generated catch block  
    }  


              }

发布的文件...

post the file...

public void postFile(){
    File file = new File(filedir2);
    try {
             System.out.println(filedir2);
             HttpClient client = new DefaultHttpClient();  
             String postURL = "http://10.0.2.2:90/mobileupload.php";                 
             HttpPost post = new HttpPost(postURL); 


         FileBody bin = new FileBody(file);
         MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
         reqEntity.addPart("image", bin);
         post.setEntity(reqEntity);  
         HttpResponse response = client.execute(post);  
         HttpEntity resEntity = response.getEntity();  
         if (resEntity != null) {    
                   Log.i("RESPONSE",EntityUtils.toString(resEntity));
             }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我已经做了结合了mobileupload.php和upload.php的一个PHP文件,我只是想知道是否有一种方法,我能得到这个成一个方法,只是做一个职位?

I have made a php file that combines both mobileupload.php and upload.php, i was just wondering if there was a way i could get this into one method and just do one post?

帮助将AP preciated

Help would be appreciated

感谢

詹姆斯

推荐答案

您可以使用这样的:

File file = new File("FileToSend.txt");
HttpClient client = new HttpClient();

String url = "http://www.yourdomain.com/destination.php";
PostMethod postMethod = new PostMethod(url);

Part[] parts = {new FilePart(file.getName(), file)};
postMethod.setParameter("name", "value"); // set parameters like this instead in separate call

postMethod.setRequestEntity( new MultipartRequestEntity(parts, postMethod.getParams()));

int status = client.executeMethod(postMethod);

这篇关于Android的后处理文件和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆