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.