我想从XML Parsing
实施url
。下面是使用xml
解析的代码,但它不起作用。任何人都可以帮助我相应地获得response
。
public class MainActivity extends AppCompatActivity {
// Declare variables
TextView textview;
NodeList nodelist;
ProgressDialog pDialog;
// Insert image URL
// String URL = "http://www.androidbegin.com/tutorial/XMLParseTutorial.xml";
String URL = "my private url";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new DownloadXML().execute(URL);
textview = (TextView) findViewById(R.id.text);
}
// DownloadXML AsyncTask
private class DownloadXML extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressbar
pDialog = new ProgressDialog(MainActivity.this);
// Set progressbar title
pDialog.setTitle("Android Simple XML Parsing using DOM Tutorial");
// Set progressbar message
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
// Show progressbar
pDialog.show();
}
@Override
protected Void doInBackground(String... Url) {
try {
URL url = new URL(Url[0]);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// Download the XML file
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
Log.e("testing "," ==>");
// Locate the Tag Name
// nodelist = doc.getElementsByTagName("item");
nodelist = doc.getElementsByTagName("ApiResponseOfTwiliosZ_PlQ1mzg");
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
for (int temp = 0; temp < nodelist.getLength(); temp++) {
Node nNode = nodelist.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
// Set the texts into TextViews from item nodes
// Get the title
Log.e("Message checking "," ===> "+getNode("ResponseStatus", eElement));
}
}
// Close progressbar
pDialog.dismiss();
}
}
// getNode function
private static String getNode(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
.getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
}
并且错误如下所示。
java.lang.NullPointerException: Attempt to invoke interface method 'int org.w3c.dom.NodeList.getLength()' on a null object reference
我希望从此回复中获得token
。
以下是我的api response
<ApiResponseOfTwiliosZ_PlQ1mzg xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.datacontract.org/2004/07/Healthcare4Free.WebApi.Models">
<Data i:nil="true"/>
<ResponseStatus>
<Message>Success</Message>
<StatusCode>200</StatusCode>
<TwilioToken>
<Token>
eyJjdHkiOiJ0d2lsaW8tZnBhO3Y9MSIsInR5cCI6IkpXVCIsImFsZyI6IkhTMjU2In0.eyJpc3MiOiJTSzMxZDVjM2NiNDVmMjQ2ZjI5NDY4MTFlOGQxYWZlYjM4IiwiZXhwIjoxNTA4Njc4NzYzLCJqdGkiOiJTSzMxZDVjM2NiNDVmMjQ2ZjI5NDY4MTFlOGQxYWZlYjM4LTE1MDg2NzUxNjMiLCJzdWIiOiJBQzdlNDBhZjY1YTYwMGYyYmQ3Yjg2Zjk1OTFkZjM4ZTFiIiwiZ3JhbnRzIjp7ImlkZW50aXR5IjoiaWRlbnRpdHkiLCJ2aWRlbyI6eyJyb29tIjoicm9vbUlkIn19fQ.yHKcOOiUrVZKYsQ3110YzgprRpODd9fp1dQLQFNw7Gw
</Token>
</TwilioToken>
</ResponseStatus>
</ApiResponseOfTwiliosZ_PlQ1mzg>
答案 0 :(得分:0)
要让令牌更新您的代码:
在doInBackground()
中Log.e("Message checking ", " ===> " + getNode("Token", eElement));
在你的onPostExecute()
中TextView textview;
NodeList nodelist;
ProgressDialog pDialog;
String URL = "http://json-gen.com/rest/service/get/uCSFzJgDKrjq7DKCNiQtm";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new DownloadXML().execute(URL);
textview = (TextView) findViewById(R.id.text);
}
// DownloadXML AsyncTask
private class DownloadXML extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressbar
pDialog = new ProgressDialog(MainActivity.this);
// Set progressbar title
pDialog.setTitle("Android Simple XML Parsing using DOM Tutorial");
// Set progressbar message
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
// Show progressbar
pDialog.show();
}
@Override
protected Void doInBackground(String... Url) {
try {
URL url = new URL(Url[0]);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// Download the XML file
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
Log.e("testing ", " ==>");
// Locate the Tag Name
// nodelist = doc.getElementsByTagName("item");
nodelist = doc.getElementsByTagName("TwilioToken");
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
Node nNode = nodelist.item(0);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
// Set the texts into TextViews from item nodes
// Get the title
Log.e("Message checking ", " ===> " + getNode("Token", eElement));
textview.setText(getNode("Token", eElement));
}
// Close progressbar
pDialog.dismiss();
}
}
// getNode function
private static String getNode(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
.getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
以下是完整的代码:
library(relsurv)
library(survminer)
fit.hist <- rs.surv(Surv(time,cens)~ordered(Hist)+
ratetable(age = age, sex = sex, year = year),
ratetable = poptab, method="pohar-perme")