0

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"]);
            }
     }

    }
3
  • 1
    Type mismatch: cannot convert from JSONObject to String in return statement. Commented Mar 4, 2014 at 9:12
  • if you want to return string then return json instead of jObj which is of type JSONObject in your doInBackground Commented Mar 4, 2014 at 9:27
  • you try return JsonObject on doInBackground but you define String as return statement, change one of those Commented Mar 4, 2014 at 9:29

2 Answers 2

1

obviously it is going to throw a Typemismatch you are returning a JSONObject and your onPostExecute() accepts String.

You need to change your AsyncTask like this

private class sendPostData extends AsyncTask<String, Void, JSONObject>

and onPostExecute() to this:

@Override
protected void onPostExecute(JSONObject result) {
    childname.settext(result.getstring("ChildPassportName"));
    childid.settext(result.getstring("ChildID"));
}

[EDIT] just need one more fix
change doInBackground() like this:

protected JSONObject doInBackground(String... params){
    ....
    ....
}

its body should remain as it is.
Hope it helps. :)

Sign up to request clarification or add additional context in comments.

4 Comments

In my doinbackground protected String doInBackground(String... params) retuned jobj(jsonobject ) ,,it oly shows error
Guys can you suggest with some simple working example
Actually this is my json output [{"ChildPassportName":"arun2","ChildID":"111"}] I have tried ur code ,,but it shows error in protected void onPostExecute(String result) { //View your result here. jObj = new JSONObject(result); childname.settext(jObj.getstring["ChildPassportName"]); childid.settext(jObj.getstring["ChildID"]); }
@ArunShankar updated my answer... you clearly need to work on your Java basics.
0

you try return JsonObject from doInBackground but you must return String. so you need change one of those. in bellow code i return JsonObject for you:

private class sendPostData extends AsyncTask<String, Void, JsonObject>
    {
            @Override
        protected JsonObject doInBackground(String... params) {  

                    // your code

                    json=sb.toString();
                    jObj = new JSONObject(json);
                    return jObj;

        }
           @Override
        protected void onPostExecute(JsonObject result) {
            //View your result here.
               childname.settext(result.getstring["ChildPassportName"]);
               childid.settext(result.getstring["ChildID"]);
            }
     }

or you must send String like follow:

private class sendPostData extends AsyncTask<String, Void, String>
        {
                @Override
            protected String doInBackground(String... params) {  

                        // your code

                        json=sb.toString();

                        return json;

            }
               @Override
            protected void onPostExecute(String result) {
                //View your result here.
                   jObj = new JSONObject(result);
                   childname.settext(jObj.getstring["ChildPassportName"]);
                   childid.settext(jObj.getstring["ChildID"]);
                }
         }

both is working

1 Comment

can you post your logcat error on your question or use first code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.