我一直得到這個問題 例外:連接超時 我在另一臺設備上運行我的項目,所以我必須使用IP地址。所以我改變localhost:8080/lab/lab1.xml
爲http://192.168.1.5/lab/lab1.xml
現在我運行後,它加載一段時間後加載它的設備上,然後顯示 空白頁。當我檢查Logcat它顯示異常:連接超時 請幫助我。由於Android:例外:連接超時
因此,這裏是MainActivity.java
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tvResponse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResponse = (TextView) findViewById(R.id.tvResponse);
new PostAsync().execute();
}
class PostAsync extends AsyncTask<Void, Void, Void> {
ProgressDialog pd;
XMLHelper helper;
@Override
protected void onPreExecute() {
pd = ProgressDialog.show(MainActivity.this, "by Es", "Loading", true, false);
}
@Override
protected Void doInBackground(Void... arg0) {
helper = new XMLHelper();
helper.get();
return null;
}
@Override
protected void onPostExecute(Void result) {
StringBuilder builder = new StringBuilder();
for(EventValue event : helper.events) {
builder.append("\nWhat: " + event.getWhat());
builder.append("\nWhen: " + event.getWhen());
builder.append("\nWhere: " + event.getWhere());
builder.append("\n");
}
tvResponse.setText(builder.toString());
pd.dismiss();
}
}
}
這裏是XMLHelper.java
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
public class XMLHelper extends DefaultHandler {
/**
* The URL to be parsed
*/
private String URL_MAIN = "http://192.168.1.5/lab/lab1.xml";
String TAG = "XMLHelper";
Boolean currTag = false;
String currTagVal = "";
public EventValue event = null;
public ArrayList<EventValue> events = new ArrayList<EventValue>();
public void get() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser mSaxParser = factory.newSAXParser();
XMLReader mXmlReader = mSaxParser.getXMLReader();
mXmlReader.setContentHandler(this);
InputStream mInputStream = new URL(URL_MAIN).openStream();
mXmlReader.parse(new InputSource(mInputStream));
} catch(Exception e) {
// Exceptions can be handled for different types
// But, this is about XML Parsing not about Exception Handling
Log.e(TAG, "Exception: " + e.getMessage());
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(currTag) {
currTagVal = currTagVal + new String(ch, start, length);
currTag = false;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currTag = false;
if(localName.equalsIgnoreCase("what"))
event.setWhat(currTagVal);
else if(localName.equalsIgnoreCase("when"))
event.setWhen(currTagVal);
else if(localName.equalsIgnoreCase("where"))
event.setWhere(currTagVal);
else if(localName.equalsIgnoreCase("event"))
events.add(event);
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
Log.i(TAG, "TAG: " + localName);
currTag = true;
currTagVal = "";
if(localName.equals("event"))
event = new EventValue();
}
}
這裏是EventValue.java
public class EventValue {
String what, when, where;
public String getWhat() {
return what;
}
public void setWhat(String what) {
this.what = what;
}
public String getWhen() {
return when;
}
public void setWhen(String when) {
this.when = when;
}
public String getWhere() {
return where;
}
public void setWhere(String where) {
this.where = where;
}
}
這裏是XML文件lab1.xml(localhost:8080/lab/lab1.xml)
<event>
<what>Summer</what>
<when>March1</when>
<where>--</where>
</event>
<event>
<what>asdasdas</what>
<when>March 2</when>
<where>asasas</where>
</event>
<event>
<what>asdasdq</what>
<when>asdasdx</when>
<where>asdasdf</where>
</event>
我假設你在清單中設置了互聯網權限,看起來你錯過了其他平板電腦ip地址的端口號。 – chaitanya
你可以在應用程序之外自己訪問xml文件嗎?目錄中是否有密碼,以便在您獲得訪問權限之前提示您? – Gyhth
@chaitanya你是什麼意思錯過了其他平板電腦的IP地址端口號? – dotamon