我正在尝试将 seismic_text.java 文件中的函数 getUrlContents(string)调用到我的 MainActivity.java 文件中。如何从文件中的任何位置调用该函数?任何信息或提示表示赞赏。我在下面包含我的文件。
这是我的 MainActivity.java :
package bt.alfaquake;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.NotificationManager;
import android.content.Intent;
import android.view.View;
import android.app.PendingIntent;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.NotificationCompat;
import bt.alfaquake.seismic_text;
public class MainActivity extends AppCompatActivity {
NotificationCompat.Builder notification;
private static final int uniqueID = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notification = new NotificationCompat.Builder(this);
}
}
这是我的 seismic_text.java : 包装bt.alfaquake;
import java.net.*;
import java.io.*;
public class seismic_text {
public static String getUrlContents(String theUrl) {
StringBuilder content = new StringBuilder();
try
{
URL url = new URL(theUrl);
URLConnection urlConnection = url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null)
{
content.append(line + "\n");
}
bufferedReader.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return content.toString();
}
}
}
答案 0 :(得分:0)
只需在MainActivty.java中调用它:
seismic_text.getUrlContents(url);
答案 1 :(得分:0)
你可以调用seismic_text.getUrlContents(url);但它会导致NetworkOnMainThreadException
只需将此调用包装到Simple AsynkTask。
new MyTask().execute();
并从您的代码中调用它:
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Request
{
private string sNameField;
/// <remarks/>
public string SName
{
get
{
return this.sNameField;
}
set
{
this.sNameField = value;
}
}
/// <remarks/>
public Message Message
{
get
{
return this.messageField;
}
set
{
this.messageField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = false)]
public partial class Message
{
private AddO addOField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://pd.com")]
public AddO AddO
{
get
{
return this.addOField;
}
set
{
this.addOField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://pd.com")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://pd.com", IsNullable = false)]
public partial class AddO
{
private CaseD caseD;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.a.com")]
public CaseD CaseD
{
get
{
return this.caseDField;
}
set
{
this.caseDField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.a.com")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.a.com", IsNullable = false)]
public partial class CaseD
{
private CaseA caseAField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.z.com")]
public CaseA CaseA
{
get
{
return this.caseAField;
}
set
{
this.caseAField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.z.com")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.z.com", IsNullable = false)]
public partial class CaseA
{
private Doc docField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.a.com")]
public DocuE DocE
{
get
{
return this.docEField;
}
set
{
this.docEField = value;
}
}
}