2014-05-06 139 views
0

我有一个使用GET方法开发webservices的客户端,我正在为他创建android应用程序。我总是使用HttpClient/HttpPost发送图像到服务器,但知道我需要发送它使用HttpGet,而我有点在这里输了...如何使用HttpClient/HttpGet将文件发送到服务器?

我正在寻找一些例子的小时,但我什么也没找到澄清我。我也尝试使用GET搜索HttpURLConnection,但我发现的所有示例均基于POST。

有人能给我一个提示吗?

谢谢

+1

您无法使用GET发送文件。 – njzk2

+1

HTTP方法GET不适合发送图像。您应该使用POST或PUT。 – hgoebl

+0

yesss @ hgoebl,这就是为什么我总是使用POST,但我的客户想用GET代替....所以,就像njzk2说,我们不能或只是没有appropiate? – lienmt

回答

0

您可以使用此代码发布其他文件类型,如图像。

import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.mime.HttpMultipartMode; 
import org.apache.http.entity.mime.MultipartEntity; 
import org.apache.http.entity.mime.content.FileBody; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.util.EntityUtils; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.app.Activity; 

public class MainActivity extends Activity { 

private static final String TAG = "MainActivity.java"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // we are going to use asynctask to prevent network on main thread exception 
    new PostDataAsyncTask().execute(); 

} 

public class PostDataAsyncTask extends AsyncTask<String, String, String> { 

    protected void onPreExecute() { 
     super.onPreExecute(); 
     // do stuff before posting data 
    } 

    @Override 
    protected String doInBackground(String... strings) { 
     try { 

      // 1 = post text data, 2 = post file 
      int actionChoice = 2; 

      // post a text data 
      if(actionChoice==1){ 
       postText(); 
      } 

      // post a file 
      else{ 
       postFile(); 
      } 

     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String lenghtOfFile) { 
     // do stuff after posting data 
    } 
} 

// this will post our text data 
private void postText(){ 
    try{ 
     // url where the data will be posted 
     String postReceiverUrl = "http://yourdomain.com/post_data_receiver.php"; 
     Log.v(TAG, "postURL: " + postReceiverUrl); 

     // HttpClient 
     HttpClient httpClient = new DefaultHttpClient(); 

     // post header 
     HttpPost httpPost = new HttpPost(postReceiverUrl); 

     // add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("firstname", "Mike")); 
     nameValuePairs.add(new BasicNameValuePair("lastname", "Dalisay")); 
     nameValuePairs.add(new BasicNameValuePair("email", "[email protected]")); 

     httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // execute HTTP post request 
     HttpResponse response = httpClient.execute(httpPost); 
     HttpEntity resEntity = response.getEntity(); 

     if (resEntity != null) { 

      String responseStr = EntityUtils.toString(resEntity).trim(); 
      Log.v(TAG, "Response: " + responseStr); 

      // you can add an if statement here and do other actions based on the response 
     } 

    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

// will post our text file 
private void postFile(){ 
    try{ 

     // the file to be posted 
     String textFile = Environment.getExternalStorageDirectory() + "/sample.txt"; 
     Log.v(TAG, "textFile: " + textFile); 

     // the URL where the file will be posted 
     String postReceiverUrl = "http://yourdomain.com/post_data_receiver.php"; 
     Log.v(TAG, "postURL: " + postReceiverUrl); 

     // new HttpClient 
     HttpClient httpClient = new DefaultHttpClient(); 

     // post header 
     HttpPost httpPost = new HttpPost(postReceiverUrl); 

     File file = new File(textFile); 
     FileBody fileBody = new FileBody(file); 

     MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
     reqEntity.addPart("file", fileBody); 
     httpPost.setEntity(reqEntity); 

     // execute HTTP post request 
     HttpResponse response = httpClient.execute(httpPost); 
     HttpEntity resEntity = response.getEntity(); 

     if (resEntity != null) { 

      String responseStr = EntityUtils.toString(resEntity).trim(); 
      Log.v(TAG, "Response: " + responseStr); 

      // you can add an if statement here and do other actions based on the response 
     } 

    } catch (NullPointerException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
} 
+0

谢谢你的回复,但我的疑问是如何使用HttpGet发送文件(如图像),而不是在你的代码中发布POST ... – lienmt

相关问题