我搜寻了整个互联网,但没有答案可以帮助我。
我正在尝试从MySQL数据库中获取数据字符串,但仅收到此'andoid.os.NetworkOnMainThreadException'
错误。有人可以帮我吗?
这是我的代码:
public String getInformationForBarcode(String barcode) {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.example.com"); // Yes, i changed this in live code to my url with data in it
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpClient.execute(httpPost,
responseHandler);
return response.trim();
} catch (Exception e) {
System.out.println("ERROR : " + e.toString());
return "error";
}
}
是的,我将
<uses-permission android:name="android.permission.INTERNET" />
。
我所得到的只是LogCat中的错误消息:(
希望你们中的一个能帮助我!
谢谢回答。
当您尝试建立与数据库的连接时,最好在另一个线程中执行此操作,以避免应用程序崩溃,因为此类操作可能需要很长时间。您可以用来AsyncTask
在后台执行这些操作。请访问开发者网站,以获取有关任务http://developer.android.com/reference/android/os/AsyncTask.html的更多信息
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句