Issue
class GetData extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpURLConnection urlConnection = null;
String result = "";
try {
URL url = new URL("http://192.168.0.100/index.php?kod=1eba7936&mode=1");
urlConnection = (HttpURLConnection) url.openConnection();
int code = urlConnection.getResponseCode();
if(code==200){
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
if (in != null) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
}
in.close();
}
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
urlConnection.disconnect();
}
return result;
}
@Override
protected void onPostExecute(String result) {
System.out.println(result);
super.onPostExecute(result);
}
}
I got such class, its work great exept i cannot catch variable as it returns void. The result its single integer value. And System.out.println(result) display proper value on console.
I call function from MainActivity with
new GetData().execute();
My goal is to have result from GetData as integer variable in MainActivity.
Solution
I made new class with public string
public class Data {
public static String data;
}
In onPostExecute i add:
Data.data = result.toString();
And now i can use Data.data as string whetever i want.
Answered By - Sirwiz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.