0

我完成了我的时间表应用程序,其中时间表存储在 wamp 服务器中,我的 android 应用程序将获取表并将它们保存在 android 上的 sqlite DB 中。我使用 Jsonparsing 从服务器获取数据。

我未能在 www.000webhost.com 上托管我的表,在 android logcat 中,我收到有关 Json 解析的警告和错误。

浏览时我知道,我在 wamp 中使用的 php 是 3.4,但在上面的网站上它是 3.2,它不支持 json 数组。

有没有办法解决这个问题?是否有任何其他免费网站可以托管支持 Json 数组?

这是我的部分代码..

private class GrabURL extends AsyncTask<String, Void, Void>
{
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();;

    private ProgressDialog Dialog = new ProgressDialog(MainActivity.this);

    protected void onPreExecute() 
    {
        Dialog.setMessage("Loading...");
        Dialog.show();
        nameValuePairs.add(new BasicNameValuePair("DEPT",DEPT));
        nameValuePairs.add(new BasicNameValuePair("SEM",SEM));
        nameValuePairs.add(new BasicNameValuePair("DIV",DIV));
    }
    protected Void doInBackground(String... urls)
    {
       try{
              HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new HttpPost(url);
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
              response=httpclient.execute(httppost);
              inputStream = response.getEntity().getContent();

          }
          catch(Exception e)
          {
              Dialog.dismiss();
              Toast.makeText(getBaseContext(), "Check your internet connection", Toast.LENGTH_SHORT).show();
              Log.e("log_tag", "Error in http connection "+e.toString());

          }


       return null;
    }
    protected void onPostExecute(Void unused)
    {
         try{
           BufferedReader reader = new BufferedReader(new InputStreamReader(
                    inputStream, "iso-8859-1"), 8);

            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            inputStream.close();
             json = sb.toString();
       }
       catch (Exception e) {
              Log.e("Buffer Error", "Error converting result " + e.toString());
          }
       try{
           JSONArray jarray = new JSONArray(json);
        SQLiteDatabase db=openOrCreateDatabase("Mydb", MODE_PRIVATE,null);
        db.execSQL("DROP TABLE IF EXISTS stores;");
        db.execSQL("CREATE TABLE IF NOT EXISTS stores(day VARCHAR,first VARCHAR,sec VARCHAR," +
                "third VARCHAR,fourth VARCHAR,fifth VARCHAR,sixth VARCHAR," +
                "seventh VARCHAR,eighth VARCHAR);");
        db.execSQL("INSERT INTO stores VALUES('DAY','8-9','9-10','10:15-11:15','11:15-12:15'," +
                "'12:30-1:30','1:30-2:30','2:45-3:45','3:45-4:45');");
           for (int i=0; i < (jarray.length()); i++)
           {
             JSONObject oneObject = jarray.getJSONObject(i);
          SQLiteDatabase db1=openOrCreateDatabase("Mydb", MODE_PRIVATE,null);

          db1.execSQL("INSERT INTO stores ( day,first,sec,third,fourth,fifth,sixth,seventh,eighth) " +
                "VALUES ( ?, ?, ?, ?, ?, ?, ?, ?,? )",
                new Object [] {
               strday = oneObject.getString("day"),
               strone = oneObject.getString("8-9"),
               strtwo = oneObject.getString("9-10"),
               strthree = oneObject.getString("10:15-11:15"),
               strfour = oneObject.getString("11:15-12:15"),
               strfive = oneObject.getString("12:30-1:30"),
               strsix = oneObject.getString("1:30-2:30"),
               strseven = oneObject.getString("2:45-3:45"),
               streight = oneObject.getString("3:45-4:45")
        }
                  );
        db1.close();
        Dialog.dismiss(); 
           }
        Toast.makeText(getBaseContext(), "Successfully saved.", Toast.LENGTH_SHORT).show();
           db.close();

       }
       catch (Exception e) {
            e.printStackTrace();
         Log.e("log_tag", "oops!! error in saving"+e.toString());
        Toast.makeText(getBaseContext(), "oops!! error in saving ur  time table", Toast.LENGTH_SHORT).show();
        Toast.makeText(getBaseContext(), "Your Time Table is not available", Toast.LENGTH_SHORT).show();
        }



    }
}
public void grabURL(String url)
{
    new GrabURL().execute(url);
}

日志猫....

12-18 18:27:59.373: W/KeyCharacterMap(1014): No keyboard for id 0
12-18 18:27:59.373: W/KeyCharacterMap(1014): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-18 18:28:37.283: D/dalvikvm(1014): GC_EXTERNAL_ALLOC freed 167K, 51% free 2716K/5511K, external 513K/517K, paused 51ms
12-18 18:28:38.643: D/dalvikvm(1014): GC_FOR_MALLOC freed 66K, 50% free 2827K/5639K, external 518K/1030K, paused 47ms
12-18 18:28:39.363: W/System.err(1014): org.json.JSONException: Value Invalid of type java.lang.String cannot be converted to JSONArray
12-18 18:28:39.363: W/System.err(1014):     at org.json.JSON.typeMismatch(JSON.java:107)
12-18 18:28:39.363: W/System.err(1014):     at org.json.JSONArray.<init>(JSONArray.java:91)
12-18 18:28:39.363: W/System.err(1014):     at org.json.JSONArray.<init>(JSONArray.java:103)
12-18 18:28:39.363: W/System.err(1014):     at com.exam.allabouttable.MainActivity$GrabURL.onPostExecute(MainActivity.java:195)
12-18 18:28:39.373: W/System.err(1014):     at com.exam.allabouttable.MainActivity$GrabURL.onPostExecute(MainActivity.java:1)
12-18 18:28:39.373: W/System.err(1014):     at android.os.AsyncTask.finish(AsyncTask.java:417)
12-18 18:28:39.373: W/System.err(1014):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
12-18 18:28:39.373: W/System.err(1014):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
12-18 18:28:39.373: W/System.err(1014):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 18:28:39.373: W/System.err(1014):     at android.os.Looper.loop(Looper.java:123)
12-18 18:28:39.373: W/System.err(1014):     at android.app.ActivityThread.main(ActivityThread.java:3683)
12-18 18:28:39.373: W/System.err(1014):     at java.lang.reflect.Method.invokeNative(Native Method)
12-18 18:28:39.373: W/System.err(1014):     at java.lang.reflect.Method.invoke(Method.java:507)
12-18 18:28:39.383: W/System.err(1014):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-18 18:28:39.383: W/System.err(1014):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-18 18:28:39.383: W/System.err(1014):     at dalvik.system.NativeStart.main(Native Method)
12-18 18:28:39.383: E/log_tag(1014): oops!! error in savingorg.json.JSONException: Value Invalid of type java.lang.String cannot be converted to JSONArray
12-18 18:36:29.023: I/dalvikvm(1014): Jit: resizing JitTable from 512 to 1024
4

2 回答 2

0

对不起,我刚刚解决了我的问题,在这里得到了回答

我已将 HttpPost 更改为 HttpGet,用于检索数据。

于 2013-11-16T16:41:13.300 回答
0

从您的日志看来,您传递给的字符串new JSONArray()不是有效的 JSON。确保你的字符串是有效的,也许在你传递它之前打印出来只是为了检查它是否有效。

于 2012-12-19T21:15:10.210 回答