Android从php获取json数据显示错误....!

时间:2014-03-04 09:08:11

标签: php android mysql json

通过php从mysql数据库访问数据, 在我的代码中,它显示错误,无法将字符串转换为json对象。 而且我不知道如何在android中获取json值。 什么都是这里的错误,,,建议plz ,,,

我的php文件

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

    }

2 个答案:

答案 0 :(得分:1)

显然它会抛出一个Typemismatch你返回JSONObject而你的onPostExecute()接受String

您需要像这样更改AsyncTask

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

onPostExecute()对此:

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

[编辑] 只需要再修一次 像这样更改doInBackground()

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

它的身体应该保持不变。 希望它有所帮助。 :)

答案 1 :(得分:0)

您尝试从JsonObject返回doInBackground,但您必须返回String。所以你需要改变其中一个。在下面的代码中,我将为您返回JsonObject

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

或者你必须发送如下的字符串:

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

两者都在运作