title: Java HttpPost to a php API
body: <p>Im trying to upload a file to a site using their API trough java on android.</p>
  
  <p>This is what im using to post and get a response:</p>
  
  <pre><code>    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&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(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;
  
  }
  </code></pre>
  
  <p>The response is a xml file witch i read and that part seems to work. The response i get is "No file uploaded!".</p>
  
  <p>The site have an example on how to call the api from php curl:</p>
  
  <pre><code>      // 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;
  </code></pre>
  
  <p><a href="http://www.glowfoto.com/api_upload.php" rel="nofollow">http://www.glowfoto.com/api_upload.php</a></p>
  
  <p>I have a very limited knowledge of webprogramming but isn't this equivalent to what im doing in java?</p>