我想从 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