java.lang.String cannot be converted to JSONObject

Guru Charan

Below is my code for accessing the database and I am getting the following error:

org.json.JSONException: Value

       public void onClick(View v) {
            // TODO Auto-generated method stub

            tim = time.getText().toString();
            na = name.getText().toString();
            da = date.getText().toString();

            insert();
        }
    });
}}

public void insert()
{
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    nameValuePairs.add(new BasicNameValuePair("time",tim));
    nameValuePairs.add(new BasicNameValuePair("name",na));
    nameValuePairs.add(new BasicNameValuePair("date",da));

    try
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2/mts.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());
    }

    try
    {
        JSONObject json_data = new JSONObject(result);
        code=(json_data.getInt("code"));

        if(code==1)
        {
            Toast.makeText(getBaseContext(), "Inserted Successfully",
                    Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(getBaseContext(), "Sorry, Try Again",
                    Toast.LENGTH_LONG).show();
        }
    }
    catch(Exception e)
    {
        Log.e("Fail 3", e.toString());
    }
}

This the php code Errors are:

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO

and

Warning: mysql_connect() line 7

<?php
    $host='127.0.0.1';
    $uname='root';
    $pwd='password';
    $db="android";

enter code here

$con = MySQLi($host,$uname,$pwd) or die("connection failed");
    mysql_select_db($db,$con) or die("db selection failed");

    $id=$_REQUEST['time'];
    $name=$_REQUEST['name'];
    $date=$_REQUEST['date'];

    $flag['code']=0;

    if($r=mysql_query("insert into sample values('$time','$name',$'date') ",$con))
    {
        $flag['code']=1;
        echo"";
    }

    print(json_encode($flag));
    mysql_close($con);
?>

logcat

548-548/com.example.gurucharan.mts D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
548-548/com.example.gurucharan.mts D/dalvikvm﹕ GC_FOR_ALLOC freed 123K, 3% free 9218K/9415K, paused 60ms
548-548/com.example.gurucharan.mts I/dalvikvm-heap﹕ Grow heap (frag case) to 12.827MB for 3940072-byte allocation
548-554/com.example.gurucharan.mts I/dalvikvm﹕ threadid=3: reacting to signal 3
548-551/com.example.gurucharan.mts D/dalvikvm﹕ GC_CONCURRENT freed 1K, 2% free 13065K/13319K, paused 5ms+16ms
548-554/com.example.gurucharan.mts I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
548-554/com.example.gurucharan.mts I/dalvikvm﹕ threadid=3: reacting to signal 3
548-554/com.example.gurucharan.mts I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
548-554/com.example.gurucharan.mts I/dalvikvm﹕ threadid=3: reacting to signal 3
548-554/com.example.gurucharan.mts I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
548-554/com.example.gurucharan.mts I/dalvikvm﹕ threadid=3: reacting to signal 3
548-554/com.example.gurucharan.mts I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
548-548/com.example.gurucharan.mts D/libEGL﹕ loaded /system/lib/egl/libGLES_android.so
548-548/com.example.gurucharan.mts D/libEGL﹕ loaded /system/lib/egl/libEGL_emulation.so
548-548/com.example.gurucharan.mts D/﹕ HostConnection::get() New Host Connection established 0xd77e8, tid 548
548-548/com.example.gurucharan.mts D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_emulation.so
548-548/com.example.gurucharan.mts D/libEGL﹕ loaded /system/lib/egl/libGLESv2_emulation.so
548-554/com.example.gurucharan.mts I/dalvikvm﹕ threadid=3: reacting to signal 3
548-554/com.example.gurucharan.mts I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
548-548/com.example.gurucharan.mts W/EGL_emulation﹕ eglSurfaceAttrib not implemented
548-548/com.example.gurucharan.mts D/OpenGLRenderer﹕ Enabling debug mode 0
548-554/com.example.gurucharan.mts I/dalvikvm﹕ threadid=3: reacting to signal 3
548-554/com.example.gurucharan.mts I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
548-554/com.example.gurucharan.mts I/dalvikvm﹕ threadid=3: reacting to signal 3
548-554/com.example.gurucharan.mts I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
548-554/com.example.gurucharan.mts I/dalvikvm﹕ threadid=3: reacting to signal 3
548-554/com.example.gurucharan.mts I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
548-554/com.example.gurucharan.mts I/dalvikvm﹕ threadid=3: reacting to signal 3
548-554/com.example.gurucharan.mts I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
548-554/com.example.gurucharan.mts I/dalvikvm﹕ threadid=3: reacting to signal 3
548-554/com.example.gurucharan.mts I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
548-548/com.example.gurucharan.mts E/pass 1﹕ connection success
548-548/com.example.gurucharan.mts E/pass 2﹕ connection success
548-548/com.example.gurucharan.mts E/Fail 3﹕ org.json.JSONException: Value

