0
votes
 <?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 Auto-generated method stub

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


        }

the text file contains the right content {"success":1,"message":"Insert Query Successful"}

but i am not able to retrieve it in android

2
sorry for the unstructured way of posting the question . First TimeHarshil Shah
could you try to print what is the result of JSONObject json? and is there any error?Niko Adrianus Yuwono
check response, is it null or not?Shvet
Debug and see what the json object is what you expected or not.Francesco verheye
when i print the response the log cat shows this org.apache.http.message.BasicHttpResponse@b239def8Harshil Shah

2 Answers

0
votes

It makes no sense to first do a log in using an url in <new HttpPost.(url) followed by a httpclient.execute(httppost) and use that url again with getJSONFromUrl(url);. Instead you should read the json from the existing response.

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
votes

In my case .. i was using the Http methods followed by getjsonfromURL from the JSONParser class which also had the Http methods for get and post

I think there were two calls being made and thus there was no object getting returned