无法将php文件中的json_编码值检索到android中
受保护的字符串doInBackgroundString。。。params{ //TODO自动生成的方法存根无法将php文件中的json_编码值检索到android中,php,android,json,Php,Android,Json,受保护的字符串doInBackgroundString。。。params{ //TODO自动生成的方法存根 <?php require 'connection.php'; $response = array(); $response_array = array(); $insert_query=0; if(isset($_POST["username"]) && isset($_POST["password"]) && isset($
<?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);
}
}
}
?>
文本文件包含正确的内容{success:1,message:Insert Query Successful}
但是我无法在android中检索到它,在我的例子中,首先在中使用url登录是没有意义的。我使用的是Http方法,然后是来自JSONParser类的getjsonfromURL,该类还有get和post的Http方法
我认为有两个调用正在进行,因此没有返回任何对象很抱歉以非结构化的方式发布问题。第一次您可以尝试打印JSONObject json的结果吗?是否有任何错误?检查响应,是否为空?调试并查看json对象是否符合您的预期。当我打印日志cat显示的响应org.apache.http.message。BasicHttpResponse@b239def8what你的意思是有道理的。所以我必须使用Httpresponse响应?你提供的解决方案工作得很好,但使用你的方法,我在android上获得的数据是一个字符串。如果我想要php文件中的响应数组s一个json对象我能做什么?不php脚本不包含json对象或数组。它包含php变量,它们作为json编码的文本进行回显。因此,您接收字符串形式的json文本。但是有了json文本,您现在可以使用json类来处理json文本并检索对象。
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);
}*/
}
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();