无法从android studio中的php文件接收json输出

时间:2018-03-15 07:37:37

标签: php android

我试图从php文件接收数据,但在android中显示null,这意味着我无法从php接收数据

这是我的php

<?php
require "init.php";
error_reporting(0);
$name = $_POST["Name"];
$password = $_POST["Password"]; 

// $name = "rajesh";
// $password = "1995";

$Sql1 = "SELECT * FROM `user_info` WHERE `name`='".$name."' AND `password`='".$password."';";
$result1 = mysqli_query($con, $Sql1);
while($row = mysqli_fetch_array($result1))
{
    $user_id =  $row['id']; //Getting the employee id from user_info as $user_id
    $sql2 = "SELECT *FROM empdet WHERE Employeeid ='".$user_id."'";
    $result2 = $con->query($sql2);   
    if ($result2->num_rows > 0) 
    {
        // output data of each row
        while($row = $result2->fetch_assoc()) 
        {
            if($row["Event_id"] == '' && $row["Hours"] != '00:00:00') 
            {            
                $date=$row["ScheduleDate"]; 
                $start=$row["StartTime"]; 
                $end=$row["Endtime"];
                $New_Display_Schedule[] = array("ScheduleDate"=>$date,"StartTime"=>$start,"Endtime"=>$end); 

            }else
            {
                //echo "no new change";
            }       
        }

    }else
    {
        $response =1;
    }


}//End of while loop to check for new schedule for particular user

$sql1="SELECT * FROM empdet WHERE Employeeid ='".$user_id."'";
$result1 = $con->query($sql1);
if ($result1->num_rows > 0) 
{
    while($row = $result1->fetch_assoc()) 
    {
        if($row["Status"] == 'u')
        { 
            $date=$row["ScheduleDate"]; 
            $start=$row["StartTime"]; 
            $end=$row["Endtime"];
            $Update_Display_Schedule[] = array("ScheduleDate"=>$date,"StartTime"=>$start,"Endtime"=>$end);  

        }
    }
}else 
{
    //echo "no change";
} 
$NDS=$New_Display_Schedule; 
$UDS=$Update_Display_Schedule;   
echo json_encode(array("NEW_DISPLAY_SCHEDULE"=>$NDS,"UPDATE_DISPLAY_SCHEDULE"=>$UDS));
?>

这是我的json

{
 "NEW_DISPLAY_SCHEDULE":null,
 "UPDATE_DISPLAY_SCHEDULE":[
   {
    "ScheduleDate":"2018-03-16",
    "StartTime":"8:00 am",
    "Endtime":"9:00 pm"
   }
 ]
}

我的php文件输出上面并且工作正常,但是当我尝试从android检索输出json时,它显示为null

android活动

package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Login extends AppCompatActivity
{

    EditText name, password;
    String Name, Password;
    Context ctx=this;
    String NAME=null, PASSWORD=null, EMAIL=null;
    Context context=this;
    SharedPreferences sp;
    private static final String MY_PREFS_NAME = "myprefrences";
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        name = (EditText) findViewById(R.id.main_name);
        password = (EditText) findViewById(R.id.main_password);
    }

    public void main_login(View v)
    {
        Name = name.getText().toString();
        Password = password.getText().toString();
        BackGround b = new BackGround();
        b.execute(Name, Password);

        SharedPreferences sps = getSharedPreferences("SSSS", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sps.edit();
        editor.remove("result");
        editor.commit();

        context = getApplicationContext();
        sp = getSharedPreferences("identifier", context.MODE_PRIVATE);
        SharedPreferences.Editor Ed= sp.edit();
        Ed.putString("Name",name.getText().toString()).commit();
        Ed.putString("Password",  password.getText().toString()).commit();


    }

    class BackGround extends AsyncTask<String, String, String>
    {

        @Override
        protected String doInBackground(String... params)
        {
            String name = params[0];
            String password = params[1];
            String data="";
            int tmp;

            try
            {
                URL url = new URL("http://www.powersys-india.com/sample/Scheduler/Display_App_Sch.php");
                String urlParams = "name="+name+"&password="+password;

                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setDoOutput(true);
                OutputStream os = httpURLConnection.getOutputStream();
                os.write(urlParams.getBytes());
                os.flush();
                os.close();

                InputStream is = httpURLConnection.getInputStream();
                while((tmp=is.read())!=-1)
                {
                    data+= (char)tmp;
                }

                is.close();
                httpURLConnection.disconnect();
                return data;
            } catch (MalformedURLException e)
            {
                e.printStackTrace();
                return "Exception: "+e.getMessage();
            } catch (IOException e)
            {
                e.printStackTrace();
                return "Exception: "+e.getMessage();
            }
        }

        @Override
        protected void onPostExecute(String result) {
            //check for username and password is correct or not
            if(result.equals("USERNAME/PASSWORD IS INCORRECT"))
            {
                Toast.makeText(ctx , "USERNAME/PASSWORD IS INCORRECT", Toast.LENGTH_LONG).show();
            }
            //DATA CHECK
            if(result.equals("NO SCHEDULE FOUND"))
            {
                Toast.makeText(ctx, "NO SCHEDULE FOUND", Toast.LENGTH_LONG).show();
            }

            SharedPreferences  sp = getSharedPreferences("identifier", Context.MODE_PRIVATE);
            SharedPreferences.Editor Ed= sp.edit();
            Ed.putString("result", result  );
            Ed.commit();
            Toast.makeText(ctx, "RESULT"+result, Toast.LENGTH_LONG).show();


            //Intent k = new Intent(ctx, Main2Activity.class);
           // startActivity(k);

        }
    }
}

我无法通过onpostexecute方法

从php接收数据

android活动的输出。

output of android activity.

请有人帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

您的url params拼写错误,更改

String urlParams = "name="+name+"&password="+password;

String urlParams = "Name="+name+"&Password="+password;