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!
Posted

1 solution

Use wampserver and use json code to connect php service to your database..
   
Comments
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.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
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.app.Activity;

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

public class MainActivity extends Activity {

TextView txt;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new1);

LinearLayout rootLayout = new LinearLayout(getApplicationContext());
txt = new TextView(getApplicationContext());
rootLayout.addView(txt);
setContentView(rootLayout);


txt.setText("Connecting...");

txt.setText(getServerData(KEY_121));
}


public static final String KEY_121 = "http://127.0.0.1/PAHMA.php"; //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
try{
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
try{
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");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}

/////*********continue**************
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
try{
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(r.menu.activity_main,="" menu);
="" true;
="" }

}

=""
php="" file
=""

<?php=""

="" 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 ..
e.g.

http://127.0.0.1/PAHMA.php
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