我正在尝试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,但我认为这不是最佳实践。
谁能帮助我不确定该怎么做。提前致谢。