Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.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/1/vb.net/16.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
“安卓例外”;JSONArray fail“;使用c#JSON webservice从sql server获取数据时_C#_Android_Sql_Json_Web Services - Fatal编程技术网

“安卓例外”;JSONArray fail“;使用c#JSON webservice从sql server获取数据时

“安卓例外”;JSONArray fail“;使用c#JSON webservice从sql server获取数据时,c#,android,sql,json,web-services,C#,Android,Sql,Json,Web Services,我正在开发一款android应用程序和c#桌面应用程序。我的c#应用程序正在连接sql server数据库。 我正在使用JSON c#web服务从数据库读取/发送android中的数据 我正在使用代码从数据库中检索数据,并将其显示在android的表中。 以下是android代码: public class StudentActivity extends Activity { public void onCreate(Bundle savedInstanceState)

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

我正在使用代码从数据库中检索数据,并将其显示在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();
                }

           }
           });




     }


}
我不明白为什么它会捕获这个异常并显示“JsonArray fail”,因为这是一个示例代码,我刚刚开始学习JSON webservice。 请帮我解决它。 如果你想要更多的信息,请告诉我,我会提供给你。
谢谢

a您可能会得到一个
NetworkOnMainThreadException
,因为
rs
null
,所以您无法解析。在工作线程上运行网络操作或使用
AysncTask
@varun感谢您的快速响应。我是webservice的新手,不知道如何使用AysncTask。你能帮我怎么做任何教程或有用的链接吗?看看这个。希望当我们使用安卓4.0及以上版本时,@varun NetworkOnMainThreadException会出现,但我使用的是安卓2.2 api 8。您使用的是什么版本的模拟器或设备?
catch(JSONException e)
                    {
                            Log.e("log_tag", "Error parsing data "+e.toString());
                            Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
                    }