0

帮帮我.. 下面代码中的 JSON 有问题。无法解析为 php 文件.. android 应用程序链接到 phpmyadmin@MySQL 服务器....使用 php 文件。这使用 TextView 发送数据。数据是从其他页面传递的。并将其保存在下面的文本视图中。但是当数据要发送时。JSON 不起作用,我创建的应用程序停止并检测到故障应用程序。我的 php 文件没有问题,但在这段代码中我不知道有什么问题..它没有达到我认为的 try funcion.. 只需要帮助我使用 php 文件将数据从这个应用程序插入到 mysql 服务器。

         @Override
         protected void onPreExecute() {
             super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity3.this);
            pDialog.setMessage("Sending Aduan....");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
         }

         protected String doInBackground(String... args) {


             String nama = txtView1.getText().toString();
             String alamat = txtView2.getText().toString();
             String no_ic = txtView3.getText().toString();
             String mobile = txtView4.getText().toString();
             String telefon = txtView5.getText().toString();
             String email = txtView6.getText().toString();
             String kenyataan = txtView7.getText().toString();
             String pelaksana = txtView8.getText().toString();
             String lokasi = txtView9.getText().toString();
             String daerah = txtView10.getText().toString();    

             List<NameValuePair> params = new ArrayList<NameValuePair>();
             params.add(new BasicNameValuePair("nama", nama));
             params.add(new BasicNameValuePair("alamat", alamat));
             params.add(new BasicNameValuePair("no_ic", no_ic));
             params.add(new BasicNameValuePair("mobile", mobile));
             params.add(new BasicNameValuePair("telefon", telefon));
             params.add(new BasicNameValuePair("email", email));
             params.add(new BasicNameValuePair("kenyataan", kenyataan));
             params.add(new BasicNameValuePair("pelaksana", pelaksana));
             params.add(new BasicNameValuePair("daerah", daerah));
             params.add(new BasicNameValuePair("lokasi", lokasi));



             JSONObject json = jsonParser.makeHttpRequest(url_create_aduan,"POST", params);

             Log.e("Create Response", json.toString());

             try {
                 int success = json.getInt(TAG_SUCCESS);

                 if (success == 1) {

                   Intent i = new Intent(getApplicationContext(),Rujukan.class);
                 startActivity(i);
                 finish();
                } else {
                }
                } catch (JSONException e) {
                        e.printStackTrace();
                    }
             return null;
         }


         protected void onPostExecute(String file_url) {

             pDialog.dismiss();
         }
     }
4

1 回答 1

0

问题 :

您正在尝试doInBackgroundAsyncTask.

解决方案 :

因为不可能从 UI 线程访问 UI 元素,所以将所有项目形式移动doInBackgroundonPreExecuteonPostExecuteas :

String nama,alamat,no_ic,mobile,telefon;
String email,kenyataan,pelaksana,lokasi,daerah;

 @Override
         protected void onPreExecute() {
             super.onPreExecute();

              nama = txtView1.getText().toString();
              alamat = txtView2.getText().toString();
              no_ic = txtView3.getText().toString();
              mobile = txtView4.getText().toString();
              telefon = txtView5.getText().toString();
              email = txtView6.getText().toString();
              kenyataan = txtView7.getText().toString();
              pelaksana = txtView8.getText().toString();
              lokasi = txtView9.getText().toString();
              daerah = txtView10.getText().toString();  

            pDialog = new ProgressDialog(MainActivity3.this);
            pDialog.setMessage("Sending Aduan....");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
         }

         protected String doInBackground(String... args) {


             List<NameValuePair> params = new ArrayList<NameValuePair>();
             params.add(new BasicNameValuePair("nama", nama));
             params.add(new BasicNameValuePair("alamat", alamat));
             params.add(new BasicNameValuePair("no_ic", no_ic));
             params.add(new BasicNameValuePair("mobile", mobile));
             params.add(new BasicNameValuePair("telefon", telefon));
             params.add(new BasicNameValuePair("email", email));
             params.add(new BasicNameValuePair("kenyataan", kenyataan));
             params.add(new BasicNameValuePair("pelaksana", pelaksana));
             params.add(new BasicNameValuePair("daerah", daerah));
             params.add(new BasicNameValuePair("lokasi", lokasi));


             int success=0;
             JSONObject json = jsonParser.
               makeHttpRequest(url_create_aduan,"POST", params);

             Log.e("Create Response", json.toString());

             try {
                  success = json.getInt(TAG_SUCCESS);

                } catch (JSONException e) {
                  e.printStackTrace();
                  success=0;
                  }
             return ""+success;
         }


         protected void onPostExecute(String result) {

                pDialog.dismiss();
                if (Integer.parseInt(result) == 1) {

                   Intent i = new Intent(
                   Current_Activity.this,Rujukan.class);
                 startActivity(i);
                 finish();
                } else {
                   //your task here
                }
         }
     }
于 2012-12-15T11:39:58.883 回答