Issue
I am using async
task for getting Json
data from a url
.
as you can see, in the log cat , i am getting url output but still catch block is getting executed and i am unable to figure out error
Code:
public class MainActivity extends AppCompatActivity {
Scanner scanner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
weatherTask task = new weatherTask();
task.execute();
}
private class weatherTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
try{
URL url = new URL("http://api.openweathermap.org/data/2.5/weather?lat=28.732754&lon=77.175051&appid=e6a99aef963a3e835da961d3cf9aeeb6");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
scanner = new Scanner(in);
scanner.useDelimiter("\\A");
boolean has_input = scanner.hasNext();
if(has_input){
Log.d("url output",scanner.next());
String result = scanner.next().toString();
return result;
}
}
catch (Exception e){
// errorText.setVisibility(View.VISIBLE);
Log.i("inside","do in back");
// relativeLayout.setVisibility( View.GONE);
e.printStackTrace();
}
return null;
}
}
}
Logcat
2020-01-12 20:54:51.814 17593-17663/? D/url output: {"coord":{"lon":77.18,"lat":28.73},"weather":[{"id":711,"main":"Smoke","description":"smoke","icon":"50n"}],"base":"stations","main":{"temp":289.2,"feels_like":288.97,"temp_min":288.15,"temp_max":289.82,"pressure":1014,"humidity":67},"visibility":2200,"wind":{"speed":0.37,"deg":12},"clouds":{"all":20},"dt":1578842444,"sys":{"type":1,"id":9165,"country":"IN","sunrise":1578793526,"sunset":1578831159},"timezone":19800,"id":1269125,"name":"Jawaharnagar","cod":200}
2020-01-12 20:54:51.814 17593-17663/? I/inside: do in back
2020-01-12 20:54:51.814 17593-17663/? W/System.err: java.util.NoSuchElementException
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at java.util.Scanner.throwFor(Scanner.java:874)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at java.util.Scanner.next(Scanner.java:1388)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at com.tarandeepsingh.test.MainActivity$weatherTask.doInBackground(MainActivity.java:91)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at com.tarandeepsingh.test.MainActivity$weatherTask.doInBackground(MainActivity.java:24)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at java.lang.Thread.run(Thread.java:764)
thanks
Solution
NoSuchElementException
means there are no more tokens available. Try below
boolean has_input = scanner.hasNext();
if(has_input) {
String result = scanner.next();
Log.d("url output", result);
return result;
}
Answered By - Md. Asaduzzaman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.