5

I am trying to read a QRCode from android device and send the extracted ID to the web server which is built using Django.

I followed the tutorial here and the data was sent properly to the tutorial server. Then I just customised the sent data to be only an integer (the id) and sent it to my server link, but the view method wasn't fired at all.

My Code:

Android Code:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String data = intent.getStringExtra("SCAN_RESULT");
            new HttpAsyncTask().execute("https://bfish.neuro.mpg.de/baierlab/inventory/");

        }
    }
}

private class HttpAsyncTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

        String test_data = "111";           

        return POST(urls[0],test_data);
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getBaseContext(), "Data Sent!",       Toast.LENGTH_LONG).show();
    }
}

public static String POST(String url, String data){
    InputStream inputStream = null;
    String result = "";
    try {

        HttpClient httpclient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(url);

        String json = "";

        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("cross_id", data);

        json = jsonObject.toString();

        StringEntity se = new StringEntity(json);

        httpPost.setEntity(se);

        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        HttpResponse httpResponse = httpclient.execute(httpPost);

        inputStream = httpResponse.getEntity().getContent();

        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }       
    return result;
}

Django View:

def inventory(request):

    # Just write something on a file to know the method is being fired or not 

    if request.method == 'POST':

        # Try to parse the cross_id from the POST

urls:

url(r'^inventory', views.inventory, name='inventory')

I know I have to read the data from request.body but the method is not fired at all.

Thank you very much.

The Maestro
  • 659
  • 1
  • 5
  • 21
  • What is the error? – arcegk Mar 12 '16 at 04:56
  • @arcegk The problem is that I am not receiving the POST in my server. I am not having an error message or anything else. The android app tells me the data is being sent but the server is not receiving! – The Maestro Mar 18 '16 at 09:22

0 Answers0