gpt4 book ai didi

java - 从 PHP/MySQL 在 Android 上操作 JSON 对象

转载 作者:行者123 更新时间:2023-11-30 04:28:42 25 4
gpt4 key购买 nike

此代码运行完美...但我不知道如何操作我得到的结果。在这里,我只是通过一个简单的查询从应用程序发送到网站

<?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();
?>

这是我的安卓代码:

    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,然后将其显示在 TextView 或其他东西上。

最佳答案

打电话

int dr_serial = json_data.getInt("dr_serial");

然后你就可以用它做任何你想做的事情了。

关于java - 从 PHP/MySQL 在 Android 上操作 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8076151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com