2014-10-17 131 views
0
<?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); 
    } 

} 
} 

?> 

保护字符串doInBackground(字符串... PARAMS){// TODO自动生成方法存根不能从PHP文件中检索json_encode值到安卓

 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); 
      }*/ 


     } 

的文本文件包含正确的内容{ “成功”:1,“消息”:“插入查询成功”}

但我不能够检索它在android系统

+0

抱歉发布问题的非结构化方式。第一次 – 2014-10-17 07:54:16

+0

您可以尝试打印JSONObject json的结果吗?并有任何错误? – 2014-10-17 07:55:04

+0

检查回应,是否为空? – Shvet 2014-10-17 07:55:28

回答

0

这是没有意义的,首先做一个登录页使用g <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(); 
+0

如何从现有的响应中读取json? – 2014-10-17 08:34:22

+0

你在说什么是有道理的..所以我必须使用Httpresponse响应? – 2014-10-17 08:35:26

+0

只需从响应中读取json文本即可。 Se编辑。 – greenapps 2014-10-17 09:25:33

0

在我的情况。我用的是HTTP方法,然后getjsonfromURL从JSONParser类也对GET HTTP方法,并张贴

我认为有正在取得,因此有两个电话没有任何物品返回