gpt4 book ai didi

java - Android:异常:连接超时

转载 作者:行者123 更新时间:2023-12-01 13:11:53 26 4
gpt4 key购买 nike

我一直遇到这个问题 异常:连接超时我正在另一台设备上运行我的项目,因此我必须使用 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>

最佳答案

如果您使用端口 8080,那么您应该在连接 URL 中使用该端口:

http://192.168.1.5:8080/lab/lab1.xml

最好检查一下您是否可以使用另一台计算机上的浏览器访问该 URL。

关于java - Android:异常:连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22793812/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com