我一直在遇到这个问题
例外:连接超时
我在另一台设备上运行我的项目,所以我必须使用IP地址。所以我将localhost:8080/lab/lab1.xml
更改为http://192.168.1.5/lab/lab1.xml
现在我运行并在加载后在设备上加载一段时间然后显示一个
空白页。当我检查Logcat时,它显示异常:连接超时
请帮帮我。感谢
所以这是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>
答案 0 :(得分:1)
如果您使用的是端口8080,那么您应该在连接URL中使用它:
http://192.168.1.5:8080/lab/lab1.xml
检查您是否可以使用其他计算机上的浏览器访问该网址也是一个好主意。