Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中将表格从web传输到我的数据库_Java_Android_Database_Eclipse - Fatal编程技术网

Java 如何在Android中将表格从web传输到我的数据库

Java 如何在Android中将表格从web传输到我的数据库,java,android,database,eclipse,Java,Android,Database,Eclipse,我正在制作一个应用程序,在这个应用程序中,我连接到一个网站,以json字符串的形式从我的web数据库下载一个表,我已经这样做了,在我的应用程序中显示为一个webArray。我已经创建了一个数据库助手类来在应用程序中创建和编辑我的数据库,我的问题是我无法将数据从我的webArray传输到我的数据库,下面是一些代码,也许你可以帮我指出正确的方向 主要活动: //this is our download file asynctask class DownloadFileAsync extends

我正在制作一个应用程序,在这个应用程序中,我连接到一个网站,以json字符串的形式从我的web数据库下载一个表,我已经这样做了,在我的应用程序中显示为一个webArray。我已经创建了一个数据库助手类来在应用程序中创建和编辑我的数据库,我的问题是我无法将数据从我的webArray传输到我的数据库,下面是一些代码,也许你可以帮我指出正确的方向

主要活动:

  //this is our download file asynctask
class DownloadFileAsync extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(DIALOG_DOWNLOAD_PROGRESS2);
    }

    @Override
    protected String doInBackground(String... aurl) {

        try {
        String result = "";
                    try {
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http://mywebsite");
                        // httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        InputStream webs = entity.getContent();
                        // convert response to string
                        try {
                            BufferedReader reader = new BufferedReader(
                                    new InputStreamReader(webs, "iso-8859-1"), 8);
                            StringBuilder sb = new StringBuilder();
                            String line = null;
                            while ((line = reader.readLine()) != null) {
                                sb.append(line + "\n");
                            }
                            webs.close();

                            result = sb.toString();
                        } catch (Exception e) {
                            Log.e("log_tag", "Error converting result " + e.toString());
                            return "ERROR_IN_CODE";
                        }
                    } catch (Exception e) {
                        Log.e("log_tag", "Error in http connection " + e.toString());
                        return "ERROR_IN_CODE";
                    }

                    // parse json data
                    try {
                        JSONArray jArray = new JSONArray(result);
                        for (int i = 0; i < jArray.length(); i++) {
                            JSONObject json_data = jArray.getJSONObject(i);
                            webResultCuestionario resultRow3 = new webResultCuestionario();
                            resultRow3._id = json_data.getString("id");
                            resultRow3.pregunta = json_data.getString("Question");
                            resultRow3.respuesta = json_data.getString("Answer");
                            CuestionarioArray.add(resultRow3);
                        }
                    } catch (JSONException e) {
                        Log.e("log_tag", "Error parsing data " + e.toString());
                        return "ERROR_IN_CODE";
                    }
    } catch (Exception e) {
        // this is the line of code that sends a real error message to the
        // log
        Log.e("ERROR", "ERROR IN CODE: " + e.toString());
        // this is the line that prints out the location in
        // the code where the error occurred.
        e.printStackTrace();
        return "ERROR_IN_CODE";
    }
        return null;
    }

    protected void onProgressUpdate(String... progress) {
         Log.d(LOG_TAG,progress[0]);
         mProgressDialog2.setProgress(Integer.parseInt(progress[0]));
    }

    @Override
    protected void onPostExecute(String unused) {
        //dismiss the dialog after the file was downloaded
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS2);
        if(unused != null && unused.equals("ERROR_IN_CODE")){
            errornote2();
        }else{
            tvCuestionario.setText("Cuestionario encontrado");
            addCuestionarioToDb();
        }
    }
}   

public void addCuestionarioToDb(){
for (webResultCuestionario currentItem: CuestionarioArray){
    int cis = Integer.parseInt(currentItem._id);
    db.insertCuestionario(CuestionarioArray.get(cis).pregunta, CuestionarioArray.get(cis).respuesta);
}
}
日志

