0
我試圖上傳一個文件到一個網站使用他們的API通過Android上的java。Java HttpPost到一個PHP API
這是用來發布和得到迴應IM:
public static HttpResponse upload(String imagePath){
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("www.site.com/api.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("type", "file"));
nameValuePairs.add(new BasicNameValuePair("image", "@" + imagePath));
nameValuePairs.add(new BasicNameValuePair("thumbsize", "200"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
System.out.println("tryar");
System.out.println(EntityUtils.toString(httppost.getEntity()));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
return response;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
的響應是一個XML文件,巫婆我讀和部分似乎工作。我得到的迴應是「沒有文件上傳!」。
該網站有關於如何調用從PHP捲曲API的例子:
// post with curl
$ch = curl_init($postURL);
$post['type']='file';
$post['image']='@'.$filename;
// You can set this to either 100,200,300, or 400 to specify the size of the thumbnail
$post['thumbsize']="400";
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 240);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
$result = curl_exec($ch);
curl_close($ch);
return $result;
我有webprogramming非常有限的知識,但不是這個相當於什麼IM做java嗎?
謝謝!問題解決了。 – mysanders 2011-04-01 09:17:12