[英]Manipulate JSON Object on Android from PHP/MySQL
这段代码可以正常工作...但是我不知道如何处理得到的结果。 在这里,我只是通过简单的查询从应用程序发送到网站
<?php
$con = mysql_connect("","user","pass") or die(mysql_error());
$objDB = mysql_select_db("tablename", $con) or die(mysql_error());
$username = mysql_real_escape_string($_POST['user1']);
$sql = mysql_query("SELECT * FROM doctor WHERE username = '$username' ");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>
这是我的Android代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Saved preferences
app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
button = (Button)findViewById(R.id.Button01);
username= (EditText)findViewById(R.id.EditText01);//et
password= (EditText)findViewById(R.id.EditText02);//et
echoresponse= (TextView)findViewById(R.id.tv);//tv
// String str_user = app_preferences.getString("username", "0");
// String str_pass = app_preferences.getString("password", "0");
// String str_check = app_preferences.getString("checked", "0");
// if(str_check.equals("yes"))
// {
// username.setText(str_user);
// //password.setText(str_pass);
// }
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://www.baemer.com/androiddealer.php"); // make sure the url is correct.
//add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("user1",username.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(new BasicNameValuePair("pass1",password.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response=httpclient.execute(httppost);
// edited by James from coderzheaven.. from here....
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String res = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + res);
echoresponse.setText("Response from PHP : " + res);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
System.out.println("Exception : " + e.getMessage());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine())!=null){
sb.append(line+"\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
System.out.println("Exception : " + e.getMessage());//if not try Log. from http://www.gaanza.com/blog/how-to-mysql/
}
//parse JSON data
try{
JSONArray jArray = new JSONArray(result);
for(int i =0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("dr_serial")+
", username: "+json_data.getString("username"));
}
}catch(JSONException e){
System.out.println("Exception : " + e.getMessage());//if not try Log. from http://www.gaanza.com/blog/how-to-mysql/
}
}
});
}
}
但是现在,当我按下按钮时,显示所有表都在Log.i(..... blabla)中说了。 如何操作这些列? 如例如:分配一些变量dr_serial,然后将其显示在文本视图或其他内容上。
刚打电话
int dr_serial = json_data.getInt("dr_serial");
然后,您可以使用它做任何您想做的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.