0

我正在尝试onclick从按钮对我的函数运行 http get 请求,我edittext需要由用户提供包含要在 http get 请求上使用的 url 的编辑文本。

我的按钮是:

<Button
        android:id="@+id/mybutton"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="button" 
        android:onClick="Functionbtn"/>

在我的活动中,我得到了

public void Functionbtn(View view) {
    Toast.makeText(getApplicationContext(), "this is my toast",
            Toast.LENGTH_LONG).show();

    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting Array of Contacts
        contacts = json.getJSONArray(TAG_CONTACTS);

        // looping through All Contacts
        for (int i = 0; i < contacts.length(); i++) {
            JSONObject c = contacts.getJSONObject(i);

            // Storing each json item in variable
            String id = c.getString(TAG_ID);
            String name = c.getString(TAG_NAME);

            Toast.makeText(getApplicationContext(), id + " = " + name,
                    Toast.LENGTH_LONG).show();

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

我得到一个错误,如:

08-12 18:01:21.353: E/AndroidRuntime(10940): Caused by: android.os.NetworkOnMainThreadException
08-12 18:01:21.353: E/AndroidRuntime(10940):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
08-12 18:01:21.353: E/AndroidRuntime(10940):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)

有人说将我的目标 sdk 设置为 9,但我认为这不是最佳实践。

谁能帮助我不确定该怎么做。提前致谢。

4

3 回答 3

2

不要network在 UI 线程内做这样的相关工作。总是network在 anc 中做基于工作的工作AsyncTask,从 UI 线程调用它。

public void Functionbtn(View view) {
   new GetTaskDone().execute();
}

和:

protected class GetTaskDone extends AsyncTask<Void, Void, String> {

     @Override
     protected void onPreExecute() {
          super.onPreExecute();
          Toast.makeText(getApplicationContext(), "this is my toast",
          Toast.LENGTH_LONG).show();
     }

    protected String doInBackground(Void... params) {

    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting Array of Contacts
        contacts = json.getJSONArray(TAG_CONTACTS);

        // looping through All Contacts
        for (int i = 0; i < contacts.length(); i++) {
            JSONObject c = contacts.getJSONObject(i);

            // Storing each json item in variable
            String id = c.getString(TAG_ID);
            String name = c.getString(TAG_NAME);
            return id + " = " + name;
        }
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }
    }

    @Override
    protected void onPostExecute(String result) {
          super.onPostExecute(result);
          Toast.makeText(getApplicationContext(), result,
                    Toast.LENGTH_LONG).show();
    }

}
于 2013-08-12T10:16:55.487 回答
1

始终记得在线程或 asynctask 类中运行网络调用。最好是异步任务。

于 2013-08-12T11:16:43.017 回答
1

由于 android 的严格规则,您不能也不应该在 UI 线程上运行网络任务(或需要时间才能完成的类似任务)。

相反,您应该通过使用AsyncTask或 Simple Java Threads在后台线程中运行任务来实现它。

于 2013-08-12T10:16:40.330 回答