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.