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
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] 삭제
몇 마디 만하겠습니다