1

The problem that I am having is that the apk i made is working on Samsung galaxyS1 and SE XperiaPlay but not working on Samsung Galaxy S4. Mainly on the login screen when the user presses the button it should move the user to next screen if the details are correct or display an alert box. The button can be pressed but it doesn't trigger any actions, like a dummy button if you will. I have no idea what to do with that.

manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.miniproject"
    android:versionCode="8"
    android:versionName="1.6" >
 <compatible-screens>
    <screen
        android:screenDensity="480"
        android:screenSize="normal" />
</compatible-screens>

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/> 
    <application
        android:layout_width="fill_parent" 
              android:layout_height="fill_parent" 
              android:orientation="vertical" 
              android:icon="@drawable/domo"
        android:allowBackup="true"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >


          <activity
            android:label="@string/app_name"
            android:name=".splashScreen">   
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
         </activity>

        <activity
            android:name="com.example.miniproject.MainActivity"
            android:label="@string/app_name" >
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>

          <activity 
            android:label="@string/app_name"
            android:name=".tableScreen">
         </activity>

    </application>

</manifest>

MainActivity.java

package com.example.miniproject;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;



public class MainActivity extends Activity implements OnClickListener {

    Button submit;
    EditText username;
    EditText password;
    HttpPost httppost;
    HttpResponse response;
    HttpClient httpclient;
    List<NameValuePair> nameValuePairs;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username =(EditText) findViewById(R.id.editTextUsername);
        password = (EditText) findViewById(R.id.editTextPassword);
        submit = (Button) findViewById(R.id.buttonSubmit);
        submit.setOnClickListener(this);
    }
    public void toast()
    {
        Context context = getApplicationContext();
        CharSequence text = "Table loading...";
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }


    public void onClick(View v) 
    {   
        if(v.getId() == submit.getId())
        {
            login();
        }   
    }

    private void login() 
    {

    try{    
            httpclient=new DefaultHttpClient();
            httppost= new HttpPost("http://www.daydreamsoftware.com/connect.php");
            nameValuePairs = new ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("username",username.getText().toString().trim()));  
            nameValuePairs.add(new BasicNameValuePair("password",password.getText().toString().trim())); 
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            response=httpclient.execute(httppost);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            final String response = httpclient.execute(httppost, responseHandler);

            //connect.php response User Found = true; No Such User Found = false
            if(response.equalsIgnoreCase("User Found"))
            {
                toast();
                startActivity(new Intent(MainActivity.this, tableScreen.class));
            }
            else
            {
                AlertDialog.Builder alertBox = new AlertDialog.Builder(this);
                alertBox.setMessage("Wrong username or password");
                alertBox.show();
            }

        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }//end:login()
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@drawable/sand"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >



    <EditText
        android:id="@+id/editTextUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editTextPassword"
        android:layout_alignLeft="@+id/editTextPassword"
        android:layout_marginBottom="34dp"
        android:ems="10" />

    <EditText
        android:id="@+id/editTextPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:ems="10"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/buttonSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editTextPassword"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="@string/stringLogIn" />

</RelativeLayout>

I have created a AVD for S4 using this s4AVD it has the same problem as the phone. I am attaching the logCat that appears after the button is clicked:

11-17 22:57:45.851: W/System.err(832): android.os.NetworkOnMainThreadException
11-17 22:57:45.851: W/System.err(832):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-17 22:57:45.851: W/System.err(832):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-17 22:57:45.851: W/System.err(832):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-17 22:57:45.851: W/System.err(832):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-17 22:57:45.861: W/System.err(832):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-17 22:57:45.871: W/System.err(832):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-17 22:57:45.871: W/System.err(832):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-17 22:57:45.871: W/System.err(832):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-17 22:57:45.871: W/System.err(832):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-17 22:57:45.871: W/System.err(832):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-17 22:57:45.871: W/System.err(832):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-17 22:57:45.891: W/System.err(832):  at com.example.miniproject.MainActivity.login(MainActivity.java:76)
11-17 22:57:45.891: W/System.err(832):  at com.example.miniproject.MainActivity.onClick(MainActivity.java:60)
11-17 22:57:45.891: W/System.err(832):  at android.view.View.performClick(View.java:4084)
11-17 22:57:45.902: W/System.err(832):  at android.view.View$PerformClick.run(View.java:16966)
11-17 22:57:45.902: W/System.err(832):  at android.os.Handler.handleCallback(Handler.java:615)
11-17 22:57:45.902: W/System.err(832):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-17 22:57:45.912: W/System.err(832):  at android.os.Looper.loop(Looper.java:137)
11-17 22:57:45.922: W/System.err(832):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-17 22:57:45.922: W/System.err(832):  at java.lang.reflect.Method.invokeNative(Native Method)
11-17 22:57:45.931: W/System.err(832):  at java.lang.reflect.Method.invoke(Method.java:511)
11-17 22:57:45.931: W/System.err(832):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-17 22:57:45.931: W/System.err(832):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-17 22:57:45.941: W/System.err(832):  at dalvik.system.NativeStart.main(Native Method)
Community
  • 1
  • 1
