Am accessing datas from mysql database via php,, In my code it shows error on cannot convert string to json object. And as well as i dont know how to get json values in android. What all is the errors here ,,,Suggestions plz,,,
My php file
<?php
$con=mysql_connect("localhost","arun","sachin11");
$rows = array();
$db_select = mysql_select_db('Schoolapp', $con);
$username = $_POST['username'];
$password = $_POST['password'];
$result = mysql_query("SELECT ChildPassportName,ChildID FROM SchoolDB where Username='$username' and Password='$password'",$con);
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode($rows);
mysql_close($con);
?>
My ChildProfile.java
public class ChildProfile extends Activity {
private TextView childname,childid;
private Button get;
private EditText username,password;
private JSONObject jObj;
private static String user,pass,json;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.child_profile);
username=(EditText) findViewById(R.id.profile_username);
password=(EditText) findViewById(R.id.profile_password);
childname=(TextView) findViewById(R.id.profile_email);
childid=(TextView) findViewById(R.id.profile_childid);
get=(Button) findViewById(R.id.profile_button1);
get.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new sendPostData().execute();
}
});
}
private class sendPostData extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params) {
user=username.getText().toString();
pass=password.getText().toString();
String link="http://192.168.1.22:81/arun/new.php?";
String data = URLEncoder.encode("username", "UTF-8")
+ "=" + URLEncoder.encode(user, "UTF-8");
data += "&" + URLEncoder.encode("password", "UTF-8")
+ "=" + URLEncoder.encode(pass, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter
(conn.getOutputStream());
wr.write( data );
wr.flush();
BufferedReader reader = new BufferedReader
(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
json=sb.toString();
jObj = new JSONObject(json);
return jObj;
}
@Override
protected void onPostExecute(String result) {
//View your result here.
childname.settext(result.getstring["ChildPassportName"]);
childid.settext(result.getstring["ChildID"]);
}
}
}
JsonObjectondoInBackgroundbut you define String as return statement, change one of those