gpt4 book ai didi

php - 使用 JSON 发送和读取

转载 作者:行者123 更新时间:2023-11-29 08:16:09 25 4
gpt4 key购买 nike

我的Android应用程序根据从应用程序发送的用户名和密码从PHP文件中读取JSON数据,也以JSON形式发送。
我现在所达到的目标是只能通过指定用户名和密码来读取,不能发送用户名和来自应用程序的密码发生..

我的问题是,是否可以使用 JSONParse 读取并发布?
请看一下 JSONParse 方法,我添加了一个已注释的部分,因为它不起作用“是否可以使用 JSON 从数据库发送用户名和密码以及读取状态”

JSONParse

private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();

pDialog = new ProgressDialog(fishtank.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();

}

@Override
protected JSONObject doInBackground(String... args) {

/*Temp
SharedPreferences settings = getSharedPreferences("mySettings", Activity.MODE_PRIVATE);
us = settings.getString("storedWifiUser", "");
ps = settings.getString("storedWifiPass", "");
try {
JSONObject json = new JSONObject();
json.put("user", us);
json.put("pass", ps);

postData(json);


} catch (JSONException e) {
e.printStackTrace();} Temp */
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}

public void postData(JSONObject json) throws JSONException {
HttpClient httpclient = new DefaultHttpClient();

try {
HttpPost httppost = new HttpPost(url);

List<NameValuePair> nvp = new ArrayList<NameValuePair>(2);
nvp.add(new BasicNameValuePair("json", json.toString()));
//httppost.setHeader("Content-type", "application/json");
httppost.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse response = httpclient.execute(httppost);
Log.i("JSON Response : ",json.toString().trim());
if(response != null) {
InputStream is = response.getEntity().getContent();
//input stream is response that can be shown back on android
}

}catch (Exception e) {
e.printStackTrace();
}
}
//Temp
@Override

protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Log.i("JSON Response : ",json.toString().trim());
// Log.i("JSON Response : "+json.toString().trim());

// System.out.println("JSON Response : "+json.toString().trim());
JSONObject c = json.getJSONObject("status");
String tog1="";
String tog2="";
String tog3="";
if(c.has("fil"))

tog1 = c.getString("fil");

if(c.has("HEA"))
tog2 = c.getString("HEA");

if(c.has("LED"))
tog3 = c.getString("LED");

Log.i("JSON Response : ",json.toString().trim());
if(tog1.equals("ON"))
{ toggle1.setChecked(true);}
else{ toggle1.setChecked(false);}
if(tog2.equals("ON"))
{ toggle2.setChecked(true);}
else{ toggle2.setChecked(false);}
if(tog3.equals("ON"))
{ toggle3.setChecked(true);}
else{toggle3.setChecked(false);}

} catch (JSONException e) {
e.printStackTrace();
}

}






}//End Json

PHP 文件

<?php

$con=mysqli_connect("localhost","root","123","pet_home");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// if(isset($_POST['json'])){
// $json=$_POST['json'];
// $data2=json_decode($json,TRUE);
// $u=$data2->{'user'};
// $p=$data2->{'pass'};
$result = mysqli_query($con,"SELECT * FROM users WHERE username='osama' AND password='123'");
$row_cnt = mysqli_num_rows($result);
//$row_cnt=1;
if($row_cnt>0){
$row = mysqli_fetch_array($result);
$data = array('success'=>true, 'error'=>'', 'status'=>array("fil" => $row['filter_st'], "HEA"=> $row['heat_st'], "LED" =>$row['led_st']));
}else{
$data = array('success'=>false, 'error' => 'No records found');
}

// }else{
// $data = array('success'=>false, 'error' => 'No POST value from Android App');
// }

echo json_encode($data);

mysqli_close($con);


?>

最佳答案

使用它来保存到数据库

nameValuePairs2 = new  ArrayList<NameValuePair>();
nameValuePairs2.add(new BasicNameValuePair("username",username));
nameValuePairs2.add(new BasicNameValuePair("password",password));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(mainurl+"registration.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String the_string_response = convertResponseToString(response);
// Toast.makeText(getApplicationContext(), "Response " + the_string_response, Toast.LENGTH_LONG).show();
}catch(Exception e){
// Toast.makeText(getApplicationContext(), "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
// System.out.println("Error in http connection "+e.toString());
}

关于php - 使用 JSON 发送和读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20562419/

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