0
我有這個類在我發送SOAP請求到服務器。我有我的主類,一個AsyncTask類,一個解析器類,一個接口。我的問題是我總是得到這個NullpointerException。我想要做的是在我的主要活動我會發送一個字符串到AsyncTask,然後在AsyncTask onPostExecute的結果,我需要將它傳遞給解析器類,在這一部分我有問題。我使用我以前的問題的接口,但它不工作。onPostExecute上的java.lang.NullPointerException?
的AsyncTask類:
public class TestAsyncTask extends AsyncTask<String, Void, String> {
public TestInterface delegate = null;
String soapString = "http://-------------";
String serverString = "https:-----------";
@Override
protected String doInBackground(String... sString) {
String yourValue = sString[0];
String resString = null; //This will be the storage of the response.
try {
//Uses URL and HttpURLConnection for server connection
URL url = new URL(serverString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
urlConnection.setChunkedStreamingMode(0);
urlConnection.addRequestProperty("SOAPAction", soapString);
urlConnection.addRequestProperty("Content-Type", "text/xml; charset=utf-8");
urlConnection.addRequestProperty("Content-Length", "" + yourValue.length());
urlConnection.setRequestMethod(HttpPost.METHOD_NAME);
// Using OutputStream and Writer to send a request to the server.
OutputStream outputStream = urlConnection.getOutputStream();
Writer writer = new OutputStreamWriter(outputStream);
writer.write(yourValue);
writer.flush();
writer.close();
//Using InputStream to get the response of the request from the server.
InputStream inPutStream = urlConnection.getInputStream();
BufferedReader buffReader = new BufferedReader(new InputStreamReader(inPutStream));
ByteArrayBuffer byteBuffer = new ByteArrayBuffer(50);
int resint = urlConnection.getResponseCode();
while ((resint = buffReader.read()) != -1) {
byteBuffer.append(resint);
}
resString = new String(byteBuffer.toByteArray());
} catch (Exception aException) {
resString = aException.getMessage();
}
return resString;
}
protected void onPostExecute(String aReturnValueString) {
//I THINK HERE IS WHERE THE PROBLEM EXIST
delegate.processFinish(aReturnValueString);
}
}
接口,這林不知道:
public interface TestInterface {
void processFinish(String output);
}
解析類:
public class TestParser implements TestInterface {
TestAsyncTask a= new TestAsyncTask();
TextView oTextView[], aTextView[], pTextView[], dTextView[]; //Create a new TextView array to store the parsing results.
private LinearLayout linearLayout;
int pars;
Context contxt;
//Constructor
public TestParser(Context cContext){
contxt = cContext; //Pass Context to constructor
}
//Getter for LinearLayout.
public LinearLayout getLinearLayout(){
return linearLayout;
}
//Setter for LinearLayout.
public void setLinearLayout(LinearLayout aSetterLinearLayout){
this.linearLayout = aSetterLinearLayout;
}
public void onCreate(Bundle savedInstanceState) {
a.delegate = this;
}
public void processFinish(String output) {
// TODO Auto-generated method stub
linearLayout = new LinearLayout(this);
//The try/catch statement encloses some code and is used to handle errors and exceptions that might occur in that code.
try {
//Use SAXParser(Simple API for XML) to handle the parsing of XML(Response). */
SAXParserFactory saxParF = SAXParserFactory.newInstance();
SAXParser sAXPar = saxParF.newSAXParser();
XMLReader xMLReader = sAXPar.getXMLReader();
// Create handler to handle XML Tags (GBXMLHandler extends DefaultHandler).
TestXMLHandler xMLHandler = new TestXMLHandler();
xMLReader.setContentHandler(xMLHandler);
InputSource inputSource = new InputSource(new StringReader(output));
xMLReader.parse(inputSource);
TestData data = TestXMLHandler.aData;
int aone = data.getoName().size();
int atwo = data.getDes().size();
oTextView = new TextView[aone];
aTextView = new TextView[aone];
dTextView = new TextView[atwo];
pTextView = new TextView[atwo];
//The for statement provides a compact way to iterate over a range of values.
for (pars = 0; pars + 1 < atwo; pars++) {
dTextView[pars] = new TextView(getApplicationContext());
dTextView[pars].setText("Description = " + data.getDes().get(pars));
linearLayout.addView(dTextView[pars]);
pTextView[pars] = new TextView(getApplicationContext());
pTextView[pars].setText("RegularSellUnitPrice = " + data.getRegularSellUnitPrice().get(pars));
linearLayout.addView(pTextView[pars]);
}
for (pars = 0; pars < aone; pars++){
oTextView[pars] = new TextView(getApplicationContext());
oTextView[pars].setText("OperatorName = " + data.getoName().get(pars));
linearLayout.addView(oTextView[pars]);
aTextView[pars] = new TextView(getApplicationContext());
aTextView[pars].setText("type = " + data.getType().get(pars));
linearLayout.addView(aTextView[pars]);
}
} catch (Exception e) {
System.out.println("XML Pasing Exception = " + e);
}
}
}
你可以張貼堆棧跟蹤?這是很多代碼,只是被要求篩選並被告知有某處出現錯誤。 –
哦對不起。在調試器中運行應用程序時。在我的onPostExecute中,存在錯誤。 – Stella
您的'TestParser'類不正確。首先,你不應該實例化一個'Activity',否則''onCreate'回調將不會被調用(所以'delegate'將爲null,因此'NullPointerException') – Luksprog