09-23 16:23:02.977: E/AndroidRuntime(6496): FATAL EXCEPTION: main
09-23 16:23:02.977: E/AndroidRuntime(6496): java.lang.IndexOutOfBoundsException: Invalid  index 100, size is 100
09-23 16:23:02.977: E/AndroidRuntime(6496):     at   java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
09-23 16:23:02.977: E/AndroidRuntime(6496):     at    java.util.ArrayList.get(ArrayList.java:304)
09-23 16:23:02.977: E/AndroidRuntime(6496):     at VerCuestionarioEspanol_copy.MainActivity.addCuestionarioToDb(MainActivity.java:580)
09-23 16:23:02.977: E/AndroidRuntime(6496):     at  VerCuestionarioEspanol_copy.MainActivity$DownloadFile3Async.onPostExecute(MainActivity.java:46 2)
09-23 16:23:02.977: E/AndroidRuntime(6496):     at  VerCuestionarioEspanol_copy.MainActivity$DownloadFile3Async.onPostExecute(MainActivity.java:1)
09-23 16:23:02.977: E/AndroidRuntime(6496):     at android.os.AsyncTask.finish(AsyncTask.java:602)
09-23 16:23:02.977: E/AndroidRuntime(6496):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
09-23 16:23:02.977: E/AndroidRuntime(6496):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
09-23 16:23:02.977: E/AndroidRuntime(6496):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-23 16:23:02.977: E/AndroidRuntime(6496):     at android.os.Looper.loop(Looper.java:154)
09-23 16:23:02.977: E/AndroidRuntime(6496):     at android.app.ActivityThread.main(ActivityThread.java:4977)
09-23 16:23:02.977: E/AndroidRuntime(6496):     at java.lang.reflect.Method.invokeNative(Native Method)
09-23 16:23:02.977: E/AndroidRuntime(6496):     at java.lang.reflect.Method.invoke(Method.java:511)
09-23 16:23:02.977: E/AndroidRuntime(6496):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-23 16:23:02.977: E/AndroidRuntime(6496):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-23 16:23:02.977: E/AndroidRuntime(6496):     at dalvik.system.NativeStart.main(Native Method)

路易斯建议我的解决方案是:

public void addCuestionarioToDb(){
  for (webResultCuestionario currentItem: CuestionarioArray){
    int cis = Integer.parseInt(currentItem._id)-1;   // just added the -1 here and runs perfect
    db.insertCuestionario(CuestionarioArray.get(cis).pregunta,  CuestionarioArray.get(cis).respuesta);
  }
}

@dor506,嗯,没有添加项目。也许会有不同的方法来添加它们?奇怪。你没有得到任何例外吗?检查log@zvzej对于包含100项的列表(有效位置从0到99),在访问数组列表位置100时,代码崩溃。如果双击日志行“at vercuestionaespanol_copy.MainActivity.addCuestionarioToDb(MainActivity)”,您将进入生成问题的代码行。可能是行“db.insertCuestionario(cuestionarray.get(cis.pregunta,cuestionarray.get(cis.respuesta);”其中cis的值为100,数组列表只有99个位置。@zvzej,如我前面所说,如果您有100个问题(编号从1到100)当您将它们添加到数组列表中时,索引将从0到99编号。要从问题编号到索引编号,您需要从问题编号中减去1。@zvzej我不确定我是否理解您的问题,但java数组在索引0上总是第一位,在索引1上总是第二位,依此类推……例如,如果您对于包含1000个项目的数组,有效索引的范围为0到999。
public void addCuestionarioToDb(){
  for (webResultCuestionario currentItem: CuestionarioArray){
    int cis = Integer.parseInt(currentItem._id)-1;   // just added the -1 here and runs perfect
    db.insertCuestionario(CuestionarioArray.get(cis).pregunta,  CuestionarioArray.get(cis).respuesta);
  }
}