我正在开发一个测试Android应用程序,该应用程序必须显示来自Mysql数据库的一些数据并发回一些数据。但我不知道我的代码有什么问题。
这是我的活动。
private Spinner spinner;
// add items into spinner dynamically
@SuppressWarnings("unused")
public void addListenerOnSpinnerItemSelection() {
spinner = (Spinner) findViewById(R.id.spinner);
try {
JSONArray data = new JSONArray(getHttpPost(url,params));
final List<String> MyArrList = new ArrayList<String>();
if (data != null)
{
for (int i = 0; i < data.length(); i++) {
JSONObject c = (JSONObject) data.get(i);
strLevel = c.getString("lv_name");
MyArrList.add(strLevel);
Log.i("Value", strLevel);
}
ArrayAdapter<String> vehicleAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, MyArrList);
vehicleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(vehicleAdapter);
}
else
{
Log.i("Value","Null value");
}
final AlertDialog.Builder ad = new AlertDialog.Builder(this);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View selectedItemView,
int position, long id) {
params.add(new BasicNameValuePair("level", strLevel));
try{
strLevel = (spinner.getSelectedItem().toString());
String resultServer = getHttpPost(url,params);
Log.i("Send to Server", strLevel);
Log.i("Result", resultServer);
}
catch (Exception e)
{
Log.e("ERROR", e.toString());
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(SettingActivity.this,
"Your Selected : Nothing",
Toast.LENGTH_SHORT).show();
}
});
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是我的PHP文件。已发送数据
<?php
ob_start();
$objConnect = mysql_connect("mydbms","xxxx","xxxx");
$objDB = mysql_select_db("xxxx");
$level = $_POST["level"];
setcookie("level", $level, time()+60*60*24*3650);
$strSQL = "SELECT question.id_lv, level_detail.lv_name FROM question LEFT JOIN level_detail ON question.id_lv = level_detail.id_lv GROUP BY id_lv ASC";
$objQuery = mysql_query($strSQL) or die(mysql_error());
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery))
{
$arrCol = array();
for($i=0;$i<$intNumField;$i++)
{
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
mysql_close($objConnect);
echo json_encode($resultArray);
ob_end_flush();
?>
这是PHP的文件。使用过的数据。
<?php
ob_start();
$objConnect = mysql_connect("mydbms","xxxx","xxxx");
$objDB = mysql_select_db("xxxx");
$strQuestionID = $_POST["sQuestion"];
$strSQL = "SELECT question.id_question, question.q_name, question.q_means, question.picture_path, level_detail.lv_name FROM question";
$strSQL .= " LEFT JOIN level_detail ON question.id_lv = level_detail.id_lv";
$strSQL .= " WHERE question.id_lv = '".$_COOKIE['level']."'";
$strSQL .= " AND q_type = 'Dragdrop'";
$strSQL .= " ORDER BY RAND( ) LIMIT 1";
$objQuery = mysql_query($strSQL) or die(mysql_error());
$objResult = mysql_fetch_array($objQuery) or die(mysql_error());
if($objResult)
{
$arr["QID"] = $objResult["id_question"];
$arr["question"] = $objResult["q_name"];
$arr["means"] = $objResult["q_means"];
$arr["picture"] = $objResult["picture_path"];
}
mysql_close($objConnect);
echo json_encode($arr);
ob_end_flush();
?>
这是HttpPost。
public String getHttpPost(String url,List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Status OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
这是LogCat。
03-16 11:29:02.108: W/System.err(2016): org.json.JSONException: End of input at character 0 of
03-16 11:29:02.112: W/System.err(2016): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
03-16 11:29:02.116: W/System.err(2016): at org.json.JSONTokener.nextValue(JSONTokener.java:97)
03-16 11:29:02.116: W/System.err(2016): at org.json.JSONObject.<init>(JSONObject.java:154)
03-16 11:29:02.116: W/System.err(2016): at org.json.JSONObject.<init>(JSONObject.java:171)
03-16 11:29:02.116: W/System.err(2016): at com.example.dfromcode.MainActivity.onCreate(MainActivity.java:136)
03-16 11:29:02.116: W/System.err(2016): at android.app.Activity.performCreate(Activity.java:5104)
03-16 11:29:02.116: W/System.err(2016): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-16 11:29:02.116: W/System.err(2016): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-16 11:29:02.116: W/System.err(2016): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-16 11:29:02.120: W/System.err(2016): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-16 11:29:02.120: W/System.err(2016): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-16 11:29:02.120: W/System.err(2016): at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 11:29:02.120: W/System.err(2016): at android.os.Looper.loop(Looper.java:137)
03-16 11:29:02.120: W/System.err(2016): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-16 11:29:02.124: W/System.err(2016): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 11:29:02.124: W/System.err(2016): at java.lang.reflect.Method.invoke(Method.java:511)
03-16 11:29:02.132: W/System.err(2016): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-16 11:29:02.132: W/System.err(2016): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-16 11:29:02.132: W/System.err(2016): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
异常消息:
org.json.JSONException: End of input at character 0
向我建议你的代码试图解析一个空字符串。换句话说,您的getHttpPost
方法(您还没有向我们展示!)失败。
(通过下注是getHttpPost
未正确处理服务器提供错误响应的情况。)
我怀疑这个测试是问题的一部分:
JSONArray data = new JSONArray(getHttpPost(url,params));
...
if (data != null)
data
实际上不可能为null
。 new
操作要么为您提供对新对象的(非null!)引用,要么以异常终止。