在我的PHP文件中。我通过Json将数据发布到android studio。我不 知道什么原因导致我的数据为空的原因 我的android应用。
<?php
session_start();
include "connection.php";
//include "header.php";
$response = array();
$propertyType = $_POST["propertyType"];
$placeType = $_POST["placeType"];
$guestType = $_POST["guestType"];
$location = $_POST["location"];
$size = $_POST["size"];
$age = $_POST["age"];
$boarder = $_POST["boarder"];
$bedroom = $_POST["bedroom"];
$bedType = $_POST["bedType"];
$bedCount = $_POST["bedCount"];
$bathroom = $_POST["bathroom"];
$streetAdd = $_POST["streetAdd"];
$subdAdd = $_POST["subdAdd"];
$cityAdd = $_POST["cityAdd"];
$building_amenities = $_POST["building_amenities"];
$unit_amenities = $_POST["unit_amenities"];
$safety_amenities = $_POST["safety_amenities"];
$pet = $_POST["pet"];
$name = $_POST["name"];
$description = $_POST["description"];
$cover_photo = $_POST["cover_photo"];
// $more_photo = $_POST["more_photo"];
// $count = $_POST["more_photo_count"]; //count of image
$rules = $_POST["rules"];
$abouts = $_POST["abouts"];
$price = $_POST["price"];
$status = "Pending";
$user_id = $_POST["user_id"];
$arr = array();
// $num_count = (int)$count;
//
// $json = file_get_contents('php://input');
// $obj = json_decode($json);
// for($i = 0; $i < $num_count; $i++){
//
// $arr[i] = $obj->{'photo'+i};
// }
if($conn)
{
$res = mysqli_query($conn,"insert into apartments (propertyType,placeType,guestType,location, size, age, boarder,bedroom,bedType,bedCount,bathroom,streetAdd,subdAdd,cityAdd,building_amenities,unit_amenities,safety_amenities,pet,name,
description,cover_photo,rules,abouts,price, user_id, status) values ('$propertyType','$placeType','$guestType','$location','$size','$age','$boarder','$bedroom','$bedType','$bedCount','$bathroom',
'$streetAdd','$subdAdd','$cityAdd','$building_amenities','$unit_amenities','$safety_amenities','$pet','$name','$description',
'$cover_photo','$rules','$abouts','$price','$user_id','$status')");
if($res)
{
$res1 = mysqli_query($conn,"Select * from apartments where name = '$name' && user_id = '$user_id'");
if (mysqli_num_rows($res1) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($res1)) {
$user_id = $row['user_id'];
$desc = $row['description'];
$status = $row['status'];
}
$response["user_id"] = $user_id;
$response['description'] = $desc;
$response['status'] = $status;
echo json_encode(array( "myArray" =>$response));
}
} else
die(mysqli_error($conn));
}
else
{
echo "Not Connected";
}
?>
这是我的android文件,我将在其中使用获取的数据 从PHP。这也是我获取我所值的android文件 发布到我的PHP上面。如您所见,我为PHP带来了很多价值, 因为我通过PHP将所有内容保存到了Mysql数据库中。我希望 您可以帮助我再次检索此值,因此我可以将其用于 下一个活动。
public class Conn_postApartment extends AsyncTask<String[],Void,String> {
Context context;
AlertDialog alertDialog;
private static final String UPLOAD_URL = "//192.168.43.205/ITSP/insert_image.php";
public Conn_postApartment(Context context)
{
this.context = context;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Post Status");
}
@Override
protected void onPostExecute(String s) {
alertDialog.setMessage(s);
alertDialog.show();
}
@Override
protected String doInBackground(String[]... voids) {
String user_id = "";
String trial = "";
Uri filePath;
URL urlcover = null;
// String loginURL="https://public-api.wordpress.com/rest/v1/sites/hackpundit.com/posts?number=5&page=1&type=post&fields=id,title,URL";
String[] step1Array = voids[0];
String[] imageArray = voids[1];
String[] step2Array = voids[2];
String[] step3Array = voids[3];
int c = imageArray.length;
String newuri = step2Array[2];
String count = Integer.toString(c);
String photo = "";
try {
urlcover = new URL("http://apartment" + newuri);
}catch (MalformedURLException e){
System.out.println("Malformed URL: " + e.getMessage());
}
try {
JSONObject jsonObject = new JSONObject();
for (int i = 0; i < imageArray.length; i++) {
jsonObject.put("photo" + i, imageArray[i]);
}
photo = jsonObject.toString();
} catch (JSONException e){
}
String conn = "http://192.168.43.205/ITSP/new_apartment.php";
try {
URL url = new URL(conn);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setDoInput(true);
http.setDoOutput(true);
OutputStream ops = http.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ops, "UTF-8"));
String data = URLEncoder.encode("propertyType","UTF-8")+"="+URLEncoder.encode(step1Array[0],"UTF-8")
+"&&"+URLEncoder.encode("placeType","UTF-8")+"="+URLEncoder.encode(step1Array[1],"UTF-8")
+"&&"+URLEncoder.encode("guestType","UTF-8")+"="+URLEncoder.encode(step1Array[2],"UTF-8")
+"&&"+URLEncoder.encode("location","UTF-8")+"="+URLEncoder.encode(step1Array[3],"UTF-8")
+"&&"+URLEncoder.encode("size","UTF-8")+"="+URLEncoder.encode(step1Array[4],"UTF-8")
+"&&"+URLEncoder.encode("age","UTF-8")+"="+URLEncoder.encode(step1Array[5],"UTF-8")
+"&&"+URLEncoder.encode("boarder","UTF-8")+"="+URLEncoder.encode(step1Array[6],"UTF-8")
+"&&"+URLEncoder.encode("bedroom","UTF-8")+"="+URLEncoder.encode(step1Array[7],"UTF-8")
+"&&"+URLEncoder.encode("bedType","UTF-8")+"="+URLEncoder.encode(step1Array[8],"UTF-8")
+"&&"+URLEncoder.encode("bedCount","UTF-8")+"="+URLEncoder.encode(step1Array[9],"UTF-8")
+"&&"+URLEncoder.encode("bathroom","UTF-8")+"="+URLEncoder.encode(step1Array[10],"UTF-8")
+"&&"+URLEncoder.encode("streetAdd","UTF-8")+"="+URLEncoder.encode(step1Array[11],"UTF-8")
+"&&"+URLEncoder.encode("subdAdd","UTF-8")+"="+URLEncoder.encode(step1Array[12],"UTF-8")
+"&&"+URLEncoder.encode("cityAdd","UTF-8")+"="+URLEncoder.encode(step1Array[13],"UTF-8")
+"&&"+URLEncoder.encode("building_amenities","UTF-8")+"="+URLEncoder.encode(step1Array[14],"UTF-8")
+"&&"+URLEncoder.encode("unit_amenities","UTF-8")+"="+URLEncoder.encode(step1Array[15],"UTF-8")
+"&&"+URLEncoder.encode("safety_amenities","UTF-8")+"="+URLEncoder.encode(step1Array[16],"UTF-8")
+"&&"+URLEncoder.encode("pet","UTF-8")+"="+URLEncoder.encode(step1Array[17],"UTF-8")
+"&&"+URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(step2Array[0],"UTF-8")
+"&&"+URLEncoder.encode("description","UTF-8")+"="+URLEncoder.encode(step2Array[1],"UTF-8")
+"&&"+URLEncoder.encode("cover_photo","UTF-8")+"="+URLEncoder.encode(urlcover.toString(),"UTF-8")
// +"&&"+URLEncoder.encode("more_photo","UTF-8")+"="+URLEncoder.encode(photo,"UTF-8")
// +"&&"+URLEncoder.encode("more_photo_count","UTF-8")+"="+URLEncoder.encode(count,"UTF-8")
+"&&"+URLEncoder.encode("rules","UTF-8")+"="+URLEncoder.encode(step3Array[0],"UTF-8")
+"&&"+URLEncoder.encode("user_id","UTF-8")+"="+URLEncoder.encode("shaira","UTF-8") //user_id
+"&&"+URLEncoder.encode("abouts","UTF-8")+"="+URLEncoder.encode(step3Array[1],"UTF-8")
+"&&"+URLEncoder.encode("price","UTF-8")+"="+URLEncoder.encode(step3Array[2],"UTF-8");
writer.write(data);
writer.flush();
writer.close();
ops.close();
InputStream ips = http.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ips,"ISO-8859-1"));
String line = "";
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(http.getInputStream()));
//A simple string to read values from each line
String json;
try {
//reading until we don't find null
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
JSONTokener result = new JSONTokener(sb.toString());
JSONObject jObject = new JSONObject(result);
String samp = result.toString().trim();
// trial = jObject.toString();
JSONObject obj= new JSONObject(result);
JSONArray array=obj.getJSONArray("myArray");
jObject = array.getJSONObject(0);
user_id = jObject.getString("user_id");
trial = user_id;
}catch (IOException err){
trial = err.getMessage() + "IO";
}catch (JSONException err){
trial = err.getMessage() + user_id;
}
} catch (MalformedURLException e) {
trial = e.getMessage() + "Malform";
} catch (IOException e) {
trial = e.getMessage();
}
// catch (JSONException e) {
// status = e.getMessage();
// }
return trial;
}
}
错误被JSONException捕获,其中指出 在(“ myArray”:{“ user_id”:“ myName”,“ description”,“ desciptionssss”,“ status”:“ Pending”}}}
的字符74处的输入结束