2
我必須使用HTTP Post請求上傳我的圖片。圖像將張貼到parse server如何使用POST解析服務器上傳圖片
我發現這個文檔,但我不知道如何停止上傳圖像。請幫幫我。我有一個http請求方法,請糾正我。
這裏是文檔。 https://www.parse.com/docs/rest#files
public boolean uploadingFiles (String tempFilePath) {
String filePath = null ;
String fileName = null ;
boolean flag = false ;
try {
filePath = tempFilePath ;
fileName = new String (filePath) ;
fileName = filePath.substring (filePath.lastIndexOf ("/") + 1 , filePath.length ()) ;
} catch (Exception e) {
Log.e ("Exception" , "uploadingFiles Message = " + e.toString ()) ;
}
HttpClient httpclient = new DefaultHttpClient () ;
try {
HttpPost httppost = new HttpPost ("https://api.parse.com/1/files/") ;
StringBody filename = new StringBody (fileName) ;
File f = new File (filePath) ;
FileBody bin = new FileBody (f) ;
MultipartEntity reqEntity = new MultipartEntity () ;
reqEntity.addPart ("X-Parse-Application-Id" , new StringBody("MY KEY")) ;
reqEntity.addPart ("X-Parse-REST-API-Key" , new StringBody("My KEY")) ;
reqEntity.addPart ("Content-Type:" , new StringBody("image/png")) ;
reqEntity.addPart ("file:" , bin) ;
httppost.setEntity (reqEntity) ;
System.out.println ("executing request " + httppost.getRequestLine ()) ;
HttpResponse response = httpclient.execute (httppost) ;
HttpEntity resEntity = response.getEntity () ;
System.out.println ("----------------------------------------") ;
System.out.println (response.getStatusLine ()) ;
if (resEntity != null) {
System.out.println ("Response content length: " + resEntity.getContentLength ()) ;
InputStream is = resEntity.getContent () ;
if (is != null) {
Writer writer = new StringWriter () ;
char [ ] buffer = new char [ 1024 ] ;
try {
Reader reader = new BufferedReader (new InputStreamReader (is , "UTF-8")) ;
int n ;
while ((n = reader.read (buffer)) != - 1) {
writer.write (buffer , 0 , n) ;
}
} finally {
is.close () ;
}
String serverResult = writer.toString () ;
System.out.println ("Response content: " + serverResult) ;
} else {
System.out.println ("Response nothing: ") ;
}
}
EntityUtils.consume (resEntity) ;
} catch (Exception e) {
e.printStackTrace () ;
} finally {
try {
httpclient.getConnectionManager ().shutdown () ;
} catch (Exception ignore) {
}
}
return flag ;
}
嗨@Arslan貴的問題得到解決。 – sandy 2012-11-05 13:23:45