我正在尝试从API填充XML数据但是获取Null。我尝试了stackoverflow的一些解决方案,这是我最新的迭代。按下按钮后,为什么我没有将XML数据填充到我的应用程序中?我没有收到任何错误消息。我将其分为两类。一个类具有UI,并将用户输入的字符串分隔为为URL正确格式化的两个字段,并具有AsyncTask。另一个类具有URL实例并执行。
我知道url格式正确,因为我在最后一个上尝试了println并从控制台点击它,然后它转到了正确的xml。我使用的测试地址是(街道:2114 Bigelow 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());
}
}
}
答案 0 :(得分: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发现普通流为空但编码人员必须手动执行此操作时,Chrome可以在窗口上路由错误。
PS:您应该首先使用postman来测试API上的请求。然后在你确定它有效的网址上测试后,在你的程序上进行测试。