【发布时间】:2016-06-06 17:39:26
【问题描述】:
如果我在网络浏览器中测试我的 PHP 文件,它可以工作(例如,给定 newObsindiaries.php?idd="52"&ido="1"&when="10",它会毫无问题地插入数据库)。
但是,在 Android 中,它不起作用并在日志中返回此消息:
org.json.JSONException: [idd=52, ido=1, when=10] 的字符 5 处未终止的数组
这是 PHP 代码:
<?php
$dbhost = "127.0.0.1";
$dbuser = "root";
$dbpass = "";
$dbname = "clinic_last";
$link = @mysqli_connect($dbhost,$dbuser,$dbpass,$dbname)
or die("Impossibile collegarsi al server: " . mysqli_error());
mysqli_select_db($link ,'clinic_last')
or die("unable to select database 'dbname': " . mysqli_error());
$idd = $_GET['idd'];
$ido = $_GET['ido'];
$when = $_GET['when'];
$string = ("INSERT INTO obsindiaries (`when`, ido, idd)
SELECT $when,$ido,idd FROM diaries WHERE idd = $idd LIMIT 1");
$string2 = "INSERT INTO obsindiaries (idod,idd,ido,`when`) VALUES
(NULL,'$idd','$ido','$when')";
$flag['code']=0;
$result = mysqli_query($link,$string);
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "**successfully Registration.**";
// echoing JSON response
echo json_encode($response);
}
if (!$result) {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
if (!$result) {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
mysqli_close($link);
?>
这是文件.class的一部分:
public void ClickSaveObsindiaries(){
SavingObs=(ImageButton)findViewById(R.id.saveObsindiaries);
SavingObs.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//INSERISCO VALORI DENTRO LISTA
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("idd", ID_diary.getText().toString()));
int positionTypeObs = typeObsindiaries.getSelectedItemPosition() + 1;
String valueType = String.valueOf(positionTypeObs);
nameValuePairs.add(new BasicNameValuePair("ido",valueType));
char first = hours.getSelectedItem().toString().charAt(0);
if (first=='I')
nameValuePairs.add(new BasicNameValuePair("when", IntervalHours.getText().toString()));
else
nameValuePairs.add(new BasicNameValuePair("when", hours.getSelectedItem().toString()));
System.out.println("valore di RESULT: "+nameValuePairs.toString());
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/newObsindiaries.php");
Log.e("httpPost is:"," "+httpPost.toString());
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));//,"UTF-8"
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(nameValuePairs.toString());
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());
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(add_obsindiaries.this, R.style.myDialog));
builder.setMessage("Misurazione inserita! Premere OK per tornare alla lista delle misurazioni")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent listOBS = new Intent(add_obsindiaries.this,obsindiaries_list.class);
fromObsList.putString("idDiary", fromObsList.getString("idDiary"));
listOBS.putExtras(fromObsList);
add_obsindiaries.this.finish();
startActivity(listOBS);
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
【问题讨论】:
标签: android sql phpmyadmin