gpt4 book ai didi

android - 我正在尝试解析 json 并下载一些 html 而不是 json 内容

转载 作者:行者123 更新时间:2023-11-29 01:44:09 25 4
gpt4 key购买 nike

我正在尝试从网站解析 json 内容并下载一些 html 文件而不是 json 对象。这是显示的代码和错误。(仅供引用,我正在解析 json 并将其显示在日志猫中)

这是MainActivity.java

   public class MainActivity extends Activity {

ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new MyAsyncTask().execute();
lv = (ListView) findViewById(R.id.listView1);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private class MyAsyncTask extends AsyncTask<String, String, String> {

String url = "http://walmartlabs.api.mashery.com/v1/taxonomy?format=json&apiKey=yhg57ygb3mdk8ywuwdsvgews";
ProgressDialog dialog;

@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Please wait...");
dialog.show();
}

@Override
protected String doInBackground(String... params) {
JSONParser parser = new JSONParser();
JSONObject array = parser.getJSON(url);
return null;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}

}

这是解析器类:JSONParser.java

public class JSONParser {
JSONObject jarray;
String line;

JSONObject getJSON(String url) {
final StringBuilder sb = new StringBuilder();
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
/* HttpGet httpPost = new HttpGet(url); */

try {
HttpResponse httpRes = httpClient.execute(httpPost);
HttpEntity entity = httpRes.getEntity();
InputStream stream = entity.getContent();
final BufferedReader reader = new BufferedReader(
new InputStreamReader(stream));

while ((line = reader.readLine()) != null) {
sb.append(line);
Log.i("PARSED", line);
}

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
jarray = new JSONObject(sb.toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

return jarray;
}

}

图像显示了从 json 中提取的数据及其错误 Error shown in the logcat

Continuation to the other image

最佳答案

在该 url 上尝试 POST 时,我遇到了与您相同的错误,但是 GET 返回:

{"categories":[ {
"id" : "5438",
"name" : "Apparel",
"path" : "Apparel",
"children" : [ {
"id" : "5438_426265",
"name" : "Accessories",
"path" : "Apparel/Accessories",
"children" : [ {
"id" : "5438_426265_1043621",
"name" : "Bandanas",
"path" : "Apparel/Accessories/Bandanas"
}, {
...

所以您需要做的是将 HttpPost 更改为 HttpGet

关于android - 我正在尝试解析 json 并下载一些 html 而不是 json 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22514141/

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