Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send POST request over Mobile Data

I am developing an Android app that sends and receives data from a python server hosted in pythonanywhere using HTTP requests and JSON.

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.

I don't know whether the problem is

  1. free server
  2. untrusted app
  3. permissions

PostRequest.java

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<String,Void,String> {


    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);

}

RequestAddNewStory.java

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();
        }

    }

}

when request send

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());
like image 882
Mohamed Moamen Avatar asked Nov 09 '22 14:11

Mohamed Moamen


1 Answers

After 3 weeks of debugging and see log files of server and log cat of client I'm not found any logical error happens so i return to see pythonanywhere free server limitations I found :

Access to external internet sites from your code e.g. urllib or wget

Specific sites via HTTP(S) only

i don't know what is point means but i decided use https instead of http and it's work perfect in both wifi and mobile data

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("https://-----.pythonanywhere.com/api/story" , json.toString());

But i'm still don't know if pythonanywhere free server allow you to send your requests only https why http request work perfect via wifi only.

like image 152
Mohamed Moamen Avatar answered Nov 14 '22 21:11

Mohamed Moamen