0

Here is my sample code.
It does barcodes scanning.
The method sendJson should call the webService.
The code runs, but in the Apache log I see NOTHING.

For some reason the webService is not called.
What am I doing wrong?

package com.example.podk.scan;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

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 org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;
import java.io.InputStream;


public class MainActivity extends Activity implements View.OnClickListener {

    private View.OnClickListener onClickListener;
    private Button scanBtn;
    private TextView formatTxt, contentTxt;

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        //retrieve scan result
        IntentResult scanningResult = 
           IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

        if (scanningResult != null) {
            //we have a result
            String scanContent = scanningResult.getContents();
            String scanFormat = scanningResult.getFormatName();
            formatTxt.setText("FORMAT: " + scanFormat);
            contentTxt.setText("CONTENT: " + scanContent);

            //call web service:
            sendJson("toto","foo");

        }else{
            Toast toast = Toast.makeText(getApplicationContext(),
                        "No scan data received!", Toast.LENGTH_SHORT);
            toast.show();
        }

    }

    public void sendJson(final String email, final String pwd) {

        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
        HttpResponse response;
        JSONObject json = new JSONObject();

        try {
            String WS_URL = "http://192.168.88.171:8080/test/test.php";
            HttpPost post = new HttpPost(WS_URL);
            json.put("email", email);
            json.put("password", pwd);
            StringEntity se = new StringEntity( json.toString());
            se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            post.setEntity(se);
            response = client.execute(post);

            /*Checking response */
            if(response!=null){
                InputStream in = response.getEntity().getContent(); //Get the data in the entity
            }

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void onClick(View v){
        //respond to clicks
        if(v.getId()==R.id.scann_button){
            //scan
            IntentIntegrator scanIntegrator = new IntentIntegrator(this);
            scanIntegrator.initiateScan();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scanBtn = (Button)findViewById(R.id.scann_button);
        formatTxt = (TextView)findViewById(R.id.scan_format);
        contentTxt = (TextView)findViewById(R.id.scan_content);
        //uaktywnij aktywnosc na przyciskanie
        scanBtn.setOnClickListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Alex Burton
  • 49
  • 1
  • 8

1 Answers1

0

Run adb logcat with your device connected and you'll probably see that your request is never getting sent because of a NetworkOnMainThread exception. You have to run network code in a Thread or preferably with an AsyncTask.

Here's the exception documentation http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

Here's the AsyncTask documentation http://developer.android.com/reference/android/os/AsyncTask.html

jlindenbaum
  • 1,891
  • 18
  • 28
  • Thanks a lot. I will study this. Could you please propose change to mine code? I'm not advanced in java :( – Alex Burton Apr 17 '15 at 21:00
  • put the `sendJSON` function into the `doInBackground` function of an `AsyncTask`. When it's done the request / processing, you can go back into your UI thread and update things accordingly in the `AsyncTask`'s `onPostExecute` function. – jlindenbaum Apr 17 '15 at 21:02
  • Thanks a lot for help! After hours I made ADB cooperate with me :) The direct reason why I had problem was missing line '' in AndroidManifest.xml. I would never know that special Internet permissions are required for Android to execute net connection... – Alex Burton Apr 18 '15 at 18:37
  • @AlexBurton you should go over the android developer docs and follow the tutorials on basic connectivity. You need the permission and you definitely need to do the network stuff off the UI thread. – jlindenbaum Apr 18 '15 at 19:15