Click here to Skip to main content
14,773,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am new in Android. I have to access MYSQL database for login module. Please guide me how do I do that using PHP webservices.

Thanks in advance!

1 solution

Use wampserver and use json code to connect php service to your database..
Misbah1 17-Aug-12 2:12am
I am using wampserver and json code. There is no syntax error in my project but desired output does not display... I dont know the reason... following is the code...

import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;

import android.util.Log;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView txt;

public void onCreate(Bundle savedInstanceState) {

LinearLayout rootLayout = new LinearLayout(getApplicationContext());
txt = new TextView(getApplicationContext());



public static final String KEY_121 = ""; //i use my real ip here

private String getServerData(String returnString) {

InputStream is = null;

String result = "";

//the year data to send
ArrayList<namevaluepair> nameValuePairs = new ArrayList<namevaluepair>();

//http post
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());

//convert response to string
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());

AndroidVivek 17-Aug-12 8:23am
if any error occurs which you cant see then use LogCat of the eclipse , its better to use..
Misbah1 17-Aug-12 11:37am
Is there any other method to connect to mysql from android eclips?
Misbah1 17-Aug-12 2:21am
///////////////////REMAINING CODE************

//parse json data
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jarray.length();i++){
jsonobject="" json_data="jArray.getJSONObject(i);
" log.i("log_tag","id:="" "+json_data.getint("userid")+
="" ",="" name:="" "+json_data.getstring("username")
="" );
="" get="" an="" output="" to="" the="" screen
="" returnstring="" +="\n\t" jarray.getjsonobject(i);=""
="" }
="" }catch(jsonexception="" e){
="" log.e("log_tag",="" "error="" parsing="" data="" "+e.tostring());
="" return="" returnstring;
="" a;
="" }

="" @override
="" public="" boolean="" oncreateoptionsmenu(menu="" menu)="" {
="" getmenuinflater().inflate(,="" menu);
="" true;
="" }


php="" file


="" mysql_connect("localhost","root","");

="" mysql_select_db("mydb");

="" $q="mysql_query("SELECT" *="" from="" users");

="" while($e="mysql_fetch_assoc($q))

" $output[]="$e;

" print(json_encode($output));
="" mysql_close();

this php file fetch data successfully in browser but does not in android application... I don't know the reason.

another question is: where do I put php file ?
I put php file in wamp/www .
Misbah1 17-Aug-12 2:32am
The output is the address of php file ..
Misbah1 17-Aug-12 11:36am
I opened the LogCat but there is no message

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900