我收到此错误“无法执行活动的方法”......有人可以帮助解决这个问题的原因吗?
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
尝试执行时......
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;
}
}
我试图通过点击按钮来读取此应用中的网址...但无法执行此操作...有人可以帮助我吗?
答案 0 :(得分:1)
您正在UI线程上执行HTTP请求(JSONObject json = jsonParser.makeHttpRequest(
url_detials, "GET", params);
)。
将这些内容移到AsyncTask
。
答案 1 :(得分:1)
您应该使用AsyncTask。 Android不允许在主UI线程上运行http请求。
JSONObject json = jsonParser.makeHttpRequest(url_detials, "GET", params);
如果您想要快速测试,可以在onCreate()方法中添加它以绕过异常*
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
我不推荐使用StrictMode!
答案 2 :(得分:0)