次のような非常に基本的な XML ファイルを解析しようとしています。
<xml>
<customer>
<CustID>10</CustID>
<custvar1>10</custvar1>
<custvar2>10</custvar2>
<custvar3>10</custvar3>
</customer>
</xml>
基本的に結果が出ません。なんらかの理由で、最後に行ったアプリを終了しても、エラーは発生しません。今日はこれに 8 時間も費やしたので、何か助けていただければ幸いです。また、解析で何をしているのか完全にはわかりません。一般的な理解はありますが、それほどしっかりしたものはありません。
私はそれを 2 つのスレッドに分割しました。少なくとも、分割したと思います。私は、このすべてのコードでどこが間違っていたかを探しています。どんな助けでも大歓迎です!!
助けてくれたみんなに前もって感謝します!!
コードは次のとおりです。
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Xml;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button getData = (Button) findViewById(R.id.getData);
getData.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
background();
Toast toast = Toast.makeText(getApplicationContext(), "Toast 1", Toast.LENGTH_SHORT);
toast.show();
}
});
}
public class Customer
{
public final String custID;
public final String var1;
public final String var2;
public final String var3;
private Customer()
{
custID = null;
var1 = null;
var2 = null;
var3 = null;
}
private Customer(String custID, String var1, String var2, String var3)
{
this.custID = custID;
this.var1 = var1;
this.var2 = var2;
this.var3 = var3;
}
}
private Customer readCustomer(XmlPullParser parser) throws XmlPullParserException, IOException
{
parser.require(XmlPullParser.START_TAG, null, "customer");
String custID = null;
String var1 = null;
String var2 = null;
String var3 = null;
while(parser.next() != XmlPullParser.END_TAG)
{
if (parser.getEventType() != XmlPullParser.START_TAG)
{
continue;
}
String name = parser.getName();
if (name.equals("CustID"))
{
custID = readCustID(parser);
} else if (name.equals("custvar1"))
{
var1 = readVar1(parser);
} else if (name.equals("custvar2"))
{
var2 = readVar2(parser);
} else if (name.equals("custvar3"))
{
var3 = readVar3(parser);
} else
{
skip(parser);
}
}
return new Customer(custID, var1, var2, var3);
}
private String readCustID(XmlPullParser parser) throws IOException, XmlPullParserException
{
parser.require(XmlPullParser.START_TAG, null, "CustID");
final String custID = readText(parser);
parser.require(XmlPullParser.END_TAG, null, "CustID");
final Handler handler;
handler = new Handler();
handler.post(new Runnable() {
public void run()
{
//Display toast
Toast toast = Toast.makeText(getApplicationContext(), "ID: " + custID, Toast.LENGTH_SHORT);
toast.show();
}
});
return custID;
}
private String readVar1(XmlPullParser parser) throws IOException, XmlPullParserException
{
parser.require(XmlPullParser.START_TAG, null, "CustID");
String var1 = readText(parser);
parser.require(XmlPullParser.END_TAG, null, "CustID");
return var1;
}
private String readVar2(XmlPullParser parser) throws IOException, XmlPullParserException
{
parser.require(XmlPullParser.START_TAG, null, "CustID");
String var2 = readText(parser);
parser.require(XmlPullParser.END_TAG, null, "CustID");
return var2;
}
private String readVar3(XmlPullParser parser) throws IOException, XmlPullParserException
{
parser.require(XmlPullParser.START_TAG, null, "CustID");
String var3 = readText(parser);
parser.require(XmlPullParser.END_TAG, null, "CustID");
return var3;
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException
{
String result = "";
if (parser.next() == XmlPullParser.TEXT)
{
result = parser.getText();
parser.nextTag();
}
return result;
}
private void skip(XmlPullParser parser) throws IOException, XmlPullParserException
{
if (parser.getEventType() != XmlPullParser.START_TAG)
{
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0)
{
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth --;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}
public class XMLparser
{
public Customer parse(InputStream in) throws XmlPullParserException, IOException {
try
{
XmlPullParser parser = Xml.newPullParser();
parser.setInput(in, null);
parser.nextTag();
return readXML(parser);
} finally {
in.close();
}
}
private Customer readXML(XmlPullParser parser) throws XmlPullParserException, IOException
{
Customer custData = new Customer();
parser.require(XmlPullParser.START_TAG, null, "xml");
while (parser.next() != XmlPullParser.END_TAG)
{
if (parser.getEventType() != XmlPullParser.START_TAG)
{
continue;
}
String name = parser.getName();
//Look for customer tag
if (name.equals("customer"))
{
custData = readCustomer(parser);
}
else
{
skip(parser);
}
}
return custData;
}
}
private InputStream downloadUrl(String urlString) throws IOException
{
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
//start the query
conn.connect();
InputStream stream = conn.getInputStream();
return stream;
}
private void background()
{
new Thread(new Runnable() {
public void run(){
//Delcare handler to allow UI modifications
final Handler handler;
handler = new Handler();
final TextView TextVar1 = (TextView) findViewById(R.id.custvar1);
final TextView TextVar2 = (TextView) findViewById(R.id.custvar2);
final TextView TextVar3 = (TextView) findViewById(R.id.custvar3);
InputStream stream = null;
String URL = "***removed***";
final TextView TextError = (TextView) findViewById(R.id.error);
Customer customer = new Customer(null,null,null,null);
XMLparser xmlParser = new XMLparser();
try
{
stream = downloadUrl(URL);
customer = xmlParser.parse(stream);
}
catch (IOException e)
{
TextError.setText(getResources().getString(R.string.connection_error));
}
catch (XmlPullParserException e)
{
TextError.setText(getResources().getString(R.string.connection_error));
}
finally
{
try {
stream.close();
} catch (IOException e) {
//
}
}
final Customer data = customer;
handler.post(new Runnable() {
public void run()
{
//Display toast
Toast toast = Toast.makeText(getApplicationContext(), "Toast 2", Toast.LENGTH_SHORT);
toast.show();
//Set Text Views to show data
TextVar1.setText(data.var1);
TextVar2.setText(data.var2);
TextVar3.setText(data.var3);
}
});
}
});
}
}