Please someone help me in this,
I have a php
script which returns Json
response as "success" if condition is satisfied.
I have put a log statement and I am getting response as "success" but when I put reesponse in if statement it does not recognize it.
plus if I get the response "success" i want to change the button color from red to green in if statement which is giving me error and my application crashes.
so my problem is:
- I get the response "success" but how to put it in if statement? because it directly execute else statement.
- The button change statement makes my application crash if i put the button change statement in else part for testing.
Here is my code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Session class instance
session = new SessionManager(getApplicationContext());
Toast.makeText(getApplicationContext(), "User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG).show();
/**
* Call this function whenever you want to check user login
* This will redirect user to LoginActivity is he is not
* logged in
* */
session.checkLogin();
setContentView(R.layout.movie_detail);
bt=(Button)findViewById(R.id.buttonchange);
titlee=(TextView)findViewById(R.id.mtitle);
date=(TextView)findViewById(R.id.mdate);
like=(TextView)findViewById(R.id.mlikes);
video=(TextView)findViewById(R.id.mvideo);
idd=(TextView)findViewById(R.id.mid);
image=(ImageView)findViewById(R.id.imageView);
UniqueId=(TextView)findViewById(R.id.unique_id);
ts=(TextSwitcher)findViewById(R.id.tsLikesCounter);
Intent i=getIntent();
//image.setImageResource(i.getStringExtra("THUMB"));
titlee.setText(i.getStringExtra("TITLE"));
like.setText("Likes = "+i.getStringExtra("LIKES"));
date.setText("Date = " + i.getStringExtra("DATE"));
video.setText("Video = " + i.getStringExtra("VIDEO"));
idd.setText(i.getStringExtra("IDD"));
UniqueId.setText(i.getStringExtra("UNIQUEID"));
String id = idd.getText().toString();
String unique = UniqueId.getText().toString();
// private void insertToDatabase(String id,String unique){
class SendPostReqAsyncTaskk extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String paramUsername = params[0];
String paramAddress = params[1];
String id = idd.getText().toString();
String unique = UniqueId.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", id));
nameValuePairs.add(new BasicNameValuePair("unique", unique));
// getting JSON Object
// Note that create product url accepts POST method
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("MY_URL");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
String json = EntityUtils.toString(response.getEntity());
if(json=="success"){
Log.d("IN IF::", json);
bt.setBackgroundResource(R.drawable.green1);
}
else {
Log.d("IN ELSE:::", json);
}
// writing response to log
Log.d("Http Response:::", json);
// Toast.makeText(getBaseContext(), response.toString(), Toast.LENGTH_SHORT).show();
HttpEntity entity = response.getEntity();
//JSONObject myObject = new JSONObject(TAG_SUCCESS);
// int success =response.getInt(TAG_SUCCESS);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return "success";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
SendPostReqAsyncTaskk check = new SendPostReqAsyncTaskk();
check.execute(id, unique);
}
here is the error to button change statement
10-20 10:34:56.597 2433-2452/? D/IN ELSE:::﹕ "success"
10-20 10:34:56.787 2433-2452/? W/dalvikvm﹕ threadid=14: thread exiting with uncaught exception (group=0x400207d8)
10-20 10:34:56.797 2433-2452/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:200) at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) at java.util.concurrent.FutureTask.setException(FutureTask.java:124) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) at java.util.concurrent.FutureTask.run(FutureTask.java:137) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) at java.lang.Thread.run(Thread.java:1096) Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. at android.view.ViewRoot.checkThread(ViewRoot.java:2812) at android.view.ViewRoot.invalidateChild(ViewRoot.java:607) at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633) at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505) at android.view.View.invalidate(View.java:5139) at android.view.View.setBackgroundDrawable(View.java:7486) at android.view.View.setBackgroundResource(View.java:7395) at com.sne.movielist.MovieDetailActivity$1SendPostReqAsyncTaskk.doInBackground(MovieDetailActivity.java:145) at com.sne.movielist.MovieDetailActivity$1SendPostReqAsyncTaskk.doInBackground(MovieDetailActivity.java:114) at android.os.AsyncTask$2.call(AsyncTask.java:185) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) at java.util.concurrent.FutureTask.run(FutureTask.java:137) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) at java.lang.Thread.run(Thread.java:1096)
10-20 10:34:56.797 175-175/? W/ActivityManager﹕ Force finishing activity com.sne.movielist/.MovieDetailActivity
10-20 10:34:56.887 175-192/? I/ActivityManager﹕ ensureBootCompleted():enableScreenfalse
10-20 10:34:56.897 2433-2433/? I/ActivityThread﹕ queueIdle
10-20 10:34:56.897 2433-2433/? V/ActivityThread﹕ Reporting idle of ActivityRecord{4a4c4b50 token=android.os.BinderProxy@4a4c4618 {com.sne.movielist/com.sne.movielist.MainActivity}} finished=false