1

Okay I am new to json and php so youll consider my question simple for you.

I am trying to insert data (name and id) from the app into the database . and I got this error

org.json.JSONException: Value <br><table of type java.lang.String cannot be converted to JSONObject

this is my php

<?php
    $host='mysql12.000webhost.com';
    $uname='a6901827_moudiz';
    $pwd='**';
    $db="a6901827_justed";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);


if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$id=$_REQUEST['id'];
    $name=$_REQUEST['name'];

$sql = 'INSERT INTO samle '.
       '(id ,name) '.
       'VALUES ($id , $name )';

mysql_select_db('a6901827_justed');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}


mysql_close($conn);
?>

and this is my code, if you think it needs improvement please don't hesitate.

public void postData(String valueIWantToSend) {
            // Create a new HttpClient and Post Header
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("id",id));
            nameValuePairs.add(new BasicNameValuePair("name",name));

            try
            {
            HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost =new HttpPost("http://justedhak.comlu.com/insert.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost); 
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                Log.e("pass 1", "connection success ");
        }
            catch(Exception e)
        {
                Log.e("Fail 1", e.toString());
                Toast.makeText(getApplicationContext(), "Invalid IP Address",
                Toast.LENGTH_LONG).show();
        }     

            try
            {
                BufferedReader reader = new BufferedReader
                (new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null)
            {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
            Log.e("pass 2", "connection success ");
        }
            catch(Exception e)
        {
                Log.e("Fail 2", e.toString());
        }     

2 Answers 2

2

Based on the error, you are trying to pass HTML formatted text in JSON string.

If you expect to pass plain text, this means something is wrong with your PHP script which returns HTML (or it doesn't work as expected).

If you want to pass HTML-formatted text, you can encode your string before adding it to your JSON object.

Sign up to request clarification or add additional context in comments.

2 Comments

I just want to insert the data into the table. what should i do ?. should i remove the json in the php and android code?
My PHP knowledge is basic. If you want to pass data from server to your client, then every value you are adding in your JSON string from your PHP script needs to be encoded beforehand. This way you are stepping away from HTML tags in JSON issues. If you want to pass JSON data from your client to your server/db, then whenever you create your JSON in your client, any value needs to be encoded by wrapping it in URLEncoder.encode(String, String
1

You are sending data as: http://justedhak.comlu.com/insert.php?id=x&name=myname

But expecting it to be {"id": x, "name": "myname"}

which should be in the post body of the http call.

The easiest way would be to forget the JSON for now. Keep the Android code, and remove the JSON part from the PHP.

your reply does not contain JSON as well. So the exception fires at the Android code.

3 Comments

I edit the question , I removed the json's , however the data is not inserting
$host='mysql12.000webhost.com'; $uname='a6901827_moudiz'; $pwd='**'; $db="a6901827_justed"; $conn = mysql_connect($dbhost, $dbuser, $dbpass); are these the same from the code? if so, your variables are different!
yes you are right , I fixed that. however I am facign another problem now related to json encoding can yo help me with that ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.