2017-09-09 39 views
1

我想从API填充XML数据,但得到空值。我尝试了很多来自于stackoverflow的解决方案,这是我最近的一次迭代。为什么按下按钮后我没有将XML数据填充到我的应用程序中?我没有收到任何错误消息。我把它分成两类。一个类具有UI,并将用户输入的字符串分隔为正确格式化为URL的两个字段,并具有AsyncTask。另一个类有URL实例并执行。Android:下载XML数据以显示在应用程序中 - 获取空值

 

我知道网址正确格式化当我试着在最后一个一个println,并点击它从控制台,它去到正确的XML。我所使用的测试地址是(街道:2114比奇洛AVE/ZIP:98109) 下面是代码:

 

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()); 
     } 
    } 


} 
+1

你是否尝试用断点调试? – Raghunandan

+1

我试图通过使用这个工具来查看你的xml:https://codebeautify.org/xmlviewer(从URL加载)。它说:“错误:无效或缺失的城市/州/邮编参数”(代码:501)所以问题出在你的链接 –

回答

0

我试过你的代码,变量finalUrl设置为https://www.google.com,它设法在应用程序中打印xml。所以你的代码的检索部分起作用。

我又试图基于this blog和使用API​​密钥的请求(你会推荐不分享)像这样的,直接在浏览器上:

http://www.zillow.com/webservice/GetSearchResults.htm?zws-id=X1-ZWz1fz4ljopwjv_4wzn5&address=1600%20Pennsylvania%20Ave&citystatezip=20500 

的响应XML格式,但它说以下内容:

Error: this account is not authorized to execute this API call

对你的代码完全相同的要求予以响应null。这是因为错误和正常流不一样。当Chrome发现普通流为空,但编码器必须手动完成时,才能在窗口上发送错误。 PS:你应该先使用邮递员在API上测试请求。然后在您测试它的url后确定它可以正常工作,然后在程序上测试它。

+0

喜哈维尔。感谢您的回复,我一直在跟着,直到下半场。 “错误和正常流是不一样的”是什么意思?当它说“错误:这个账户没有被授权执行这个API调用”是否表示我需要请求Zillow给我一些更高级别的账户? –

+0

是的。我认为你的问题与你的帐户有关。此外,要访问错误流,从您编写的代码中,最简单的方法是使用android SDK中的HttpURLConnection。我让你看看文档。 –

相关问题