gpt4 book ai didi

java - Android:下载 XML 数据以在应用程序中显示 - 获取 Null

转载 作者:太空宇宙 更新时间:2023-11-04 11:09:55 25 4
gpt4 key购买 nike

我尝试从 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());
}
}


}

最佳答案

我尝试了将变量 finalUrl 设置为 https://www.google.com 的代码,它成功地在应用程序中打印了 xml。因此代码的检索部分可以工作。

然后我尝试了基于 this blog 的请求并像这样直接在浏览器上使用您的 API key (您建议不要共享它):

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

响应是 xml 格式,但内容如下:

错误:此帐户无权执行此 API 调用

您的代码上的完全相同的请求给出响应null。这是因为错误码流和正常码流不一样。当 Chrome 发现正常流为 null 时,它可以在窗口上路由错误,但编码器必须手动执行此操作。

PS:您应该先使用postman测试API上的请求。在您确定它有效的网址上进行测试后,然后在您的程序上进行测试。

关于java - Android:下载 XML 数据以在应用程序中显示 - 获取 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46132267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com