I developing android app and now I have problem. Below is a part of my code, and it keeps skipping the "for" part. When I put a breakpoint inside for statement, it stops at the point, and executes the lines very well and makes an output that I want. When I just 'run' app, it skips that part so "String locations" value doesn't change. I googled and some say it's thread-related problem. So I put synchroinzed on the method, still not working. Any other suggestions?
UPDATE
I was trying to show code only related to the problem, but I think now showing the whole would be more useful for those who try to help so here's my entire code on showMapActivity. You can see I've tried some ways around and nothing worked. Saving path's information into String url is where I'm having problem. I tested, and other parts seem to work fine. I know my code is really massy, that was why I only posted parts of the code. TMap related classes are imported from .jar file.
public class showMapActivity extends Activity {
TMapData tmapdata=new TMapData();
TMapView tmapView;
TMapPoint origin, dest;
volatile ArrayList<TMapPoint> points=new ArrayList<>();
private TextView x;
private TextView y;
private HashMap<String,LatLng> coordinates;
private HashMap<LatLng,Double> finalpoint;
static private ConcurrentHashMap<Double,Double> path;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_map);
coordinates=new HashMap<>();
Intent intent=getIntent();
tmapView=new TMapView(this);
path=new ConcurrentHashMap<>();
coordinates=(HashMap<String,LatLng>)intent.getSerializableExtra("coordinate");
path=getPathPoints(coordinates);
int i=0;
String url=getUrl();
//String url = "https://maps.googleapis.com/maps/api/elevation/json?locations=";
//String locations="";
/*
Iterator<Double> keys= path.keySet().iterator();
while(keys.hasNext()){
Double key=keys.next();
//String lat=String.valueOf(key);
//String lng=String.valueOf(path.get(key));
locations=locations+String.valueOf(key)+","+String.valueOf(path.get(key));
if(keys.hasNext())
locations=locations+"|";
}path.entrySet()
*/
/*
for(ConcurrentHashMap.Entry<Double,Double> elem : path.entrySet())
{
String lat=String.valueOf(elem.getKey());
String lng=String.valueOf(elem.getValue());
locations=locations+lat+","+lng;
i++;
if(i!=path.size())
{
locations=locations+"|";
}
}
*/
//url=url+locations+"&key=AIzaSyDD88VFMPIfC5sr0XsFL0PDCE-QRN8gQto";
//String url=getUrl(path);
FetchUrl fetchUrl=new FetchUrl();
fetchUrl.execute(url);
}
private ConcurrentHashMap<Double,Double> getPathPoints(HashMap<String,LatLng> coordinates)
{
final ConcurrentHashMap<Double,Double> Path=new ConcurrentHashMap<>();
tmapView.setSKPMapApiKey("6bb5b7f3-1274-3c5e-ba93-790aee876673");
origin=new TMapPoint(coordinates.get("origin").latitude,coordinates.get("origin").longitude);
dest=new TMapPoint(coordinates.get("dest").latitude,coordinates.get("dest").longitude);
tmapdata.findPathData(origin, dest, new TMapData.FindPathDataListenerCallback() {
#Override
public void onFindPathData(TMapPolyLine polyLine) {
points=polyLine.getLinePoint();
for(TMapPoint point : points )
Path.put(point.getLatitude(),point.getLongitude());
}
});
return Path;
}
//ConcurrentHashMap<Double,Double> path
private synchronized String getUrl() {
int i=0;
String url = "https://maps.googleapis.com/maps/api/elevation/json?locations=";
String locations="";
for(HashMap.Entry<Double,Double> elem : path.entrySet())
{
String lat=String.valueOf(elem.getKey());
String lng=String.valueOf(elem.getValue());
locations=locations+lat+","+lng;
i++;
if(i!=path.size())
{
locations=locations+"|";
}
}
url=url+locations+"&key=AIzaSyDD88VFMPIfC5sr0XsFL0PDCE-QRN8gQto";
//https://maps.googleapis.com/maps/api/elevation/json?locations=
// 39.7391536,-104.9847034|36.455556,-116.866667&key=AIzaSyDD88VFMPIfC5sr0XsFL0PDCE-QRN8gQto
// Output format
return url;
}
private class FetchUrl extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... url) {
// For storing data from web service
String data = "";
try {
// Fetching the data from web service
//downloadURL
data = downloadUrl(url[0]);
Log.d("Background Task data", data.toString());
} catch (Exception e) {
Log.d("Background Task", e.toString());
}
return data;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//ParserTask
ParserTask parserTask = new ParserTask();
// Invokes the thread for parsing the JSON data
parserTask.execute(result);
}
}
private String downloadUrl(String strUrl) throws IOException {
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
//읽은 데이터를 버퍼에 저장
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
data = sb.toString();
Log.d("downloadUrl", data.toString());
br.close();
} catch (Exception e) {
Log.d("Exception", e.toString());
} finally {
iStream.close();
urlConnection.disconnect();
}
return data;
}
private class ParserTask extends AsyncTask<String, Integer, ArrayList<Double>> {
// Parsing the data in non-ui thread
#Override
protected ArrayList<Double> doInBackground(String... jsonData) {
JSONObject jObject;
ArrayList<Double> altitude = null;
try {
jObject = new JSONObject(jsonData[0]);
Log.d("ParserTask",jsonData[0].toString());
//DataParser class 호출
DataParser parser = new DataParser();
Log.d("ParserTask", parser.toString());
// Starts parsing data
altitude = parser.parse(jObject);
Log.d("ParserTask","Getting Altitudes");
Log.d("ParserTask",altitude.toString());
} catch (Exception e) {
Log.d("ParserTask",e.toString());
e.printStackTrace();
}
return altitude;
}
// Executes in UI thread, after the parsing process
#Override
protected void onPostExecute(ArrayList<Double> result) {
finalpoint=new HashMap<>();
LatLng latLng;
int i=0;
for(HashMap.Entry<Double,Double> elem : path.entrySet() )
{
latLng=new LatLng(elem.getKey(),elem.getValue());
finalpoint.put(latLng,result.get(i++));
}
x = (TextView) findViewById(R.id.textView5);
y = (TextView) findViewById(R.id.textView6);
x.setText(String.valueOf(finalpoint.get(coordinates.get("origin"))));
y.setText(String.valueOf(finalpoint.get(coordinates.get("dest"))));
}
}
}
(Apologies for posting this as an answer - I don't yet have the required reputation to comment)
Simply adding synchronized to a method doesn't necessarily guarantee thread safety.
How and when is path being populated?
Update after additional information provided
The problem seems to be that the path points are being generated asynchronously, and you are trying to use them before the generation process has finished (or perhaps even started). This happens because the findPathData simply starts the generation process and returns immediately (i.e. before the generation process has finished). In your code, you then go on and build the URL which is supposed to contain the point data immediately. At this point the background point generation process may not have finished, and may not have even started. As a result the point map may be empty or incomplete, and your URL will not be generated as you expect.
You need to find a way to wait until all of the path point data has been returned by the asynchronous processing before creating the URL. This looks like it could be very difficult, if not impossible, with the version of the findPathData method you are using, because it returns points via the callback one at a time and you may not know how many will be generated.
I had a quick look at the API for TMapData and it has a findPathDataAll method which seems to generate all the points and return them in a single callback call rather than one by one. If this is indeed what it does (sorry, I can't read Korean), you could use this method and then generate the URL from the callback, because when it's called you know that the generation process has been completed. If you do this, be careful to make sure that you're on the main thread before interacting with the UI or Activity.
Hope that helps.
Related
I was trying to write an app which gets some JSON from a website, converts it into an array and then puts specific elements of this array into different spots of a table in the app. I wrote the code which gets me the JSON data and converts it into an ArrayList in Eclipse. It worked great there. I now tried to implement the code in Android Studio but I just can't get it to work. I have the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_layout);
TextView textview = (TextView) findViewById(R.id.textView8);
try{
textview.setText(MainActivity.get_data().toString());
}
catch (Exception e){
textview.setText(e.getClass().getCanonicalName());
}
}
public static ArrayList<String> get_data() throws Exception{
String[] ids = new String[] {(here are some ids)};
ArrayList<String> alldata = new ArrayList<>();
for(int r=0;r<ids.length;r++) {
String url = "https://ktrax.kisstech.ch/backend/tracking?db=aviation&sw_lat=40.97765930663377&sw_lon=-26.280096606906117&ne_lat=43.01854550507729&ne_lon=-23.407171802218617&ktrax_id=icao%3a" +ids[r]+"&format=json";
URL ktraxURL = new URL(url);
JSONArray test = Networkaccess.getJSONarr(ktraxURL);
ArrayList<String> listdata = MainActivity.converter(test);
ArrayList<String> elementlist = new ArrayList<>();
(some more code (irrelevant in this matter) which makes the array nice and neat)
//System.out.println(alldata);
return alldata;
}
public static ArrayList<String> converter(JSONArray test){ //creates an array with all the data splited
(Some code that converts JSON to ArrayList (also no problems here))
}
}
class Networkaccess{
public static JSONArray getJSONarr(URL ktraxURL) throws Exception{
HttpURLConnection con = (HttpURLConnection) ktraxURL.openConnection();
//Checking for reponse code of GET Method
con.setRequestMethod("GET");
!!!!!! int responseCode = con.getResponseCode(); //THE DEBUGGER JUMPS FROM HERE TO THE CATCH IN MAIN
System.out.println("Response Code : " +responseCode);
//Reading Data and
BufferedReader readin = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = readin.readLine()) != null) {
response.append(inputLine);
}
readin.close();
//System.out.println(response.toString());
String jsonresp = new String(response.toString());
//System.out.println(jsonresp);
JSONObject myResponse = new JSONObject(jsonresp);
JSONArray test = myResponse.getJSONArray("targets");
//System.out.println(test);
return test;
}
}
I created the new network class as the error, which was displayed in the textview field was: android.os.NetworkOnMainThreadException. However, this didn't seem to do the job and the same error is still shown. I have no idea on how to continue from here. When debugging, the debugger always runs until the line which I marked with !!! in the code. Then it jumps to the catch exception in the main activity.
Looking forward to your answers!
Networking stuff tends to brick up, since responses can take a while to travel. What it's saying is that you need to move your code to another thread so that your program doesn't freeze up while it's waiting for a response.
This won't prevent networking-related slowdowns, but it will get rid of the error:
Thread networking = new Thread(){
void run() {
int responseCode = con.getResponseCode();
}
}
networking.start();
networking.join();
I have been trying to get the walking duration and distance between two locations using Distance Matrix API. I want to store this results (Distance and Duration) in variables that I will be able to access from other classes/activities.
I managed to get time and duration using AsyncTask, but when I try to store them in a variable in PostExecute method, they are always unassigned when accessing from a method or class outside of the PostExecute method. I now, understand how AsyncTask works and that is asynchronous. I tried to implement interfaces and then try to access the data but still I was not able to.
GeoTask is an inner class in my Map class.
public class GeoTask extends AsyncTask<String, Void, String> {
ProgressDialog pd;
Context mContext;
SelectTime selectTime;
//constructor is used to get the context.
public GeoTask(Context mContext) {
this.mContext = mContext;
}
public GeoTask() {
}
//This function is executed before before "doInBackground(String...params)" is executed to dispaly the progress dialog
#Override
protected void onPreExecute() {
super.onPreExecute();
selectTime=new SelectTime();
pd=new ProgressDialog(mContext);
pd.setMessage("Loading");
pd.setCancelable(false);
pd.show();
}
//This function is executed after the execution of "doInBackground(String...params)" to dismiss the dispalyed progress dialog and call "setDouble(Double)" defined in "MainActivity.java"
#Override
protected void onPostExecute(String aDouble) {
super.onPostExecute(aDouble);
if(aDouble!=null)
{
setDouble(aDouble);
}
else
Toast.makeText(mContext, "Error4!Please Try Again with proper values", Toast.LENGTH_SHORT).show();
}
#Override
protected String doInBackground(String... params) {
try {
URL url=new URL(params[0]);
HttpURLConnection con= (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.connect();
int statuscode=con.getResponseCode();
if(statuscode==HttpURLConnection.HTTP_OK)
{
BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder sb=new StringBuilder();
String line=br.readLine();
while(line!=null)
{
sb.append(line);
line=br.readLine();
}
String json=sb.toString();
Log.d("JSON",json);
JSONObject root=new JSONObject(json);
JSONArray array_rows=root.getJSONArray("rows");
Log.d("JSON","array_rows:"+array_rows);
JSONObject object_rows=array_rows.getJSONObject(0);
Log.d("JSON","object_rows:"+object_rows);
JSONArray array_elements=object_rows.getJSONArray("elements");
Log.d("JSON","array_elements:"+array_elements);
JSONObject object_elements=array_elements.getJSONObject(0);
Log.d("JSON","object_elements:"+object_elements);
JSONObject object_duration=object_elements.getJSONObject("duration");
JSONObject object_distance=object_elements.getJSONObject("distance");
Log.d("JSON","object_duration:"+object_duration);
return object_duration.getString("value")+","+object_distance.getString("value");
}
} catch (MalformedURLException e) {
Log.d("error", "error1");
} catch (IOException e) {
Log.d("error", "error2");
} catch (JSONException e) {
Log.d("error","error3");
}
return null;
}
}
In Map class, the setDouble method:
public void setDouble(String result) {
String res[] = result.split(",");
Double m = Double.parseDouble(res[0]) / 60;
Double d = Double.parseDouble(res[1])/1000;
minutes=m
distance=d
}
The minutes and distance variables initialisation
private double minutes;
public double getMinutes() {
return minutes;
}
private double distance;
public double getDistance() {
return distance;
}
and the execution:
LatLng destinationLatLng = getLatLngFromAddress(destinationPassed);
currentOrigin = getAddressFromLatLng(currentLat,currentLong);
String url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + currentOrigin + "&destinations=" + destinationPassed + "&mode=walking&language=fr-FR&avoid=tolls&key=API_KEY";
Log.d("url string",url);
geoTask.execute(url);
Furthermore, I try to access the minutes and distance variables using the get methods in another class and I get the value 0.0 returned all the time, even after the PostExecution is completed (I tested that it completed using Logs)
1)How can I achieve what I want? Access the distance and duration returned by the API in different classes? It is essential for the app that I am doing to achieve that.
2)If I cant achieve what I want using AsyncTask and this method, is there any way to use Distance Matrix API without using AsyncTask?
Probably you should replace in your
String url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + currentOrigin + "&destinations=" + destinationPassed + "&mode=walking&language=fr-FR&avoid=tolls&key=API_KEY";
line API_KEY characters by your valid Distance API key.
Take a look at this guide to get information how to obtain valid API key.
I am developing an Android Application (Android Studio - Java) which includes a sign in and registration process. I took care of the sign in and registration processes by implementing a connection between PHP files and a MySQL database through http. In short, I just created an AsyncTask class in java --called from another class-- and used it to post data to a PHP file, from there I just used the appropriate SQL commands. This part works fine.
This first part with the login and registration is important because every user will see a slightly different layout once they login. The layout is a RecyclerView composed of detailed CardView elements. Each CardView has a few TextViews with some details. The details are held by an Object i created in a separate class. To fill in the CardView elements a fetched some JSON data using a separate PHP file (and one more http connection). Parsing the data from JSON into Strings and ints was a straightforward endeavor, as was adding them to the list of custom objects. There is some code below showing how I fetched the data and added it to the Object list.
This is the complete AsyncTask class:
public class ScheduleWorker extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
ScheduleWorker (Context ctx) { context = ctx; }
#Override
protected void onPreExecute() { super.onPreExecute(); }
#Override
protected void onPostExecute (String s) {
super.onPostExecute(s);
Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
try {
loadJSON(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
protected String doInBackground(String... params) {
final String fetch_url = "http://192.168.1.70/newfetcher.php";
try {
String ussr_name = params[0];
URL url = new URL(fetch_url);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
OutputStream outputStream = con.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name", "UTF-8")+"="+URLEncoder.encode(ussr_name, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
private void loadJSON(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
Course course;
List<Course> courses = new ArrayList<Course>();
int len = jsonArray.length();
String[] titles = new String[len];
String[] types = new String[len];
String[] teachers = new String[len];
int[] pics = new int[len];
for (int i = 0; i < len; i++) {
JSONObject obj = jsonArray.getJSONObject(i);
titles[i] = obj.getString("title");
types[i] = obj.getString("type");
teachers[i] = obj.getString("teacher");
pics[i] = obj.getInt("pic");
course = new Course(titles[i], types[i], teachers[i], pics[i]);
courses.add(course);
}
}
}
Now, where I encountered problems is when I tried saving the List of Objects (and their details) to a Room Persistent Database. I based my code for the Room Database off an example from Google Developer CodeLabs. I adapted the code for my particular needs but I kept the underlying class structure. The structure includes: an Entity, a DAO, a RoomDatabase, a Repository, a ViewModel, a ViewHolder, an Adapter for the RecyclerView, and a class to populate the database. Everything seems fine except for the part where I populate the database. The example populates the database by using a callback and an AsyncTask within the RoomDatabase class.
Here is populating AsyncTask:
private static RoomDatabase.Callback sRoomDatabaseCallback = new RoomDatabase.Callback() {
#Override
public void onOpen(#NonNull SupportSQLiteDatabase db) {
super.onOpen(db);
// If you want to keep the data through app restarts,
// comment out the following line.
new PopulateDbAsync(INSTANCE).execute();
}
};
/**
* Populate the database in the background.
* If you want to start with more words, just add them.
*/
private static class PopulateDbAsync extends AsyncTask<Void, Void, Void> {
private final WordDao mDao;
PopulateDbAsync(WordRoomDatabase db) {
mDao = db.wordDao();
}
#Override
protected Void doInBackground(final Void... params) {
// Start the app with a clean database every time.
// Not needed if you only populate on creation.
Word word = new Word("Hello");
mDao.insert(word);
word = new Word("World");
mDao.insert(word);
return null;
}
}
My question is how do I populate the database with the detail arrays and/or the Object list? The example executes the callback every time a adding activity is called. I just want to populate the database when the user registers.
I'm trying to build a very basic weather app in android studio. I am using AsyncClass to return multiple strings.
As you can see in the code, I used a class named "Wrapper" that is used to store my strings so I can just return a class object and use it in the onPostExecute method of the AsyncTask. The problem I am facing is that when I test the app, all of the returned Strings somehow are undefined (the default for the Wrapper class). This means the strings are not being updated in the doInBackground method and I can't seem to figure out why!
My Activity
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(MainActivity.class.getSimpleName(), "Can't connect to Google Play Services!");
}
private class Wrapper
{
String Temperature = "UNDEFINED";
String city = "UNDEFINED";
String country = "UNDEFINED";
}
private class GetWeatherTask extends AsyncTask<String, Void, Wrapper> {
private TextView textView;
public GetWeatherTask(TextView textView) {
this.textView = textView;
}
#Override
protected Wrapper doInBackground(String... strings) {
Wrapper w = new Wrapper();
String Temperature = "x";
String city = "y";
String country = "z";
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
String inputString;
while ((inputString = bufferedReader.readLine()) != null) {
builder.append(inputString);
}
JSONObject topLevel = new JSONObject(builder.toString());
JSONObject main = topLevel.getJSONObject("main");
JSONObject cityobj = topLevel.getJSONObject("city");
Temperature = String.valueOf(main.getDouble("temp"));
city = cityobj.getString("name");
country = cityobj.getString("country");
w.Temperature= Temperature;
w.city= city;
w.country=country;
urlConnection.disconnect();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return w;
}
#Override
protected void onPostExecute(Wrapper w) {
textView.setText("Current Temperature: " + w.Temperature + " C" + (char) 0x00B0
+"\n" + "Current Location: "+ w.country +"\n" + "City: "+ w.city );
}
}
}
UPDATE:
turned out that that I was using the wrong url in my code,I was using :
http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&units=%s&appid=%s
Instead I should've been using:
http://api.openweathermap.org/data/2.5/forecast?lat=%f&lon=%f&units=%s&appid=%s
-aka instead of weather I should've been using forcast
Your error starts here
JSONObject main = topLevel.getJSONObject("main");
Probably because the topLevel object has no "main" key.
{
"city":{ },
"cod":"200",
"message":0.1859,
"cnt":40,
"list":[ ]
}
Throw your JSON into here. https://jsonformatter.curiousconcept.com/
You'll notice that there are many, many "main" keys that are within the "list" element, but you have to parse those starting from getJSONArray("list").
Basically, something like this
String city = "undefined";
String country = "undefined";
List<Double> temperatures = new ArrayList<Double>();
try {
JSONObject object = new JSONObject(builder.toString());
JSONObject jCity = object.getJSONObject("city");
city = jCity.getString("name");
country = jCity.getString("country");
JSONArray weatherList = object.getJSONArray("list");
for (int i = 0; i < weatherList.length(); i++) {
JSONObject listObject = weatherList.getJSONObject(i);
double temp = listObject.getJSONObject("main").getDouble("temp");
temperatures.add(temp);
}
} catch (JSONException e) {
e.printStackTrace();
}
return new Wrapper(city, country, temperatures);
After studying your code, either your try block is failing, which is returning your object, but empty, or there is something wrong with your JSON parsing. If you could show us the JSON you are trying to parse that would be a great help.
That being said, the fact that it is still showing as "UNDEFINED" is because that is how you initialised it, and becuase (the JSON parse is likely failing), the object is being returned in an un-edited state.
EDIT:
You are parsing the JSON wrong. You are trying to find an object called "main" in the top directory, however the main object only exists inside of an array called list!
Please look here for a more easy to see and visual representation: http://prntscr.com/dlhlrk
You can use this site to help visualise your JSON and create an appropriate soluton based upon it. https://jsonformatter.curiousconcept.com/
Looking at the API you posted earlier (api.openweathermap.org) you are trying to access variables that don't exist. I suggest you have a look at what the API returns and try getting the variables one by one if you are getting a JSONException
EDIT:
What API you are using? In your initial post you said it was http://api.openweathermap.org/data/2.5/weather but in a comment above you said it was http://api.openweathermap.org/data/2.5/forecast.
If you're using the weather API (as initially stated) you can use the below:
#Override
protected Wrapper doInBackground(String... strings) {
Wrapper w = new Wrapper();
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
String inputString;
while ((inputString = bufferedReader.readLine()) != null) {
builder.append(inputString);
}
Log.d("JSON", builder.toString());
JSONObject topLevel = new JSONObject(builder.toString());
JSONObject main = topLevel.getJSONObject("main");
JSONObject sys = topLevel.getJSONObject("sys");
w.Temperature = String.valueOf(main.getDouble("temp"));
w.city = topLevel.getString("name");
w.country = sys.getString("country");
urlConnection.disconnect();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return w;
}
I have a link where I'm providing 2 parameters and a php server side script is executing a query and writing them to my databae.
The problem is that in this specific case, it seems that I can't connect to teh url.
Here is my button xml file:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:includeFontPadding="#+id/btnSubmit"
android:text="#string/btnSubmit"
android:onClick="submitNewJ"
/>
Here is my click "listener":
public void submitNewJ(View view){
new submitJ().execute();
}
And here is the submitJ code:
public class submitJ extends AsyncTask<Void, Integer, Void>{
#Override
protected Void doInBackground(Void... params) {
try{
String encodedName = URLEncoder.encode(Name,"UTF-8");
String encodedBody = URLEncoder.encode(Body,"UTF-8");
URL url = new URL("http://site123.com/android/J/sJ.php?Name="+encodedName+"&Body="+encodedBody);
URLConnection urlConnection = url.openConnection();
#SuppressWarnings("unused")
InputStream in = urlConnection.getInputStream();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
And here is how I'm getting the strings:
EditText jN;
EditText jB;
String Name = "";
String Body = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_joke);
jN = (EditText)findViewById(R.id.newName);
jB = (EditText)findViewById(R.id.newBody);
Name = jN.getText().toString();
Body = jB.getText().toString();
}
It seems that the connection is not working here(even when I'm using the same code in otehr activities). Where am I mistaking?
I know that I'm missing something super small, but I'm not able to spot it.
P.s. My service and link are 100% tested and working.
The site you have in the example is probabbly for reference reasons, but mind that if it is using https protocol and you're reffering to a http, you will not get redirected to the correct link.
Everything in your code seems fine to me.
Just make sure that you're using the correct protocol.
You need to move these lines from onCreate() method to doInBackground() method,
Name = jN.getText().toString();
Body = jB.getText().toString();
put them inside the doInBackground() method like below,
public class submitJ extends AsyncTask<Void, Integer, Void>{
#Override
protected Void doInBackground(Void... params) {
try{
String Name = jN.getText().toString(); // move here and delete from the class
String Body = jB.getText().toString();
String encodedName = URLEncoder.encode(Name,"UTF-8");
String encodedBody = URLEncoder.encode(Body,"UTF-8");
URL url = new URL("http://site123.com/android/J/sJ.php?Name="+encodedName+"&Body="+encodedBody);
URLConnection urlConnection = url.openConnection();
#SuppressWarnings("unused")
InputStream in = urlConnection.getInputStream();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}