Michal
  • 89
  • 7
  • 1
    what does v.getId() return on the S4? – Rick Falck Nov 17 '13 at 20:42
  • 1
    To be honest I did not check it on emulator for s4 only install it straight on the device, I will try that ASAP – Michal Nov 17 '13 at 20:56
  • I found the answer.After onCreate() put this lines StrictMode.ThreadPolicy policy = newStrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); Thank you all for help [1]: http://stackoverflow.com/questions/11277734/http-doesnt-work-in-android-emulator – Michal Nov 17 '13 at 23:13

3 Answers3

3

You apparently have 'strict mode' turned on - on both your emulator and your actual S4.

'Strict Mode' is telling you that you've done network access on the UI thread, which you should not be doing.

Move your network access to a background thread, using either AsyncTask or one of the several good http libraries that are available, like OkHTTP, Retrofit, Volley, etc.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • Thank You GrayBeardedGeek ,my thoughts exactly . Whats surprising is that bypassing 'strict mode' policy works on avd but not on the phone – Michal Nov 18 '13 at 01:32
1

You disable the strict mode using following code in your onCreate:

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = 
        new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

But that is not recommended. Use AsyncTask interface.

Rishabh Srivastava
  • 3,683
  • 2
  • 30
  • 58
0

Thread policy bypass works on avd but not on actual S4. Bellow is the updated code that I used. Everything is working how it suppose to.I really wonder why the strict mode can be bypassed this way on a emulator but not on a device ?

package com.example.miniproject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;



public class MainActivity extends Activity implements OnClickListener {

    Button submit;
    EditText username;
    EditText password;
    HttpPost httppost;
    HttpResponse response;
    HttpClient httpclient;
    List<NameValuePair> nameValuePairs;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username =(EditText) findViewById(R.id.editTextUsername);
        password = (EditText) findViewById(R.id.editTextPassword);
        submit = (Button) findViewById(R.id.buttonSubmit);
        submit.setOnClickListener(this);
    }
    public void toast()
    {
        Context context = getApplicationContext();
        CharSequence text = "Table loading...";
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }


    public void onClick(View v) 
    {   
        if(v.getId() == submit.getId())
        {
            BackgroundTask one = new BackgroundTask();
            one.execute();
        }   
    }



public class BackgroundTask extends AsyncTask<String,Integer,Boolean>
{
        @Override
        protected void onPreExecute() 
        {
          super.onPreExecute();
        }

        @Override
        protected Boolean doInBackground(String... params) 
         {
            boolean sucess=false;

             try{   
                    httpclient=new DefaultHttpClient();
                    httppost= new HttpPost("http://www.daydreamsoftware.com/connect.php");
                    nameValuePairs = new ArrayList<NameValuePair>();

                    nameValuePairs.add(new BasicNameValuePair("username",username.getText().toString().trim()));  
                    nameValuePairs.add(new BasicNameValuePair("password",password.getText().toString().trim())); 
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    response=httpclient.execute(httppost);
                    ResponseHandler<String> responseHandler = new BasicResponseHandler();
                    final String response = httpclient.execute(httppost, responseHandler);

                    //connect.php response User Found = true; No Such User Found = false
                    if(response.equalsIgnoreCase("User Found"))
                    {

                        sucess=true;
                    }


                }
            catch(IOException e)
                {
                     e.printStackTrace();
                }
            return sucess;
         }
        protected void onPostExecute(Boolean result) 
        {
            if(result){
        toast();
        startActivity(new Intent(MainActivity.this, tableScreen.class));
        }

        if(!result){
            AlertDialog.Builder alertBox = new AlertDialog.Builder(MainActivity.this);
            alertBox.setMessage("Wrong username or password");
            alertBox.show();
        }

        }

        }
}
Michal
  • 89
  • 7