0

我正在开发一个 android 应用程序和 c# 桌面应用程序。我的 c# 应用程序正在连接 sql server 数据库。我正在使用 JSON c# Web 服务从数据库中读取/发送 android 中的数据。

我正在使用代码从数据库中检索数据并将其显示在表中的 android 中。这是安卓代码:

public class StudentActivity extends Activity
{


     public void onCreate(Bundle savedInstanceState)

     {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.student);

            Button button2 = (Button) findViewById(R.id.button2);


            button2.setOnClickListener(new View.OnClickListener()
            {
            public void onClick(View view)
              {
                 String rs = null;
                InputStream is = null;

                try{
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http://10.0.2.2:51220/Service1.svc/getData?");
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        is = entity.getContent();

                        Log.e("log_tag", "connection success ");
                     //   Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
                }
                catch(Exception e)
                {
                        Log.e("log_tag", "Error in http connection "+e.toString());
                        Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();

                }
                //convert response to string
                try
                {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                        StringBuilder sb = new StringBuilder();
                        String line = null;
                        while ((line = reader.readLine()) != null)
                        {
                                sb.append(line + "\n");
                              //  Toast.makeText(getApplicationContext(), "Input Reading pass", Toast.LENGTH_SHORT).show();
                        }
                        is.close();

                        rs=sb.toString();
                }
                catch(Exception e)
                {
                       Log.e("log_tag", "Error converting rs "+e.toString());
                    Toast.makeText(getApplicationContext(), " Input reading fail", Toast.LENGTH_SHORT).show();

                }

                //parse json data
                try
                {

                JSONArray jArray = new JSONArray(rs);


                String re=jArray.getString(jArray.length()-1);


                TableLayout tv=(TableLayout) findViewById(R.id.table);
                tv.removeAllViewsInLayout();


                   int flag=1;

                for(int i=-1;i<jArray.length()-1;i++)

                        {

                       TableRow tr=new TableRow(StudentActivity.this);

                                tr.setLayoutParams(new LayoutParams(
                                           LayoutParams.FILL_PARENT,
                                           LayoutParams.WRAP_CONTENT));




                                if(flag==1)
                                {

                                    TextView b6=new TextView(StudentActivity.this);
                                     b6.setText("ID");
                                     b6.setTextColor(Color.BLUE);
                                     b6.setTextSize(15);
                                     tr.addView(b6);


                                    TextView b19=new TextView(StudentActivity.this);
                                     b19.setPadding(10, 0, 0, 0);
                                     b19.setTextSize(15);
                                     b19.setText("First Name");
                                     b19.setTextColor(Color.BLUE);
                                     tr.addView(b19);

                                   TextView b29=new TextView(StudentActivity.this);
                                 b29.setPadding(10, 0, 0, 0);
                                     b29.setText("Last Name");
                                     b29.setTextColor(Color.BLUE);
                                     b29.setTextSize(15);
                                     tr.addView(b29);


                                     TextView b4=new TextView(StudentActivity.this);
                                 b4.setPadding(10, 0, 0, 0);
                                     b4.setText("Project");
                                     b4.setTextColor(Color.BLUE);
                                     b4.setTextSize(15);
                                     tr.addView(b4);


                                     TextView b5=new TextView(StudentActivity.this);
                                 b5.setPadding(10, 0, 0, 0);
                                     b5.setText("Date and Time");
                                     b5.setTextColor(Color.BLUE);
                                     b5.setTextSize(15);
                                     tr.addView(b5);





                                 tv.addView(tr);

                                     final View vline = new View(StudentActivity.this);
                                          vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
                                          vline.setBackgroundColor(Color.BLUE);



                                tv.addView(vline);
                                flag=0;


                                }

                                else
                                {



                                    JSONObject json_data = jArray.getJSONObject(i);

                                    Log.i("log_tag","id: "+json_data.getInt("f1")+
                                              ", firstname: "+json_data.getString("f2")+
                                              ", lastname: "+json_data.getInt("f3") +
                                              ", project: "+json_data.getString("f4")+
                                              ", datetime: "+json_data.getString("f5"));




                            TextView b=new TextView(StudentActivity.this);
                                String stime=String.valueOf(json_data.getInt("f1"));
                                  b.setText(stime);
                                b.setTextColor(Color.RED);
                                b.setTextSize(15);
                                tr.addView(b);


                               TextView b1=new TextView(StudentActivity.this);
                                b1.setPadding(10, 0, 0, 0);
                                b1.setTextSize(15);
                                String stime1=json_data.getString("f2");
                                 b1.setText(stime1);
                                b1.setTextColor(Color.WHITE);
                                tr.addView(b1);

                              TextView b2=new TextView(StudentActivity.this);
                             b2.setPadding(10, 0, 0, 0);
                                String stime2=String.valueOf(json_data.getInt("f3"));
                                b2.setText(stime2);
                                b2.setTextColor(Color.RED);
                                b2.setTextSize(15);
                                tr.addView(b2);


                                TextView b3=new TextView(StudentActivity.this);
                             b3.setPadding(10, 0, 0, 0);
                                String stime3=String.valueOf(json_data.getInt("f4"));
                                b3.setText(stime3);
                                b3.setTextColor(Color.WHITE);
                                b3.setTextSize(15);
                                tr.addView(b3);


                                TextView b4=new TextView(StudentActivity.this);
                             b4.setPadding(10, 0, 0, 0);
                                String stime4=String.valueOf(json_data.getInt("f5"));
                                b4.setText(stime4);
                                b4.setTextColor(Color.RED);
                                b4.setTextSize(15);
                                tr.addView(b4);

                                  tv.addView(tr);


                        final View vline1 = new View(StudentActivity.this);
                      vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                      vline1.setBackgroundColor(Color.WHITE);
                      tv.addView(vline1);      


                                }

                       }

                }
                catch(JSONException e)
                {
                        Log.e("log_tag", "Error parsing data "+e.toString());
                        Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
                }

           }
           });




     }


}

当我单击按钮从数据库中检索数据时运行时,我的应用程序正在捕获此异常

catch(JSONException e)
                    {
                            Log.e("log_tag", "Error parsing data "+e.toString());
                            Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
                    }

我不明白为什么它会捕获此异常并显示“JsonArray 失败”,因为这是一个示例代码,我刚刚开始学习 JSON Web 服务。请帮我解决它。如果您想了解更多信息,请告诉我我会为您提供。谢谢

4

0 回答 0