当我的api级别设置为8时,一切都很好。但是当我尝试将minSdkVersion设置为11时,我获取xml的http请求不起作用。 if上有一个例外。 这是代码:
URL url;
String ciao="";
try {
url = new URL("myurl");
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(in);
DOMSource domSource = new DOMSource(dom);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
ciao=writer.toString();
}
}
catch(Exception e)
{
}
return ciao;
答案 0 :(得分:2)
原因是,从API 11开始,您必须在后台线程上进行所有网络访问。如果不这样做,您将收到NetworkOnMainThreadException
。这是为了防止用户界面在访问网络时无响应。
有关详细信息,请参阅here
答案 1 :(得分:0)
确保您的清单有以下声明:
<uses-sdk android:minSdkVersion="number-of-version-you-want" />
然后
注意:强> 使用AsyncTask以避免主线程上出现ANR和NetworkOnMainThreadException异常。
修改强> 请找到你的asynctask代码:我还使用了progressDialog来显示&#34; loading请等待&#34;如果是一种消息,URL调用需要更长的时间。
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
.... // Some code here
// Async task can also be called from within an onclick code.
new AsyncTaskOperation().execute("");
}
/* Async Task called to avoid Android Network On Main Thread Exception. Web services need to be consumed only in background. */
private class AsyncTaskOperation extends AsyncTask <String, Void, Void>
{
private ProgressDialog Dialog = new ProgressDialog(LoginActivity.this);
String ciao="";
protected void onPreExecute() {
// Display the loading spinner
Dialog.setMessage("Loading... Please wait.. ");
Dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
Dialog.setInverseBackgroundForced(false);
Dialog.setCancelable(false);
Dialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.progressbar_new));
Dialog.show();
}
@Override
protected Void doInBackground(String... paramsObj) {
try {
url = new URL("myurl");
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(in);
DOMSource domSource = new DOMSource(dom);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
ciao=writer.toString();
}
}
catch(Exception e)
{
}
return null;
}
protected void onPostExecute(Void unused)
{
// Close progress dialog
Dialog.dismiss();
// Do actions with ciao string
} // End of method onPostExecute
} // End of class AsyncTaskOperation
} // MainActivity结束
答案 2 :(得分:0)
API级别8允许从UI线程发出http请求。但是在更高版本中,在UI线程上发出http请求会抛出一个名为NetworkonMainThread异常的异常。要确认问题,请发布您的错误日志