我正在尝试从网站解析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();
}
}
}
}
这是Parser类: 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提取的数据及其错误
答案 0 :(得分:1)
在该网址上尝试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
。