【问题标题】:Android Studio with JSON URL does not give any result [duplicate]带有 JSON URL 的 Android Studio 没有给出任何结果 [重复]
【发布时间】:2016-02-22 22:12:59
【问题描述】:

我的情况很烦人,因为我无法得到结果。 我正在尝试使用 localhost JSON GET 调用。当我在浏览器中使用此 URL 时,我得到一个 JSON 结果(不是 xml) http://localhost:8080/Boxbackend/webresources/entities.movingboxes

[
{
"boxId": 1,
"boxName": "Stuff_1"
},
{
"boxId": 2,
"boxName": "Stuff_2"
},
{
"boxId": 3,
"boxName": "Stuff_3"
}
]

我使用这个 netbeans 教程创建了一个 RESTful Web 服务

https://netbeans.org/kb/docs/websvc/rest.html

我在 android studio 中使用了这个例子

https://www.learn2crack.com/2013/10/android-asynctask-json-parsing-example.html

当 url 传递时:

JSONObject json = jParser.getJSONFromUrl(url);

json 得到结果为 null 并触发异常 有时异常:android.os.NetworkOnMainThreadException

我有时也使用 10.0.2.2 而不是 localhost

我有点沮丧..

也许有人可以帮忙

额外信息:

Mainactivity onCreate 有一个按钮

Button box_list_button=(Button)findViewById(R.id.box_list_button);
        box_list_button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {    
                i = new Intent(getApplicationContext(), boxlist.class);
                startActivity(i);
            }
        });

boxlist.class 是这样的

   public class boxlist extends Activity {

        ArrayList mobileArray;
        String [] mobileResult =  {"there is no data"};
        private static String url = "http://localhost:8080/Boxbackend/webresources/entities.movingboxes";
        JSONArray mbox = null;
        private static final String TAG_BNAME = "boxName";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.movingbox_layout);
            new JSONParse().execute();
}
    private class JSONParse extends AsyncTask<String, String, JSONObject> {
        private ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
//            uid = (TextView)findViewById(R.id.uid);
//            name1 = (TextView)findViewById(R.id.name);
//            email1 = (TextView)findViewById(R.id.email);
            pDialog = new ProgressDialog(boxlist.this);
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }

        @Override
        protected JSONObject doInBackground(String... args) {
            JSONParser jParser = new JSONParser();

            // Getting JSON from URL

            JSONObject json = jParser.getJSONFromUrl(url);
            return json;
        } .......

【问题讨论】:

    标签: android json localhost


    【解决方案1】:

    android.os.NetworkOnMainThreadException 将在您尝试从主线程执行任何类型的网络访问时抛出。相反,使用某种机制将工作转移到后台线程,例如 AsyncTask、Loader 或 Service。

    在 Android 应用的主线程上进行任何类型的阻塞调用都是一种不好的做法,包括文件访问和网络请求。也避免繁重的计算。您可以阅读更多关于 herehere 的信息。

    【讨论】:

    • 但是问题中有一个AsyncTask...
    猜你喜欢
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多