我已经尝试过这个代码。在onclick上工作正常,但在我的输出中我的json值没有显示在输出字段中,
但当我用childname.setText(result);
替换json代码时,我可以得到下面屏幕截图中显示的输出
我想分割这些值并在相应的输入字段中显示它。
建议plz ,,,,提前致谢
我的Javafile
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,child,id;
@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
valuess();
if(!user.equals("") & !pass.equals(""))
{
new sendPostData().execute();
}
else if(pass.equals("") & user.equals(""))
{
Toast toast=Toast.makeText(ChildProfile.this,"Enter Username & Password", Toast.LENGTH_LONG);
toast.show();
}
else if(user.equals("") )
{
Toast toast=Toast.makeText(ChildProfile.this,"Enter Username", Toast.LENGTH_LONG);
toast.show();
}
else if(pass.equals(""))
{
Toast toast=Toast.makeText(ChildProfile.this,"Enter Password", Toast.LENGTH_LONG);
toast.show();
}
}
});
}
private class sendPostData extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params) {
try{
valuess();
String link="http://192.168.1.6:81/arun/value.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;
}
return sb.toString();
}catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
//View your result here.
/* if(result.equals("wrong"))
{
Toast toast=Toast.makeText(ChildProfile.this,"Invalid account.Enter correct username and password or Please SignUp", Toast.LENGTH_LONG);
toast.show();
}
else
{
Toast toast=Toast.makeText(ChildProfile.this,"Invalid account.Enter correct username and password or Please SignUp", Toast.LENGTH_LONG);
toast.show();
}
json=result;
try {
jObj = new JSONObject(result);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
child=jObj.getString("ChildPassportName");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
id=jObj.getString("ChildID");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
childid.setText(id);
Toast toast=Toast.makeText(ChildProfile.this,"Invalid account.Enter correct username and password or Please SignUp", Toast.LENGTH_LONG);
toast.show();*/
childname.setText(result);
}
}
public void valuess()
{
user = username.getText().toString();
pass = password.getText().toString();
}
}
我的PHP文件
<?php
$con=mysql_connect("localhost","arun","sachin11");
$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);
?>
php输出
[{"ChildPassportName":"Arun","ChildID":"0"}]
答案 0 :(得分:0)
试试这个..
您的结果为 JSONArray ,因此您必须解析它。
@Override
protected void onPostExecute(String result) {
String id = "";
String name = "";
try {
JSONArray new_array = new JSONArray(result);
JSONObject obj = new_array.getJSONObject(0);
id = obj.getString("ChildID"):
name = obj.getString("ChildPassportName"):
}catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
childid.setText(id);
childname.setText(name);
}