1

I have been working to connect MySql database to my android app and searched on internet. Some of the solutions worked with one problem. The problem is that the function returns data in the form which is String and the content of String is :

[{"id":1,"username":"admin","password":"root","email":"admin@localhost.com","fullname":"Site Admin"}]

But I want the data in form of some sort of array through which I can get all the user data like email , fullname separately. I tried to implement a function but that didn't work. Following is the code:

BackgroundWorker.java

public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
String tresult;


BackgroundWorker (Context ctx){
    context = ctx;
}
@Override
protected String doInBackground(String... params) {
    String type = params[0];
    String username = params[1];
    String password = params[2];

    String login_url = "http://192.168.1.18/myproj/api/query1.php";
    if (type.equals("login")){
        try {
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
            String post_data = URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"+
                    URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();

            outputStream.close();

            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
            String line="";
            String result="";

            while((line = bufferedReader.readLine())!= null){
                result+=line;
            }

            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();

            return result;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle("Login Status");
}

@Override
protected void onPostExecute(String result) {
    alertDialog.setMessage(result);
    alertDialog.show();
    tresult = result;
    //return tresult;
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}


}

BackgroundWorker is a class which works in background and connect with database. And in MainActivity it is called as following:

MainActivity

public void onLogin(View view, String user, String pass){
    String type = "login";
    BackgroundWorker backgroundWorker = new BackgroundWorker(this);
    backgroundWorker.execute(type,user,pass);
}

Calling of function:

        loginbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String username = user_edittext.getText().toString();
            String password = pass_edittext.getText().toString();
            onLogin(v,username,password);


        }
    });

In short I want to return the variable result or tresult from protected void onPostExecute(String result) in my MainActivity and convert it to Array. Thankyou! waiting for experts opinion.

Imran Aslam
  • 208
  • 2
  • 15
  • "I want the data in form of some sort of array through which I can get all the user data like email , fullname separately. I tried to implement a function but that didn't work. "..You cannot do that from front end(i.e android/java).You have to write the server side coding in that way – kgandroid Nov 01 '16 at 07:14
  • from back end i.e php it returns associative array: `$result = $sql->fetchAll(PDO::FETCH_ASSOC); echo json_encode($result);`. I can separate details in php but in android, still it will give me in one string. – Imran Aslam Nov 01 '16 at 07:17

1 Answers1

0

It seems you have two issues:

  1. Converting a Json string to a more friendly format
  2. Returning this friendly format to another activity

on 1: There is no built in way to parse JSON strings in Java but lots of easy library options available - take a look at How to parse JSON in Java where these are discussed.

on 2: In general data is converted to a bundle and returned to your main activity as part of the intent system

Community
  • 1
  • 1
Elemental
  • 7,365
  • 2
  • 28
  • 33