I have the following code and it works fine with in an activity. But how can we use it from inside a service?
The service is running in background and whenever user copies any text, it triggers onPrimaryClipChanged(), and an HTTP Get request is made to send this data to the server. Then we process the result and show a toast notification based on the result.
The Service runs globally and need to be in background as a service.
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray place = jsonObj.getJSONArray("tuc");
JSONObject address = place.getJSONObject(0).getJSONObject("phrase");
meaning = address.getString("text");
} catch (final JSONException e) {
// Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
// Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pd.isShowing())
pd.dismiss();
showToast(result);
}
}
Tried using same code inside a service class,but it does not works. It compiles, but do nothing when run..
please help me, how can i parse json in service class. THANK YOU in advance