0

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

我创建了一个 ListView 类,一旦选择了一个项目,就会启动一个新的活动。当这个活动开始时,一个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();

}

最后是崩溃的异步任务:class GetProductDetails extends 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;
    }

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

我很确定“makeHttpRequest”会导致错误,但我不明白为什么?在活动 1 中,我已经从我的服务器成功检索了数据......

谁能帮忙?

创建以下错误消息:

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
4

2 回答 2

2

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

您需要运行doInbackground在后台线程上调用的网络相关操作

于 2013-11-02T15:52:38.230 回答
0

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

于 2013-11-02T15:54:51.400 回答