以下是我用于访问数据库的代码,但出现以下错误:
org.json.JSONException:值
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());
}
}
这是PHP代码
错误是:
不推荐使用:mysql_connect():不推荐使用mysql扩展,以后将被删除:使用mysqli或PDO
和
警告:mysql_connect()第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);
?>
日志猫
548-548 / com.example.gurucharan.mts D / dalvikvm:不晚启用CheckJNI(已启用)
548-548 / com.example.gurucharan.mts D / dalvikvm:GC_FOR_ALLOC释放123K,3%释放9218K / 9415K,暂停60ms
548-548 / com.example.gurucharan.mts I / dalvikvm-heap:将堆(frag大小写)增长到12.827MB以分配3940072字节
548-554 / com.example.gurucharan.mts I / dalvikvm:threadid = 3:对信号3作出反应
548-551 / com.example.gurucharan.mts D / dalvikvm:GC_CONCURRENT释放了1K,2%释放了13065K / 13319K,暂停了5ms + 16ms
548-554 / com.example.gurucharan.mts I / dalvikvm:将堆栈跟踪写入'/data/anr/traces.txt'
548-554 / com.example.gurucharan.mts I / dalvikvm:threadid = 3:对信号3做出反应
548-554 / com.example.gurucharan.mts I / dalvikvm:将堆栈跟踪写入'/data/anr/traces.txt'
548-554 / com.example.gurucharan.mts I / dalvikvm:threadid = 3:对信号3作出反应
548-554 / com.example.gurucharan.mts I / dalvikvm:将堆栈跟踪写入'/data/anr/traces.txt'
548-554 / com.example.gurucharan.mts I / dalvikvm:threadid = 3:对信号3作出反应
548-554 / com.example.gurucharan.mts I / dalvikvm:将堆栈跟踪写入'/data/anr/traces.txt'
548-548 / com.example.gurucharan.mts D / libEGL:已加载/system/lib/egl/libGLES_android.so
548-548 / com.example.gurucharan.mts D / libEGL:已加载/system/lib/egl/libEGL_emulation.so
548-548 / com.example.gurucharan.mts D /:HostConnection :: get()建立新的主机连接0xd77e8,TID 548
548-548 / com.example.gurucharan.mts D / libEGL:已加载/system/lib/egl/libGLESv1_CM_emulation.so
548-548 / com.example.gurucharan.mts D / libEGL:已加载/system/lib/egl/libGLESv2_emulation.so
548-554 / com.example.gurucharan.mts I / dalvikvm:threadid = 3:对信号3做出反应
548-554 / com.example.gurucharan.mts I / dalvikvm:将堆栈跟踪写入'/data/anr/traces.txt'
548-548 / com.example.gurucharan.mts W / EGL_emulation:eglSurfaceAttrib未实现
548-548 / com.example.gurucharan.mts D / OpenGLRenderer:启用调试模式0
548-554 / com.example.gurucharan.mts I / dalvikvm:threadid = 3:对信号3做出反应
548-554 / com.example.gurucharan.mts I / dalvikvm:将堆栈跟踪写入'/data/anr/traces.txt'
548-554 / com.example.gurucharan.mts I / dalvikvm:threadid = 3:对信号3做出反应
548-554 / com.example.gurucharan.mts I / dalvikvm:将堆栈跟踪写入'/data/anr/traces.txt'
548-554 / com.example.gurucharan.mts I / dalvikvm:threadid = 3:对信号3做出反应
548-554 / com.example.gurucharan.mts I / dalvikvm:将堆栈跟踪写入'/data/anr/traces.txt'
548-554 / com.example.gurucharan.mts I / dalvikvm:threadid = 3:对信号3作出反应
548-554 / com.example.gurucharan.mts I / dalvikvm:将堆栈跟踪写入'/data/anr/traces.txt'
548-554 / com.example.gurucharan.mts I / dalvikvm:threadid = 3:对信号3做出反应
548-554 / com.example.gurucharan.mts I / dalvikvm:将堆栈跟踪写入'/data/anr/traces.txt'
548-548 / com.example.gurucharan.mts E / pass 1:连接成功
548-548 / com.example.gurucharan.mts E / pass 2:连接成功
548-548 / com.example.gurucharan.mts E / Fail 3:org.json.JSONException:值
最佳答案
查看我如何使用php编码进行插入
<?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);
?>
注意:
对于新版本的php,在
<?php
代码段后添加此行error_reporting(E_ALL ^ E_DEPRECATED);
关于php - java.lang.String无法转换为JSONObject,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25351944/