hi ,
i want to fetch data from below sparkfun url to my andriod application
it is not fetching data specially it is not working after below line of code
SONObject jsonObject = new JSONObject(response);
any help will be appreciated.
Thanks,
vipul
please find code below
[/code
ublic class Main3Activity extends AppCompatActivity implements View.OnClickListener {
private static final String URL =
"http://data.sparkfun.com/output/9JNxdKV1opHRol0d81g1.json";
private Views mViews;
private BookAdapter booksAdapter;
private ArrayList<Book> books;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
mViews = new Views();
mViews.fetchBooksButton.setOnClickListener(this);
books = new ArrayList<>();
}
@Override
public void onClick(View v) {
new BookDownloadTask().execute(URL);
}
class BookDownloadTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
mViews.progressBar.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(String[] params) {
try {
java.net.URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(reader);
StringBuilder stringBuilder = new StringBuilder();
String tempString;
while ((tempString = bufferedReader.readLine()) != null) {
stringBuilder.append(tempString);
stringBuilder.append("\n");
}
return stringBuilder.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String response) {
mViews.progressBar.setVisibility(View.GONE);
if (response != null) {
parseJsonResponse(response);
}
}
}
private void parseJsonResponse(String response) {
try {
Toast.makeText(this, "response is ok", Toast.LENGTH_SHORT).show();
JSONObject jsonObject = new JSONObject(response);
//String res = response;
// Toast.makeText(this, res, Toast.LENGTH_SHORT).show();
JSONArray bookArray = jsonObject.getJSONArray("");
for (int i = 0; i < bookArray.length(); i++) {
JSONObject bookObject = bookArray.getJSONObject(i);
Toast.makeText(this, "first object", Toast.LENGTH_SHORT).show();
String faucet = jsonObject.getString("faucet");
int humidity = jsonObject.getInt("humidity");
int watermeter = jsonObject.getInt("watermeter");
String timestamp = jsonObject.getString("timestamp");
books.add(new Book(faucet, humidity, watermeter, timestamp));
}
booksAdapter = new BookAdapter(Main3Activity.this, books);
mViews.bookList.setAdapter(booksAdapter);
mViews.bookList.setVisibility(View.VISIBLE);
} catch (Exception e) {
e.printStackTrace();
}
}
class Views {
final ListView bookList;
final ProgressBar progressBar;
final Button fetchBooksButton;
public Views() {
bookList = (ListView) findViewById(R.id.booksList);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
fetchBooksButton = (Button) findViewById(R.id.fetchBookButton);
}
}
}
]