Reputation: 1
I am building a weather app using api from openweathermap.org and when i run the program it shows an error.How to fix that?
I have created an account on openweathermap.org to create api key and used url="https://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=2ee7238c0e02a5b1efef8418a075c72c"
public class MainActivity extends AppCompatActivity {
public class DownloadTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... urls) {
URL url;
HttpURLConnection conn=null;
try {
String result="";
url=new URL(urls[0]);
conn=(HttpURLConnection) url.openConnection();
conn.connect();
InputStream is=conn.getInputStream();
InputStreamReader reader=new InputStreamReader(is);
int data=reader.read();
while(data!=-1){
char current=(char)data;
result=current+result;
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 json=new JSONObject(result);
String weatherInfo=json.getString("weather");
Log.i("Contents ",weatherInfo);
JSONArray arr=new JSONArray(weatherInfo);
for(int i=0;i<arr.length();i++){
JSONObject jsonPart=arr.getJSONObject(i);
Log.i("main",jsonPart.getString("main"));
Log.i("decription",jsonPart.getString("description"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DownloadTask task=new DownloadTask();
try {
task.execute("https://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=2ee7238c0e02a5b1efef8418a075c72c").get();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output is:
org.json.JSONException: Expected literal value at character 0 of }002:"doc","nodnoL":"eman",3473462:"di",0063:"enozemit",}2114941651:"tesnus",8424341651:"esirnus","BG":"yrtnuoc",1900.0:"egassem",4141:"di",1:"epyt"{:"sys",1880741651:"td",}09:"lla"{:"sduolc",}033:"ged",1.3:"deeps"{:"dniw",00001:"ytilibisiv",}62.992:"xam_pmet",51.092:"nim_pmet",37:"ytidimuh",8101:"erusserp",24.592:"pmet"{:"niam","snoitats":"esab",]}"d01":"noci","niar thgil":"noitpircsed","niaR":"niam",005:"di"{[:"rehtaew",}15.15:"tal",31.0-:"nol"{:"drooc"{
Upvotes: 0
Views: 2210
Reputation: 544
The object you are trying to parse/read does not start with the character "{". And a JSON Object/file always starts with a "{"
result=current+result; // this line should be replaced
result += current; //-----by this line
Upvotes: 1
Reputation: 719299
Here is your problem:
InputStream is=conn.getInputStream();
InputStreamReader reader=new InputStreamReader(is);
int data=reader.read();
while(data!=-1){
char current=(char)data;
result=current+result; // <<==== HERE
data=reader.read();
}
Your code is storing the JSON in result
in the reverse order!
Naturally, the reversed JSON is not parseable.
To fix this, change:
result = current + result;
to
result = result + current;
Or better still, use one of the "bulk read" options described in https://www.baeldung.com/java-convert-reader-to-string
(At least, wrap the Reader
in a BufferedReader
. Reading one character or byte at a time from an unbuffered input pipeline is very inefficient.)
Upvotes: 2