0

收到此错误“无法执行活动的方法”......有人可以帮助为什么会发生这种情况吗?

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;
}
}

我试图通过单击按钮从该应用程序中读取 URL...但无法这样做...有人可以帮助我吗?

4

3 回答 3

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!

于 2013-06-17T10:41:09.177 回答
1

JSONObject json = jsonParser.makeHttpRequest( url_detials, "GET", params);您正在UI 线程上执行 HTTP 请求 ( )。
将这些东西移到AsyncTask.

我应该如何修改这个?

于 2013-06-17T10:33:33.357 回答
0

因为您在 UI 线程中请求 JSONObject。你必须像这样在 AsycTask 中使用。或者你应该像这样使用 runOnUiThread ..

于 2013-06-17T10:40:47.637 回答