Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Using MYSQL for my android app:

MainActivity.

public class MainActivity extends Activity implements OnClickListener {

Button login;
EditText username, password;
TextView status;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePair;


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

    setup();
}

private void setup() {
    // TODO Auto-generated method stub

    username = (EditText) findViewById(R.id.username);
    password = (EditText) findViewById(R.id.password);
    login = (Button) findViewById(R.id.login);
    status = (TextView) findViewById(R.id.status);
    login.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.main, menu);
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    switch(v.getId()) {
    case R.id.login:

        login();

        break;
    }
    }

private void login() {
    // TODO Auto-generated method stub
    try {
        httpclient= new DefaultHttpClient();
        httppost= new HttpPost("http://109.74.245.109/~profiled/android/index.php"); 
    //  Adding Data:
        nameValuePair = new ArrayList<NameValuePair>(1);
    //  Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
        nameValuePair.add(new BasicNameValuePair("username",username.getText().toString().trim()));
        nameValuePair.add(new BasicNameValuePair("password",password.getText().toString().trim()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
    // Execute HTTP Post Request
        response = httpclient.execute(httppost);



        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost, responseHandler);
        status.setText(""+response);
        if(response.equalsIgnoreCase("No user found")) {

            startActivity(new Intent(MainActivity.this, UserPage.class));
        }


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

Here is my error:

05-17 13:39:25.309: W/System.err(292): java.net.SocketException: Permission denied
05-17 13:39:25.318: W/System.err(292):  at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
05-17 13:39:25.318: W/System.err(292):  at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:186)
05-17 13:39:25.318: W/System.err(292):  at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:265)
05-17 13:39:25.318: W/System.err(292):  at java.net.Socket.checkClosedAndCreate(Socket.java:873)
05-17 13:39:25.318: W/System.err(292):  at java.net.Socket.connect(Socket.java:1020)
05-17 13:39:25.333: W/System.err(292):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
05-17 13:39:25.337: W/System.err(292):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
05-17 13:39:25.337: W/System.err(292):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-17 13:39:25.337: W/System.err(292):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-17 13:39:25.337: W/System.err(292):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
05-17 13:39:25.337: W/System.err(292):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-17 13:39:25.337: W/System.err(292):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-17 13:39:25.337: W/System.err(292):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-17 13:39:25.337: W/System.err(292):  at com.profiledt.helloworld.MainActivity.login(MainActivity.java:88)
05-17 13:39:25.337: W/System.err(292):  at com.profiledt.helloworld.MainActivity.onClick(MainActivity.java:70)
05-17 13:39:25.337: W/System.err(292):  at android.view.View.performClick(View.java:2408)
05-17 13:39:25.337: W/System.err(292):  at android.view.View$PerformClick.run(View.java:8816)
05-17 13:39:25.337: W/System.err(292):  at android.os.Handler.handleCallback(Handler.java:587)
05-17 13:39:25.337: W/System.err(292):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-17 13:39:25.357: W/System.err(292):  at android.os.Looper.loop(Looper.java:123)
05-17 13:39:25.357: W/System.err(292):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-17 13:39:25.357: W/System.err(292):  at java.lang.reflect.Method.invokeNative(Native Method)
05-17 13:39:25.357: W/System.err(292):  at java.lang.reflect.Method.invoke(Method.java:521)
05-17 13:39:25.357: W/System.err(292):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-17 13:39:25.357: W/System.err(292):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-17 13:39:25.357: W/System.err(292):  at dalvik.system.NativeStart.main(Native Method)

I don't fully understand the error, but with it being permissions I have come to a few conclusions:

  • It cannot connect to the mysql database
  • Something to do with IP address and my server?
share|improve this question
 
Do you have INTERNET permission set in your manifest file? developer.android.com/reference/android/… –  Mateusz May 17 '13 at 14:04
 
Hi Mateusz, I've added what the people below have suggested. Still not working –  Bradly Spicer May 17 '13 at 14:36
add comment

2 Answers

up vote 1 down vote accepted

Add this to your manifest and check.

<uses-permission android:name="android.permission.INTERNET"/>
share|improve this answer
 
Why Access network state? it requres an internet permission not network state to acesss the interet –  DArkO May 17 '13 at 14:10
 
Corrected, thanks! –  Juned Ahsan May 17 '13 at 14:13
 
Hi, I added this to activity_main.xml and The application won't run and I'm getting this: a LOT of errors: pastebin.com/xZSAtnj6 –  Bradly Spicer May 17 '13 at 14:27
 
You need to add this to AndroidMainfest.xml –  Juned Ahsan May 17 '13 at 14:28
 
Hey Juned, I added it to my AndroidManifest.xml underneath <manifest/> and I'm still getting the same error –  Bradly Spicer May 17 '13 at 14:34
show 2 more comments
<uses-permission android:name="android.permission.ACCESS_INTERNET"/>

Also i have written a library that can help you do this in a different thread as opposed to what you are doing( running requests in the UI thread).

Maybe you will like it. if not there are other options out there

https://github.com/darko1002001/android-rest-client

Hope this helps.

EDIT

There are 2 components at play usually. you don't connect to the Database directly, but rather you create a PHP script in your case which will connect to the database and act as a WebService which can accept requests and return JSON for example. THe android app acts as a client which calls your PHP webservice which connects to the database, reads and returns results.

so you have:

client(android) <-> Web service (PHP) <-> MySql Database

android doesn't go straight to the database.

A simple test would be the PC browser. if you enter your full url and press enter in a browser you should get the same exact response as you did that on android.

That means you have performed a GET request to your web service as all browser-enter actions are.

In your case it seems you are doing a POST request.

Read some artices on REST web services to get more info on how this is supposed to be setup.

Hope this helps.

share|improve this answer
 
Hi DArkO, I'm still somewhat new to all this, will this allow me to connect to a mysql database and echo out all the information? Thanks –  Bradly Spicer May 17 '13 at 14:35
 
edited my answer. –  DArkO May 17 '13 at 14:42
 
Hey DArkO, that's what I've been doing :) Thanks –  Bradly Spicer May 17 '13 at 14:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.