我正在尝试从此网址中抓取图书标题:http://api.nytimes.com/svc/books/v2/lists/2010-10-01/trade-fiction-paperback?api-key=c775dd98c5a108e9ebc790ca2c47ac19:18:74338855
我已使用以下代码从此网址中抓取信息,该网址适用于该场合:http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44db6a862fba0b067b1930da0d769e98
它适用于第二个网址,因为只有一个"天气"我正在刮的字符串。但是在另一个网址中有几个" book_details"字符串,我不知道如何从每个标题中删除标题。
这是我的代码,它返回JSONException:book_details没有值。
public class MainActivity extends AppCompatActivity {
public class DownloadTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection)url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1){
char current = (char) data;
result += current;
data = reader.read();
}
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONObject jsonObject = new JSONObject(result);
String bookInfo = jsonObject.getString("book_details");
Log.i("Book Info", bookInfo);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DownloadTask task = new DownloadTask();
task.execute("http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44db6a862fba0b067b1930da0d769e98");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我认为问题出在onPostExecute方法中。任何帮助将不胜感激。
答案 0 :(得分:1)
您的代码的一个大问题是您没有正确地循环结果。您收到的是每本书的JSONArray JSONArray,因此您需要正确遍历每个数组。下面是一个基本示例,说明了提取名称和说明。从那里你可以真正提取你想要的任何信息。但我强烈建议学习如何在Android中解析JSONObjects和Arrays。
t=0
a=5
b=[]
b.append(1)
while a!=0:
b.append(a-1)
a=a-1
print b
<强>输出强>
其他资源
http://developer.android.com/reference/org/json/JSONObject.html
http://www.tutorialspoint.com/android/android_json_parser.htm