0

Am getting this error "Could not execute method of the activity" ... can some one help why this is happening ?

06-17 15:47:54.562: E/AndroidRuntime(910): FATAL EXCEPTION: main
06-17 15:47:54.562: E/AndroidRuntime(910): java.lang.IllegalStateException: Could   not      execute method of the activity
06-17 15:47:54.562: E/AndroidRuntime(910):  at     android.view.View$1.onClick(View.java:3591)
06-17 15:47:54.562: E/AndroidRuntime(910):  at android.view.View.performClick(View.java:4084)
06-17 15:47:54.562: E/AndroidRuntime(910):  at android.view.View$PerformClick.run(View.java:16966)
06-17 15:47:54.562: E/AndroidRuntime(910):  at android.os.Handler.handleCallback(Handler.java:615)
06-17 15:47:54.562: E/AndroidRuntime(910):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-17 15:47:54.562: E/AndroidRuntime(910):  at android.os.Looper.loop(Looper.java:137)
06-17 15:47:54.562: E/AndroidRuntime(910):  at android.app.ActivityThread.main(ActivityThread.java:4745)
06-17 15:47:54.562: E/AndroidRuntime(910):  at java.lang.reflect.Method.invokeNative(Native Method)
06-17 15:47:54.562: E/AndroidRuntime(910):  at java.lang.reflect.Method.invoke(Method.java:511)
06-17 15:47:54.562: E/AndroidRuntime(910):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-17 15:47:54.562: E/AndroidRuntime(910):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-17 15:47:54.562: E/AndroidRuntime(910):  at dalvik.system.NativeStart.main(Native Method)
06-17 15:47:54.562: E/AndroidRuntime(910): Caused by: java.lang.reflect.InvocationTargetException
06-17 15:47:54.562: E/AndroidRuntime(910):  at java.lang.reflect.Method.invokeNative(Native Method)
06-17 15:47:54.562: E/AndroidRuntime(910):  at java.lang.reflect.Method.invoke(Method.java:511)
06-17 15:47:54.562: E/AndroidRuntime(910):  at android.view.View$1.onClick(View.java:3586)
06-17 15:47:54.562: E/AndroidRuntime(910):  ... 11 more
06-17 15:47:54.562: E/AndroidRuntime(910): Caused by: android.os.NetworkOnMainThreadException
06-17 15:47:54.562: E/AndroidRuntime(910):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-17 15:47:54.562: E/AndroidRuntime(910):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-17 15:47:54.562: E/AndroidRuntime(910):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-17 15:47:54.562: E/AndroidRuntime(910):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-17 15:47:54.562: E/AndroidRuntime(910):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
06-17 15:47:54.562: E/AndroidRuntime(910):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-17 15:47:54.562: E/AndroidRuntime(910):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119 )
06-17 15:47:54.562: E/AndroidRuntime(910):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-17 15:47:54.562: E/AndroidRuntime(910):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-17 15:47:54.562: E/AndroidRuntime(910):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-17 15:47:54.562: E/AndroidRuntime(910):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-17 15:47:54.562: E/AndroidRuntime(910):  at com.test.readurl.JSONParser.makeHttpRequest(JSONParser.java:62)
06-17 15:47:54.562: E/AndroidRuntime(910):  at com.test.readurl.MainActivity.button1_onClick(MainActivity.java:40)
06-17 15:47:54.562: E/AndroidRuntime(910):  ... 14 more

while trying to execute...

package com.test.readurl;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
private static final String url_detials = "http://foefinder.com/api/details.php";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void button1_onClick(View view){
    //Toast.makeText(getApplicationContext(), "Hello Test", Toast.LENGTH_LONG).show();

     // JSON parser class
    JSONParser jsonParser = new JSONParser();

    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    String pid = null;
    params.add(new BasicNameValuePair("pid", pid));

    // getting product details by making HTTP request
    // Note that product details url will use GET request
    JSONObject json = jsonParser.makeHttpRequest(
            url_detials, "GET", params);

    // check your log for json response
    Log.d("Single Product Details", json.toString());

} 

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

Am trying to read a URL from this app on a button click... but not able to do so... can some one help me with this ?

user2301765
  • 669
  • 2
  • 8
  • 22

3 Answers3

1

You're doing an HTTP request (JSONObject json = jsonParser.makeHttpRequest( url_detials, "GET", params);) on the UI thread.
Move this stuff in an AsyncTask.

How should I modify this ?

Community
  • 1
  • 1
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
1

You should use AsyncTask. Android doesnt allow http request running on the main UI thread.

JSONObject json = jsonParser.makeHttpRequest(url_detials, "GET", params);

If you want a quick test, you can add this in your onCreate() method to bypass the exception*

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy);

I wont recommend using StrictMode though!

Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
0

Because you are request JSONObject in UI Thread. you must have to use in AsycTask like This. Or You should use runOnUiThread like This..

Community
  • 1
  • 1
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99