【发布时间】:2015-09-20 08:48:57
【问题描述】:
您好,我正在尝试根据用户评分将数据插入Mysql 数据库。但我得到 1 个错误:
异步任务:
private class MyInsertDataTask extends AsyncTask<String, Void, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(UserProfile.this);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setIndeterminate(true);
pDialog.setMessage("Please Wait...");
pDialog.setCancelable(false);
pDialog.setInverseBackgroundForced(true);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("magazi_id", "" + 3));
nameValuePairs.add(new BasicNameValuePair("ratingNumber", String.valueOf(percent)));
nameValuePairs.add(new BasicNameValuePair("comment",comTxt));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://my.chatapp.info/order_api/insertData/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());
}
try
{
JSONObject json_data = new JSONObject(result);
code=(json_data.getInt("code"));
if(code==1)
{
Toast.makeText(UserProfile.this, "Inserted Successfully",
Toast.LENGTH_SHORT).show();
return String.valueOf(code);
}
else
{
Toast.makeText(UserProfile.this, "Sorry, Try Again",
Toast.LENGTH_LONG).show();
return String.valueOf(code);
}
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
return null;
}
@Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
pDialog.dismiss();
}
}
插入方式:
private void insertDataToDatabaseFromRating() {
ratingBar = (RatingBar) newLayout.findViewById(R.id.ratingBar);
ratingComment = (EditText) newLayout.findViewById(R.id.ratingComment);
percent = ratingBar.getRating();
comTxt = ratingComment.getText().toString();
if (comTxt.isEmpty()) {
comTxt = " ";
}
MyInsertDataTask task = new MyInsertDataTask();
task.execute();
}
我插入数据的 .php 文件
<?php
try {
$handler = new PDO('mysql:host=localhost;dbname=project', 'username', 'password');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
echo $e->getMessage();
die();
}
$magaziId = $_REQUEST['magazi_id'];
$rating=$_REQUEST['ratingNumber'];
$comment = $_REQUEST['comment'];
$flag['code']=0;
if($handler->query('INSERT INTO ratings VALUES(, $magaziId, $rating, $comment)'){
$flag['code']=1;
}
print(json_encode($flag));
?>
错误:
09-20 04:43:11.470 7318-7350/com.order.app.order E/pass 1﹕ connection success
09-20 04:43:11.470 7318-7350/com.order.app.order E/pass 2﹕ connection success
09-20 04:43:11.470 7318-7350/com.order.app.order E/Fail 3﹕ org.json.JSONException: End of input at character 0 of
我通过了 1 和 2,但它失败了。谁能帮帮我?
谢谢
【问题讨论】:
-
什么字符串得到
result = sb.toString();行? -
只需添加
Log.e("pass 2", "connection success "+result);并检查从服务器获取的字符串 -
我不想从服务器获取字符串我想向服务器发送信息但我不知道我做错了什么