makeHttpRequest崩溃了

时间:2013-11-02 15:50:20

标签: java android android-asynctask

我想从sql server检索数据,databench的网关是一个php脚本。没关系。

我创建了一个类ListView,一旦选择了一个项目,新的活动就是startet。启动此活动时,将完成http get请求:

当选择对象时,Activity1调用Activity2(“EditItemActivity”)

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) 
            {
                // getting values from selected ListItem
                String pid = ((TextView) view.findViewById(R.id.pid)).getText()
                        .toString();
                String catID = prodCatCrossList.get(pid);

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        EditItemActivity.class);
                // sending parameters to next activity
                in.putExtra(TAG_PID, pid);
                in.putExtra(TAG_CATID, catID);

                // starting new activity and expecting some response back
                startActivityForResult(in, 100);
            }

传输的参数很好我仔细检查过这个。

现在调用Activity2.oncreate:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_item);

    // getting product details from intent
    Intent i = getIntent();

    // getting product id (pid) from intent
    pid = i.getStringExtra(TAG_PID);
    catID = i.getStringExtra(TAG_CATID);

    // Getting complete product details in background thread
    new GetProductDetails().execute();

}

最后崩溃的异步任务:类GetProductDetails扩展了AsyncTask     {

    protected String doInBackground(String... args) 
    {
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                // Check for success tag
                int success;
                try {

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

                    JSONObject json = jsonParser.makeHttpRequest(
                            url_item_details, "GET", params);
                    // json success tag
                    success = json.getInt(TAG_SUCCESS);
                    if (success == 1) {
                            //...........
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();                        
                }
            }
        });

        return null;
    }

params值是正确的,json流中的语法也是正确的。

我很安静“makeHttpRequest”导致错误,但我不明白为什么?在activity1中,我已经从我的服务器成功检索了数据......

谁可以提供帮助?

创建以下错误消息:

11-02 16:27:12.053: E/AndroidRuntime(17563): FATAL EXCEPTION: main
11-02 16:27:12.053: E/AndroidRuntime(17563): android.os.NetworkOnMainThreadException
11-02 16:27:12.053: E/AndroidRuntime(17563):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at com.dafocus.shoporganizer.JSonParser.makeHttpRequest(JSonParser.java:63)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at com.dafocus.shoporganizer.EditItemActivity$GetProductDetails$1.run(EditItemActivity.java:136)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at android.os.Handler.handleCallback(Handler.java:615)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at android.os.Looper.loop(Looper.java:137)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at android.app.ActivityThread.main(ActivityThread.java:4921)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at java.lang.reflect.Method.invokeNative(Native Method)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at java.lang.reflect.Method.invoke(Method.java:511)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
11-02 16:27:12.053: E/AndroidRuntime(17563):    at dalvik.system.NativeStart.main(Native Method)
11-02 16:27:52.873: D/dalvikvm(17563): GC_CONCURRENT freed 142K, 13% free 10241K/11719K, paused 2ms+1ms, total 16ms

2 个答案:

答案 0 :(得分:2)

删除doInbackground中的runOnUiThread。您正在runOnUiThread内(在ui线程上)执行与网络相关的操作。

您需要在后台线程

上调用的doInbackground中运行与网络相关的操作

答案 1 :(得分:0)

就像@Raghunandan所说,你明确在主线程上做网络工作。负责处理所有Android应用程序的Dalvik虚拟机不允许这样做。这就是为什么你的应用程序在其主线程上发出网络(在本例中为JSON)请求时崩溃的原因。