无法从php文件中检索json_encode值到android

时间:2014-10-17 07:52:58

标签: php android json

 <?php 

 require 'connection.php';

 $response = array();

 $response_array = array();

 $insert_query=0;

 if(isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["phone"]))
{
 $username = $_POST["username"];
 $password = $_POST["password"];
 $phone = $_POST["phone"];

 $check_existing_user = mysql_query("select * from eh_users where username like '".$username."'");



if(mysql_num_rows($check_existing_user)==0)
    {


 $insert_query = mysql_query("INSERT INTO eh_users(username, password, phone)           VALUES('$username','$password','$phone')");


 if($insert_query==1)
    {
        $response["success"]=1;

        $response["message"]="Insert Query Successful";

        file_put_contents("myFile.txt", json_encode($response));

        echo json_encode($response);

    }   

else
    {
        $response["success"]=0;

        $response["message"]="Error : Query not successful";

        echo json_encode($response);
    }

}
}

?>

protected String doInBackground(String ... params){             // TODO自动生成的方法存根

        try
        {
            String url="http://www.iloveexpressions.com/eh/signUp.php";

            httpclient = new DefaultHttpClient();
            httppost = new HttpPost(url);
            nameValuePairs = new ArrayList<NameValuePair>(3);

            Log.d("Parameters ", username+" "+password+" "+phone);

            nameValuePairs.add(new BasicNameValuePair("username", username.trim()));
            nameValuePairs.add(new BasicNameValuePair("password", password.trim()));
            nameValuePairs.add(new BasicNameValuePair("phone", phone.trim()));

            Log.d("name value pairs", nameValuePairs.toString());

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            response = httpclient.execute(httppost);

            JSONObject json = jsonParser.getJSONFromUrl(url);

            int success = json.getInt("success");

            //JSONArray jArray = json.getJSONArray("success");

            Log.d("Json object : ",success+"");

            /*for(int i = 0; i < jArray.length(); i++ )
            {
                JSONObject c = jArray.getJSONObject(i);

                String success = c.getString("success");

                String message = c.getString("message");

                Log.d("Sucess : ", success);

                Log.d("Message : ", message);
            }*/


        }

文本文件包含正确的内容{&#34;成功&#34;:1,&#34;消息&#34;:&#34;插入查询成功&#34;}

但我无法在android

中检索它

2 个答案:

答案 0 :(得分:0)

首先使用<new HttpPost.(url)后跟httpclient.execute(httppost)的网址登录并使用getJSONFromUrl(url);再次使用该网址是没有意义的。相反,你应该从现有的响应中读取json。

HttpResponse response = httpClient.execute(httpPost);

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));

StringBuilder sb = new StringBuilder();

String line;
while ((line = reader.readLine()) != null) 
    {
    sb = sb.append(line);
    }

String jsonText =  sb.toString();

答案 1 :(得分:0)

在我的情况下..我正在使用Http方法,后跟来自JSONParser类的getjsonfromURL,它也有用于获取和发布的Http方法

我认为有两个调用正在进行,因此没有对象返回