2011-02-09 7 views
2

私は2つのファイルを投稿するとき1の方法、およびいくつかのテキストを投稿する別のものを持っている、彼らはAndroidのポストファイルとテキスト

ポストの下のデータです...

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 
    } 


       } 

ポスト私は1つの方法にこれを取得し、ちょうど1つのポストを行うことができます方法があった場合、ファイルは...

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ファイルを作ってきた、私は思っていましたか?

ヘルプあなたはこのようなものを使用することができます

ジェームズ

答えて

2

おかげでいただければ幸いです:それはHttpClientをコマンドを認識does notの

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); 
+1

を、私は3.0.1 jarファイルが含まれていますファイル。任意のアイデアなぜ、それはHttpDefaultClientを取るが、それdoesnt仕事 – user606669

関連する問題