Jamil

See how i am using php coding for insertion

<?php
$host="host"; //replace with database hostname
$username="example"; //replace with database username
$password="examplepass"; //replace with database password
$db_name="empinfo"; //replace with database name
 // i used mysql_connect
$con = mysql_connect($host,$username,$password) or die("connection failed");
mysql_select_db($db_name,$con) or die("db selection failed");

if(isset($_REQUEST)){
$topic=$_REQUEST['topic'];
$text=$_REQUEST['picname'];
$aid=$_REQUEST['aid'];
$date=$_REQUEST['date'];}

$texttt = "Picture";
$flag['code']=0;
$last_ID = 0;
$adminpic = "ASd";

// also mentioned the fields value of table in insert query

if($r=mysql_query("INSERT INTO `UPicture` (`ID` ,`AID` ,`ImageName` ,`Topic` ,`Date` ) VALUES (NULL, '$aid', '$text', '$topic', '$date'); ",$con))
{
$flag['code']=1;
$last_ID = mysql_insert_id();
}

$r=mysql_query("INSERT INTO `front_list` (`ID` ,`AID` ,`posttype` ,`date` ,`MID` ,`Adminpic`)VALUES (NULL , '$aid', '$texttt', '$date', '$last_ID', '$adminpic'); ",$con);

echo(json_encode($flag));
mysql_close($con);
?>

NOTE:

for new version of php add this line after <?php snippet

error_reporting(E_ALL ^ E_DEPRECATED);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Cannot convert string to JSONObject

분류에서Dev

Error org.json.JSONException: Value 1 at error of type java.lang.Integer cannot be converted to boolean

분류에서Dev

JSONArray java.lang.String을 JSONObject로 변환 할 수 없습니다

분류에서Dev

JSONObject의 java.lang.NullPointerException

분류에서Dev

Catchable fatal error: cannot be converted to string?

분류에서Dev

Catchable fatal error: cannot be converted to string?

분류에서Dev

JSON 예외 : java.lang.String을 JSONObject로 변환 할 수 없습니다.

분류에서Dev

java.lang.String이 Android에서 JSONObject로 변환되지 않는 이유

분류에서Dev

java.lang.String은 JSONObject로 변환 할 수 없습니다. 불일치 유형

분류에서Dev

java.lang.ClassCastException가 : java.lang.String의이 org.json.JSONObject 캐스트 할 수없는

분류에서Dev

org.json.JSONException: Value [....] of type org.json.JSONArray cannot be converted to JSONObject

분류에서Dev

org.jsonexception value of type org.jsonobject cannot be converted to jsonarray

분류에서Dev

java.lang.ClassNotFoundException : net.sf.json.JSONObject

분류에서Dev

http 응답을 전달할 때 java.lang.String 유형의 값을 JSONObject로 변환 할 수 없습니다.

분류에서Dev

org.json.JSONException : java.lang.String 유형의 값 <을 JSONObject로 변환 할 수 없습니다.

분류에서Dev

Android JSON 구문 분석 issue-java.lang.string을 JSONObject로 변환 할 수 없습니다.

분류에서Dev

java.lang.String 유형의 값 <br은 Android에서 JSONObject로 변환 할 수 없습니다.

분류에서Dev

JSONException : 'cityId': 4, cityName : 'Ahmedabad'java.lang.String 값을 JSONObject로 변환 할 수 없습니다.

분류에서Dev

JSONException : 'cityId': 4, cityName : 'Ahmedabad'java.lang.String 값을 JSONObject로 변환 할 수 없습니다.

분류에서Dev

java.lang.String 유형의 <html> <head> <title> Apache 값을 JSONObject로 변환 할 수 없습니다.

