title: Send POST request over Mobile Data
body: <p>I am developing an Android app that sends and receives data from a python server hosted in <strong>pythonanywhere</strong> using HTTP requests and JSON.</p>
  
  <p>The application is working perfect via WIFI but the problem occurs when I use it via Mobile Data. All the data coming from server with GET requests works perfectly but POST and DELETE request do not appear to send or otherwise work.</p>
  
  <p>I don't know whether the problem is</p>
  
  <ol>
  <li>free server</li>
  <li>untrusted app</li>
  <li>permissions</li>
  </ol>
  
  <p><strong><em>PostRequest.java</em></strong></p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  
  import org.apache.http.HttpResponse;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.entity.StringEntity;
  import org.apache.http.impl.client.DefaultHttpClient;
  
  import android.os.AsyncTask;
  import android.util.Log;
  
  public abstract class PostRequest extends AsyncTask&lt;String,Void,String&gt; {
  
  
      private static String convertInputStreamToString(InputStream inputStream) throws IOException{
          BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
          String line = "";
          String result = "";
          while((line = bufferedReader.readLine()) != null)
              result += line;
  
          inputStream.close();
          return result;
  
      }   
  
      @Override
      protected String doInBackground(String... params) {
  
          InputStream inputstream = null;
          String result = "";
          try {
              HttpClient httpclient = new DefaultHttpClient();
              HttpPost post = new HttpPost(params[0]);
              StringEntity se = new StringEntity(params[1]);
              post.setEntity(se);
              post.setHeader("Accept", "application/json");
              post.setHeader("Content-type", "application/json");
              HttpResponse httpResponse = httpclient.execute(post);
              inputstream = httpResponse.getEntity().getContent();
              if(inputstream != null)
                  result = convertInputStreamToString(inputstream);
              else
                  result = "Did not work!";
          } catch (Exception e) {
              Log.i("[ error stream ]", e.toString());
          }
  
          return result;
      }
  
      @Override
      abstract public void onPostExecute(String result);
  
  }
  </code></pre>
  
  <p><strong><em>RequestAddNewStory.java</em></strong></p>
  
  <pre><code>import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.Intent;
  import android.widget.Button;
  import android.widget.Toast;
  
  import com.shortstory.activities.AddHashtag;
  import com.shortstory.api.PostRequest;
  
  public class RequestAddNewStory extends PostRequest {
  
      Context context ;
      Button AddHashtag ;
  
      public RequestAddNewStory(Context context , Button AddHashtag)
      {
          this.context = context ;
          this.AddHashtag = AddHashtag ;
      }
  
      @Override
      public void onPostExecute(String result) {
  
          JSONObject json;
          try {
              json = new JSONObject(result);
              String s = json.getString("message");
              if (s.equals("done")) {
  
                 Toast.makeText(context,"now add hashtags", Toast.LENGTH_LONG).show();
                 Intent Addhashtag = new Intent(context , AddHashtag.class);
                 context.startActivity(Addhashtag);
                 ((Activity) context).finish();
              }else
              {
                  AddHashtag.setEnabled(true);
                  Toast.makeText(context,"can't add story", Toast.LENGTH_LONG).show();
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
      }
  
  }
  </code></pre>
  
  <p><strong><em>when request send</em></strong></p>
  
  <pre><code>json.accumulate("user_name", "mkm");
  json.accumulate("title", "hello");
  json.accumulate("body", "hello all");
  json.accumulate("img", " ");
  RequestAddNewStory rans = new RequestAddNewStory(this , AddHashtag);
  rans.execute("http://-----.pythonanywhere.com/api/story" , json.toString());
  </code></pre>