I am trying to run http get request on my onclick
function from button, I have edittext
where in need to supply by user the edittext contain the url to be use on http get request.
my button is :
<Button
android:id="@+id/mybutton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="button"
android:onClick="Functionbtn"/>
and in my activity I got
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();
}
}
and I got an error like :
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)
Some say put my target sdk to 9 but I don't think that is the best practice for this.
Could anyone help me not really sure what to do. Thanks in advance.