분류에서Dev

오류. org.json.JSONException : java.lang.String 유형의 값 <br을 JSONObject로 변환 할 수 없습니다.

분류에서Dev

java.lang.String 유형의 값 데이터베이스를 JSONObject로 변환 할 수 없습니다.

분류에서Dev

java.lang.String 유형의 연결된 값을 JSONObject로 변환 할 수 없습니다.

분류에서Dev

org.json.JSONException : java.lang.String 유형의 값 데이터를 JSONObject로 변환 할 수 없습니다.

분류에서Dev

apigee policy error - java.lang.String cannot be cast to com.apigee.flow.message.Message

분류에서Dev

Update a query using executeUpdate - Error says String cannot be cast to java.lang.Integer

분류에서Dev

. java.lang.NoSuchMethodError : org.json.JSONObject <초기화> (Ljava / LANG / 개체) V

분류에서Dev

jsonObject can not converted as jsonArray type miss match

분류에서Dev

JSONObject cannot be resolved to a type

Related 관련 기사

  1. 1

    Cannot convert string to JSONObject

  2. 2

    Error org.json.JSONException: Value 1 at error of type java.lang.Integer cannot be converted to boolean

  3. 3

    JSONArray java.lang.String을 JSONObject로 변환 할 수 없습니다

  4. 4

    JSONObject의 java.lang.NullPointerException

  5. 5

    Catchable fatal error: cannot be converted to string?

  6. 6

    Catchable fatal error: cannot be converted to string?

  7. 7

    JSON 예외 : java.lang.String을 JSONObject로 변환 할 수 없습니다.

  8. 8

    java.lang.String이 Android에서 JSONObject로 변환되지 않는 이유

  9. 9

    java.lang.String은 JSONObject로 변환 할 수 없습니다. 불일치 유형

  10. 10

    java.lang.ClassCastException가 : java.lang.String의이 org.json.JSONObject 캐스트 할 수없는

  11. 11

    org.json.JSONException: Value [....] of type org.json.JSONArray cannot be converted to JSONObject

  12. 12

    org.jsonexception value of type org.jsonobject cannot be converted to jsonarray

  13. 13

    java.lang.ClassNotFoundException : net.sf.json.JSONObject

  14. 14

    http 응답을 전달할 때 java.lang.String 유형의 값을 JSONObject로 변환 할 수 없습니다.

  15. 15

    org.json.JSONException : java.lang.String 유형의 값 <을 JSONObject로 변환 할 수 없습니다.

  16. 16

    Android JSON 구문 분석 issue-java.lang.string을 JSONObject로 변환 할 수 없습니다.

  17. 17

    java.lang.String 유형의 값 <br은 Android에서 JSONObject로 변환 할 수 없습니다.

  18. 18

    JSONException : 'cityId': 4, cityName : 'Ahmedabad'java.lang.String 값을 JSONObject로 변환 할 수 없습니다.

  19. 19

    JSONException : 'cityId': 4, cityName : 'Ahmedabad'java.lang.String 값을 JSONObject로 변환 할 수 없습니다.

  20. 20

    java.lang.String 유형의 <html> <head> <title> Apache 값을 JSONObject로 변환 할 수 없습니다.

  21. 21

    오류. org.json.JSONException : java.lang.String 유형의 값 <br을 JSONObject로 변환 할 수 없습니다.

  22. 22

    java.lang.String 유형의 값 데이터베이스를 JSONObject로 변환 할 수 없습니다.

  23. 23

    java.lang.String 유형의 연결된 값을 JSONObject로 변환 할 수 없습니다.

  24. 24

    org.json.JSONException : java.lang.String 유형의 값 데이터를 JSONObject로 변환 할 수 없습니다.

  25. 25

    apigee policy error - java.lang.String cannot be cast to com.apigee.flow.message.Message

  26. 26

    Update a query using executeUpdate - Error says String cannot be cast to java.lang.Integer

  27. 27

    . java.lang.NoSuchMethodError : org.json.JSONObject <초기화> (Ljava / LANG / 개체) V

  28. 28

    jsonObject can not converted as jsonArray type miss match

  29. 29

    JSONObject cannot be resolved to a type

뜨겁다태그

보관