title: HTTP Post Android webview
body: <p>I'm trying to implement a function, which passes base64 encoded image from a webview to my web application via HTTP Post. And then, lets say, this image must be shown right away in my view. I've used this <a href="http://apachejava.blogspot.com/2011/05/post-params-with-android-webview.html" rel="nofollow">example</a> but it didn't work properly, so after googling a little bit, I've started using AsyncTask:</p>
  
  <pre><code>public void tryUpload(String url, Context context) {
      new UploadTask(context).execute(url);
  }
  
  private class UploadTask extends AsyncTask&lt;String, Void, HttpResponse&gt; {
      private Context context;
      public UploadTask(Context ctx) {
          context = ctx;
      }
  
      @Override
      protected HttpResponse doInBackground(String... urls) {
          List&lt;NameValuePair&gt; formData = new ArrayList&lt;NameValuePair&gt;(3);
          formData.add(new BasicNameValuePair("image", "base64EncodedString"));
  
          HttpPost httpPost = new HttpPost(urls[0]);
          try {
              httpPost.setEntity(new UrlEncodedFormEntity(formData, HTTP.UTF_8));
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          }
          HttpClient httpClient = new DefaultHttpClient();
          HttpResponse response = null;
          try {
              response = httpClient.execute(httpPost);
  
              WebView browser = (WebView) findViewById(R.id.my_web_engine);
              browser.setWebChromeClient(new MyWebChromeClient());
              browser.setWebViewClient(new MyWebViewClient());
  
              try{
                  String data = new BasicResponseHandler().handleResponse(response);
                  browser.loadDataWithBaseURL(urls[0], data, "text/html", HTTP.UTF_8, null);
              } catch (IOException e) {
                  e.printStackTrace();
          }
  
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          return response;
      }
  
      @Override
      protected void onPostExecute(HttpResponse response) {
          HttpEntity entity = response.getEntity();
          if (entity != null) {
              String responseBody = null;
              try {
                  responseBody = EntityUtils.toString(response.getEntity());
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
              if (responseBody.equalsIgnoreCase("OK"))
                  Log.w("myApp", "Everything is OK");
              else
                  Log.w("myApp", "Something is wrong");
          }
      }
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      if (Intent.ACTION_SEND.equals(action) &amp;&amp; type != null) {
          if (type.startsWith("image/")) {
                      // for simplicity skipped image encoding to base64
                      String url = getResources().getString(R.string.url) + "/MyController/ImageUpload";
                      Context context = ImageUploadActivity.this;
                      tryUpload(url, context);
              }
          }
      }
  }
  </code></pre>
  
  <p>And here is my ASP.NET MVC action:</p>
  
  <pre><code>// temporary removed authorization
  [HttpPost]
  public ActionResult ImageUpload(string image)
  {
      // skipped some lines
      ViewData.Add("imageFromWebView", image);
      return View();
  }
  </code></pre>
  
  <p>And a <code>View</code> contains following line:</p>
  
  <p><code>&lt;img src="data:image/png;base64,&lt;%: (string)ViewData["imageFromWebView"] %&gt;"/&gt;</code></p>
  
  <p>When I delete <code>[HttpPost]</code>, the view is loaded without a problem, but the image is not displayed.</p>
  
  <p>But when I leave <code>[HttpPost]</code>, I get an exception </p>
  
  <p><code>org.apache.http.client.HttpResponseException: Not Found</code> </p>
  
  <p>in the following line: </p>
  
  <p><code>String data = new BasicResponseHandler().handleResponse(response);</code></p>
  
  <p><strong>Does anyone know what I'm doing wrong?</strong></p>