J'essaie de remplir des données XML à partir d'une API mais en obtenant Null. J'ai essayé un certain nombre de solutions de stackoverflow et c'est ma dernière itération. Pourquoi les données XML ne s'affichent-elles pas dans mon application après avoir appuyé sur le bouton? Je ne reçois aucun message d'erreur. Je l'ai séparé en deux classes. Une classe a l'interface utilisateur et sépare la chaîne entrée par l'utilisateur dans les deux champs correctement formatés pour une URL et possède une asyncTask. L'autre classe a l'instance d'URL et s'exécute.Android: Téléchargement de données XML à afficher dans l'application - obtention de Null
Je sais que l'URL est correctement formatage comme je l'ai essayé une println sur le dernier et cliqué dessus de la console et il est allé au bon xml. L'adresse de test j'ai utilisé était (rue: 2114 Bigelow Ave/zip: 98109) Voici le code:
GetProperty.java:
public class GetProperty {
public String address;
//URL with ZWSID
public static final String myURL = "http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=[REMOVED ID]";
public String finalUrl;
String line;
private static final boolean DEBUG = true;
public GetProperty(String address) {
this.address = address;
finalUrl = myURL + address;
System.out.println("The FINAL URL string is: " + finalUrl);
line = "";
}
public void load() throws MalformedURLException, IOException
{
//Create URL with address from above
URL url = new URL(finalUrl);
//URLConnection connection = url.openConnection();
InputStream stream = url.openStream();
BufferedReader br;
try
{
br = new BufferedReader(new InputStreamReader(stream));
// consume any data remaining in the input stream
while (br.readLine() != null) {
line = line + br.readLine(); }
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public String getLine() {
return line;
}
}
MainActivity.java:
public class MainActivity extends AppCompatActivity {
EditText getStreetET;
EditText getZipET;
Button getInfoBTN;
TextView xmlTextView;
String streetAddress;
String zipAddress;
String userAddress;
GetProperty property;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getStreetET = (EditText) findViewById(R.id.editText);
getZipET = (EditText) findViewById(R.id.editText2);
xmlTextView = (TextView) findViewById(R.id.textView);
getInfoBTN = (Button) findViewById(R.id.button);
//Get information about address user typed in edit text when BUTTON is clicked
getInfoBTN.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Get text from user and change it to a string
streetAddress = getStreetET.getText().toString();
//Add a + sign wherever there is a space in the street address
streetAddress = streetAddress.replaceAll(" ", "+");
//adding &address= for the URL
streetAddress = "&address=" + streetAddress;
zipAddress = "&citystatezip=" + getZipET.getText().toString();
//Combine street & zip into one string address
userAddress = streetAddress + zipAddress;
System.out.println("The user address without the URL is: " + userAddress);
//Make sure the user actually typed something
if(!containsWhiteSpace(userAddress)) {
getAddressInfoTask addressTask = new getAddressInfoTask();
addressTask.execute(userAddress);
}
//Test if user typed in correct address?
else {
xmlTextView.setText("");
}
}
});
}
public static boolean containsWhiteSpace(String userAddress) {
if (!hasLength(userAddress)) {
return false;
}
int strLen = userAddress.length();
for (int i = 0; i < strLen; i++) {
if (Character.isWhitespace(userAddress.charAt(i))) {
return true;
}
}
return false;
}
public static boolean hasLength(String userAddress) {
return (userAddress != null && userAddress.length() > 0);
}
//AsyncTask for separate thread due to internet connection
private class getAddressInfoTask extends AsyncTask<String, Void, GetProperty>
{
protected GetProperty doInBackground(String... params)
{
property = new GetProperty(userAddress);
try
{
property.load();
}
catch (IOException e)
{
e.printStackTrace();
}
return property;
}
protected void onPostExecute(GetProperty property)
{
//Place the XML in the TextView
xmlTextView.setText(property.getLine());
}
}
}
avez-vous essayé le débogage avec des points de rupture? – Raghunandan
J'ai essayé d'afficher votre fichier XML en utilisant cet outil: https://codebeautify.org/xmlviewer (charger depuis l'URL). Il dit: "Erreur: ville/état/code ZIP invalide ou manquant" (code: 501) donc le problème est avec votre lien –