2015-11-11 68 views

回答

0

試試這個:

AsyncHttpClient client = new AsyncHttpClient(); 
    RequestParams params = new RequestParams(); 
    params.put("notes", "Test api support"); 
    client.post(restApiUrl, params, responseHandler); 

希望這有助於!

1

工作實例

HttpClient httpclient; 
HttpPost httppost; 


httpclient = new DefaultHttpClient(); 
httppost = new HttpPost(URLRepo.URL_IMAGESAVE); 


List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(2); 
nameValuePairs.add(new BasicNameValuePair("user_id", ""+deichapp.getInt("userid", 0))); 
//.. add parameters 
File file = new File(new URI(obj.getString("fileUri"))); 
nameValuePairs.add(new BasicNameValuePair("filename", file.getName())); 

httpclient.getParams().setParameter("Connection", "Keep-Alive"); 
httpclient.getParams().setParameter("Content-Type", "multipart/form-data;"); 

MultipartEntityBuilder entity = MultipartEntityBuilder.create(); 
        for (BasicNameValuePair nameValuePair : nameValuePairs) { 
      entity.addTextBody(nameValuePair.getName(), nameValuePair.getValue()); 
        } 

entity.addPart("file", new FileBody(new File(new URI(obj.getString("fileUri"))))); 

httppost.setEntity(entity.build()); 

// Send and store the Image 
HttpResponse response = httpclient.execute(httppost); 
    String json; 
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 
json = reader.readLine(); 

這裏使用了原生的Android API和沒有外部圖書館的像異步HTTP客戶端。確保您在後臺線程上執行此代碼。

相關問題