i am totally new to android ..and i was trying a tutorial i found online.basic aim is to retrieve data from the database and display it. I am not able to execute it .I have attached the code and error log.It would be a great help if anyone can rectify the error.
<package com.example.androidhive;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class AllProductsActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static String url_all_products = "192.168.0.1/android_connect/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
// products JSONArray
JSONArray products = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_products);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = getListView();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
EditProductActivity.class);
// sending pid to next activity
in.putExtra(TAG_PID, pid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
// Response from Edit Product Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllProductsActivity.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllProductsActivity.this, productsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}`
Error
07-18 02:55:17.809: D/dalvikvm(2205): GC_FOR_ALLOC freed 56K, 5% free 2815K/2944K, paused 106ms, total 107ms
07-18 02:55:17.839: I/dalvikvm-heap(2205): Grow heap (frag case) to 5.628MB for 2949608-byte allocation
07-18 02:55:17.889: D/dalvikvm(2205): GC_FOR_ALLOC freed <1K, 3% free 5695K/5828K, paused 48ms, total 48ms
07-18 02:55:18.349: D/gralloc_goldfish(2205): Emulator without GPU emulation detected.
07-18 02:55:21.079: D/dalvikvm(2205): GC_FOR_ALLOC freed 19K, 2% free 6529K/6620K, paused 86ms, total 87ms
07-18 02:55:22.309: W/dalvikvm(2205): threadid=12: thread exiting with uncaught exception (group=0xb3a4fb90)
07-18 02:55:22.349: E/AndroidRuntime(2205): FATAL EXCEPTION: AsyncTask #1
07-18 02:55:22.349: E/AndroidRuntime(2205): Process: com.example.androidhive, PID: 2205
07-18 02:55:22.349: E/AndroidRuntime(2205): java.lang.RuntimeException: An error occured while executing doInBackground()
07-18 02:55:22.349: E/AndroidRuntime(2205): at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-18 02:55:22.349: E/AndroidRuntime(2205): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-18 02:55:22.349: E/AndroidRuntime(2205): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-18 02:55:22.349: E/AndroidRuntime(2205): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-18 02:55:22.349: E/AndroidRuntime(2205): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-18 02:55:22.349: E/AndroidRuntime(2205): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-18 02:55:22.349: E/AndroidRuntime(2205): at java.lang.Thread.run(Thread.java:841)
07-18 02:55:22.349: E/AndroidRuntime(2205): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=192.168.0.1/android_connect/get_all_products.php
07-18 02:55:22.349: E/AndroidRuntime(2205): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
07-18 02:55:22.349: E/AndroidRuntime(2205): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
07-18 02:55:22.349: E/AndroidRuntime(2205): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-18 02:55:22.349: E/AndroidRuntime(2205): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-18 02:55:22.349: E/AndroidRuntime(2205): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-18 02:55:22.349: E/AndroidRuntime(2205): at com.example.androidhive.JSONParser.makeHttpRequest(JSONParser.java:62)
07-18 02:55:22.349: E/AndroidRuntime(2205): at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:127)
07-18 02:55:22.349: E/AndroidRuntime(2205): at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:1)
07-18 02:55:22.349: E/AndroidRuntime(2205): at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-18 02:55:22.349: E/AndroidRuntime(2205): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-18 02:55:22.349: E/AndroidRuntime(2205): ... 3 more
07-18 02:55:22.819: I/Choreographer(2205): Skipped 362 frames! The application may be doing too much work on its main thread.
07-18 02:55:25.829: I/Choreographer(2205): Skipped 171 frames! The application may be doing too much work on its main thread.
07-18 02:55:30.059: E/WindowManager(2205): android.view.WindowLeaked: Activity com.example.androidhive.AllProductsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b40ada18 V.E..... R.....ID 0,0-461,175} that was originally added here
07-18 02:55:30.059: E/WindowManager(2205): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:346)
07-18 02:55:30.059: E/WindowManager(2205): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
07-18 02:55:30.059: E/WindowManager(2205): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
07-18 02:55:30.059: E/WindowManager(2205): at android.app.Dialog.show(Dialog.java:286)
07-18 02:55:30.059: E/WindowManager(2205): at com.example.androidhive.AllProductsActivity$LoadAllProducts.onPreExecute(AllProductsActivity.java:117)
07-18 02:55:30.059: E/WindowManager(2205): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
07-18 02:55:30.059: E/WindowManager(2205): at android.os.AsyncTask.execute(AsyncTask.java:535)
07-18 02:55:30.059: E/WindowManager(2205): at com.example.androidhive.AllProductsActivity.onCreate(AllProductsActivity.java:57)
07-18 02:55:30.059: E/WindowManager(2205): at android.app.Activity.performCreate(Activity.java:5243)
07-18 02:55:30.059: E/WindowManager(2205): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-18 02:55:30.059: E/WindowManager(2205): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
07-18 02:55:30.059: E/WindowManager(2205): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
07-18 02:55:30.059: E/WindowManager(2205): at android.app.ActivityThread.access$700(ActivityThread.java:135)
07-18 02:55:30.059: E/WindowManager(2205): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
07-18 02:55:30.059: E/WindowManager(2205): at android.os.Handler.dispatchMessage(Handler.java:102)
07-18 02:55:30.059: E/WindowManager(2205): at android.os.Looper.loop(Looper.java:137)
07-18 02:55:30.059: E/WindowManager(2205): at android.app.ActivityThread.main(ActivityThread.java:4998)
07-18 02:55:30.059: E/WindowManager(2205): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 02:55:30.059: E/WindowManager(2205): at java.lang.reflect.Method.invoke(Method.java:515)
07-18 02:55:30.059: E/WindowManager(2205): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
07-18 02:55:30.059: E/WindowManager(2205): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
07-18 02:55:30.059: E/WindowManager(2205): at dalvik.system.NativeStart.main(Native Method)
07-18 02:55:30.059: I/Choreographer(2205): Skipped 217 frames! The application may be doing too much work on its main thread.
07-18 02:55:32.999: I/Process(2205): Sending signal. PID: 2205 SIG: 9
This exception message:
java.lang.IllegalStateException: Target host must not be n\
ull, or set in parameters. scheme=null, host=null, path=192.168.0.1/android_connect/get_all_products.php
tells me that the makeHttpRequest fails to identify the host parameter in the url you pass. So I really would go with #Titus advice and provide a full URL with http:// prefix (or whatever the protocol is). If the type of error changes you may have more then one error.
Related
I'm trying to add some values to my database from my android application through JSON.
I have used the below code previously with eclipse and worked perfectly, now im trying it using android studio and it isn't working I don't know why!
code:
public class Main2Activity extends AppCompatActivity {
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText ID;
EditText fname;
EditText lname;
EditText phone;
Button addbtn;
// url to create new product
private static String url_create_product = "http://www.lamia.byethost18.com/add_info.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
String H,Q,C,Ls;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ID = (EditText) findViewById(R.id.ID);
lname = (EditText) findViewById(R.id.lname);
fname = (EditText) findViewById(R.id.fname);
phone = (EditText) findViewById(R.id.phone);
H = ID.getText().toString();
Q = lname.getText().toString();
C = fname.getText().toString();
Ls = phone.getText().toString();
addbtn = (Button) findViewById(R.id.addbtn);
addbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new CreateNewProduct().execute();
}
});
}
class CreateNewProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Main2Activity.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("ID", H));
params.add(new BasicNameValuePair("lname", Q));
params.add(new BasicNameValuePair("fname", C));
params.add(new BasicNameValuePair("phone", Ls));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
// Intent i = new Intent(getApplicationContext(), AdminExercise.class);
// startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
I got this error:
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/JSON Parser: Error parsing data org.json.JSONException: Value <html><body><script of type java.lang.String cannot be converted to JSONObject
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: Process: com.example.hatim.maps, PID: 1934
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: java.lang.RuntimeException: An error occurred while executing doInBackground()
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: at android.os.AsyncTask$3.done(AsyncTask.java:309)
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818)
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: at com.example.hatim.maps.Main2Activity$CreateNewProduct.doInBackground(Main2Activity.java:113)
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: at com.example.hatim.maps.Main2Activity$CreateNewProduct.doInBackground(Main2Activity.java:77)
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: at android.os.AsyncTask$2.call(AsyncTask.java:295)
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818)
I don't get what is the error?
can someone help please? thank you!
It seems that your request is not receiving a JSON, may be is an HTML error because the value received starts with html format:
E/JSON Parser: Error parsing data org.json.JSONException: Value <html><body><script of type java.lang.String cannot be converted to JSONObject
I hope someone can help me.
I have an android app that has a standard login but also has twitter and facebook login buttons.
When the user try's to login using the facebook button the app crashes and I am unsure as to why? I believe that it is because the email field is returning as null.
I have rebuilt the app numerous times, the app is public on facebook, the app id is correct as is the package name.
logcat is below
09-03 15:15:45.971 16896-17259/com.projects.bmxtrackfinder W/chromium﹕ [WARNING:keycode_converter.cc(91)] empty code string
09-03 15:15:47.470 16896-17259/com.projects.bmxtrackfinder W/chromium﹕ [WARNING:keycode_converter.cc(91)] empty code string
09-03 15:15:48.128 16896-17259/com.projects.bmxtrackfinder W/chromium﹕ [WARNING:keycode_converter.cc(91)] empty code string
09-03 15:15:52.942 16896-16896/com.projects.bmxtrackfinder W/BindingManager﹕ Cannot call determinedVisibility() - never saw a connection for the pid: 16896
09-03 15:16:04.690 16896-16896/com.projects.bmxtrackfinder W/BindingManager﹕ Cannot call determinedVisibility() - never saw a connection for the pid: 16896
09-03 15:16:04.821 16896-16896/com.projects.bmxtrackfinder E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-03 15:16:04.821 16896-16896/com.projects.bmxtrackfinder E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-03 15:16:04.828 16896-16896/com.projects.bmxtrackfinder E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-03 15:16:04.829 16896-16896/com.projects.bmxtrackfinder E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-03 15:16:05.094 16896-16896/com.projects.bmxtrackfinder E/FACEBOOK USERNAME**﹕ John TrackFinder
09-03 15:16:05.095 16896-16896/com.projects.bmxtrackfinder E/FACEBOOK ID**﹕ 135514753461349
09-03 15:16:05.096 16896-16896/com.projects.bmxtrackfinder E/FACEBOOK EMAIL**﹕ null
09-03 15:16:05.124 16896-17668/com.projects.bmxtrackfinder E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #4
Process: com.projects.bmxtrackfinder, PID: 16896
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.fragments.activity.LoginActivity$2.onAsyncTaskDoInBackground(LoginActivity.java:263)
at com.asynctask.MGAsyncTask.doInBackground(MGAsyncTask.java:45)
at com.asynctask.MGAsyncTask.doInBackground(MGAsyncTask.java:10)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
09-03 15:16:05.667 16896-16896/com.projects.bmxtrackfinder E/WindowManager﹕ android.view.WindowLeaked: Activity com.fragments.activity.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{2f585567 V.E..... R.....ID 0,0-729,192} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:298)
at android.app.ProgressDialog.show(ProgressDialog.java:116)
at android.app.ProgressDialog.show(ProgressDialog.java:99)
at com.asynctask.MGAsyncTask.onPreExecute(MGAsyncTask.java:60)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:591)
at android.os.AsyncTask.execute(AsyncTask.java:539)
at com.fragments.activity.LoginActivity.syncFacebookUser(LoginActivity.java:274)
at com.fragments.activity.LoginActivity$4.onCompleted(LoginActivity.java:423)
at com.facebook.Request$1.onCompleted(Request.java:303)
at com.facebook.Request$4.run(Request.java:1726)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
09-03 15:16:12.839 16896-16908/com.projects.bmxtrackfinder W/art﹕ Suspending all threads took: 11.292ms
(LoginActivity.java:263) = String email = user.asMap().get("email").toString;
Asynch code below:
package com.asynctask;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import com.projects.bmxtrackfinder.R;
import com.utilities.MGUtilities;
public class MGAsyncTask extends AsyncTask<Void, Void, String> {
public ProgressDialog dialog;
public Activity activity;
public Object object;
public int tag = 0;
public int listTag = 0;
OnMGAsyncTaskListener mCallback;
public interface OnMGAsyncTaskListener {
public void onAsyncTaskDoInBackground(MGAsyncTask asyncTask);
public void onAsyncTaskProgressUpdate(MGAsyncTask asyncTask);
public void onAsyncTaskPostExecute(MGAsyncTask asyncTask);
public void onAsyncTaskPreExecute(MGAsyncTask asyncTask);
}
public void setMGAsyncTaskListener(OnMGAsyncTaskListener listener) {
try {
mCallback = (OnMGAsyncTaskListener) listener;
} catch (ClassCastException e) {
throw new ClassCastException(this.toString() + "Did not implement OnMGAsyncTaskListener");
}
}
public MGAsyncTask(Activity activity) {
this.activity = activity;
}
public void startAsyncTask() {
this.execute();
}
#Override
protected String doInBackground(Void... params) {
mCallback.onAsyncTaskDoInBackground(this);
return "";
}
#Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation. parse json data
dialog.dismiss();
mCallback.onAsyncTaskPostExecute(this);
}
#Override
protected void onPreExecute() {
// Things to be done before execution of long running operation. For example showing ProgessDialog
String loading = MGUtilities.getStringFromResource(activity, R.string.loading);
dialog = ProgressDialog.show(activity, "", loading, true);
mCallback.onAsyncTaskPreExecute(this);
}
#Override
protected void onProgressUpdate(Void... values) {
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
mCallback.onAsyncTaskProgressUpdate(this);
}
public void setMessage(String message) {
dialog.setMessage(message);
}
public void setTitle(String title) {
dialog.setTitle(title);
}
public void setIcon(int resId) {
dialog.setIcon(resId);
}
}
I am getting the following runtime exception.
Fatal signal 6 (SIGABRT) at 0x00000820 (code=-6), thread 2080
thread eixiting with uncaught exception
java.lang.RuntimeException: An error occured while executing doInBackground()
I am posted the full stacktrace errors below:
Stacktrace:
02-02 02:13:17.137: E/dalvikvm(2080): VM aborting
02-02 02:13:17.137: A/libc(2080): Fatal signal 6 (SIGABRT) at 0x00000820 (code=-6), thread 2080 (e.quranmadeeasy)
02-02 02:13:20.177: D/dalvikvm(2140): GC_FOR_ALLOC freed 101K, 8% free 2981K/3208K, paused 74ms, total 75ms
02-02 02:13:20.177: I/dalvikvm-heap(2140): Grow heap (frag case) to 3.637MB for 635812-byte allocation
02-02 02:13:20.217: D/dalvikvm(2140): GC_FOR_ALLOC freed 0K, 6% free 3602K/3832K, paused 36ms, total 36ms
02-02 02:13:20.397: W/dalvikvm(2140): threadid=13: thread exiting with uncaught exception (group=0xb3a3cba8)
02-02 02:13:20.397: E/AndroidRuntime(2140): FATAL EXCEPTION: AsyncTask #1
02-02 02:13:20.397: E/AndroidRuntime(2140): Process: com.qrme.quranmadeeasy, PID: 2140
02-02 02:13:20.397: E/AndroidRuntime(2140): java.lang.RuntimeException: An error occured while executing doInBackground()
02-02 02:13:20.397: E/AndroidRuntime(2140): at android.os.AsyncTask$3.done(AsyncTask.java:300)
02-02 02:13:20.397: E/AndroidRuntime(2140): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
02-02 02:13:20.397: E/AndroidRuntime(2140): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
02-02 02:13:20.397: E/AndroidRuntime(2140): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
02-02 02:13:20.397: E/AndroidRuntime(2140): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-02 02:13:20.397: E/AndroidRuntime(2140): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-02 02:13:20.397: E/AndroidRuntime(2140): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-02 02:13:20.397: E/AndroidRuntime(2140): at java.lang.Thread.run(Thread.java:841)
02-02 02:13:20.397: E/AndroidRuntime(2140): Caused by: java.lang.NullPointerException
02-02 02:13:20.397: E/AndroidRuntime(2140): at com.qrme.quranmadeeasy.LessonActivity$getLesson.doInBackground(LessonActivity.java:259)
02-02 02:13:20.397: E/AndroidRuntime(2140): at com.qrme.quranmadeeasy.LessonActivity$getLesson.doInBackground(LessonActivity.java:1)
02-02 02:13:20.397: E/AndroidRuntime(2140): at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-02 02:13:20.397: E/AndroidRuntime(2140): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-02 02:13:20.397: E/AndroidRuntime(2140): ... 4 more
I am pointed out the error line in below code.
LessonActivity.java:
public class LessonActivity extends Activity implements OnItemClickListener {
AdapterLesson al; // creating object of AdapterLesson class
static ArrayList<Lesson> lessonList = null;
static ArrayList<Settings> settings = null;
int chapterId;
AdapterPages adapPage;
static ArrayList<Page> selectedpageList = null;
static ArrayList<Page> pageList = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_lesson);
ParseAnalytics.trackAppOpened(getIntent());
if (getIntent() != null && getIntent().getExtras() != null) {
String chapt_id = getIntent().getExtras().getString("CHAPTER_ID");
chapter = getIntent().getExtras().getString("CHAPTER_NAME");
// txtChapterdetails.setText(chapter);
if (chapt_id != null && !chapt_id.equalsIgnoreCase("")) {
chapterId = Integer.parseInt(chapt_id);
}
}
initialize();
listLesson.setOnItemClickListener(this);
}
public class getLesson extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
lessonList = DatabaseQueryHelper.getInstance().getLesson(chapterId); --->259th line
return null;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onPostExecute(String result) {
if(lessonList!=null)
{
if(lessonList.size()>0)
{
al = new AdapterLesson(LessonActivity.this, lessonList);
listLesson.setAdapter(al);
}
}
}
}
}
Anyone can help me with this.Thank you.
The problem is in DatabaseQueryHelper class. The method getInstance() is called before an instance has been initialized. So when you call it you get NullPointerException. Look for ways to implement a Singleton in order to correct this one.
I got this error whenever I change my URL to my online hosting, but if I change to 10.0.2.2 everything seems fine and running.
LogCat
02-01 23:02:18.302 17643-18052/com.example.jithea.testlogin E/JSON Parser﹕ Error parsing data org.json.JSONException: Value <br><table of type java.lang.String cannot be converted to JSONObject
02-01 23:02:18.303 17643-18052/com.example.jithea.testlogin W/dalvikvm﹕ threadid=12: thread exiting with uncaught exception (group=0x40d8d9a8)
02-01 23:02:18.303 17643-18052/com.example.jithea.testlogin W/dalvikvm﹕ threadid=12: uncaught exception occurred
02-01 23:02:18.304 17643-18052/com.example.jithea.testlogin W/System.err﹕ java.lang.RuntimeException: An error occured while executing doInBackground()
02-01 23:02:18.304 17643-18052/com.example.jithea.testlogin W/System.err﹕ at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-01 23:02:18.304 17643-18052/com.example.jithea.testlogin W/System.err﹕ at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
02-01 23:02:18.305 17643-18052/com.example.jithea.testlogin W/System.err﹕ at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
02-01 23:02:18.305 17643-18052/com.example.jithea.testlogin W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:239)
02-01 23:02:18.305 17643-18052/com.example.jithea.testlogin W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-01 23:02:18.305 17643-18052/com.example.jithea.testlogin W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-01 23:02:18.306 17643-18052/com.example.jithea.testlogin W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-01 23:02:18.306 17643-18052/com.example.jithea.testlogin W/System.err﹕ at java.lang.Thread.run(Thread.java:838)
02-01 23:02:18.306 17643-18052/com.example.jithea.testlogin W/System.err﹕ Caused by: java.lang.NullPointerException
02-01 23:02:18.306 17643-18052/com.example.jithea.testlogin W/System.err﹕ at com.example.jithea.testlogin.NewsActivity$LoadAllProducts.doInBackground(NewsActivity.java:132)
02-01 23:02:18.306 17643-18052/com.example.jithea.testlogin W/System.err﹕ at com.example.jithea.testlogin.NewsActivity$LoadAllProducts.doInBackground(NewsActivity.java:107)
02-01 23:02:18.307 17643-18052/com.example.jithea.testlogin W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-01 23:02:18.307 17643-18052/com.example.jithea.testlogin W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-01 23:02:18.307 17643-18052/com.example.jithea.testlogin W/System.err﹕ ... 4 more
02-01 23:02:18.307 17643-18052/com.example.jithea.testlogin W/dalvikvm﹕ threadid=12: calling UncaughtExceptionHandler
02-01 23:02:18.315 17643-18052/com.example.jithea.testlogin E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
Caused by: java.lang.NullPointerException
at com.example.jithea.testlogin.NewsActivity$LoadAllProducts.doInBackground(NewsActivity.java:132)
at com.example.jithea.testlogin.NewsActivity$LoadAllProducts.doInBackground(NewsActivity.java:107)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
02-01 23:02:18.371 17643-17659/com.example.jithea.testlogin I/SurfaceTextureClient﹕ [STC::queueBuffer] (this:0x528dc150) fps:43.08, dur:1044.57, max:73.51, min:6.02
And here's my NewsActivity.java
public class NewsActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParserNews jParser = new JSONParserNews();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static String url_all_products = "http://agustiniancampusevents.site40.net/newsDB/get_all_news.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_NEWS = "news";
private static final String TAG_PID = "pid";
private static final String TAG_NEWSTITLE = "newstitle";
// products JSONArray
JSONArray products = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = getListView();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
ViewNewsActivity.class);
// sending pid to next activity
in.putExtra(TAG_PID, pid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
// Response from Edit Product Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all product by making HTTP Request
*/
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
*/
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewsActivity.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
*/
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_NEWS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String newstitle = c.getString(TAG_NEWSTITLE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NEWSTITLE, newstitle);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
ViewNewsActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* *
*/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
NewsActivity.this, productsList,
R.layout.news_list_item, new String[]{TAG_PID,
TAG_NEWSTITLE},
new int[]{R.id.pid, R.id.newstitle});
// updating listview
setListAdapter(adapter);
}
});
}
}
}
me again, in my previous post, i was told to use AsyncTask in codes, to avoid mainexceptionthread, and now i'm encountering this errors:
**
01-26 09:39:06.220: E/AndroidRuntime(17218): FATAL EXCEPTION: AsyncTask #1
01-26 09:39:06.220: E/AndroidRuntime(17218): java.lang.RuntimeException: An error occured while executing doInBackground()
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-26 09:39:06.220: E/AndroidRuntime(17218): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-26 09:39:06.220: E/AndroidRuntime(17218): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-26 09:39:06.220: E/AndroidRuntime(17218): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-26 09:39:06.220: E/AndroidRuntime(17218): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-26 09:39:06.220: E/AndroidRuntime(17218): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-26 09:39:06.220: E/AndroidRuntime(17218): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-26 09:39:06.220: E/AndroidRuntime(17218): at java.lang.Thread.run(Thread.java:856)
01-26 09:39:06.220: E/AndroidRuntime(17218): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:867)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4066)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.view.View.invalidate(View.java:10193)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.widget.TextView.invalidateRegion(TextView.java:4375)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.widget.TextView.invalidateCursor(TextView.java:4318)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.widget.TextView.spanChange(TextView.java:7172)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:8759)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:979)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:688)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.text.Selection.setSelection(Selection.java:76)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.text.Selection.setSelection(Selection.java:87)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:302)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.widget.TextView.setText(TextView.java:3535)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.widget.TextView.setText(TextView.java:3405)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.widget.EditText.setText(EditText.java:80)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.widget.TextView.setText(TextView.java:3380)
01-26 09:39:06.220: E/AndroidRuntime(17218): at com.example.projectthesis.Main$phpconnect.doInBackground(Main.java:98)
01-26 09:39:06.220: E/AndroidRuntime(17218): at com.example.projectthesis.Main$phpconnect.doInBackground(Main.java:1)
01-26 09:39:06.220: E/AndroidRuntime(17218): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-26 09:39:06.220: E/AndroidRuntime(17218): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-26 09:39:06.220: E/AndroidRuntime(17218): ... 5 more
**
i think it is because of the Exception e part, which has inputEmail.setText(e.toString());
but when i am changing it to just e.printstacktrace, it results nothing. Can you help me here guys these are my codes:
android:
**
public class Main extends Activity {
EditText inputEmail;
EditText inputPassword;
Button btnLogin;
private ProgressDialog pDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inputEmail = (EditText) findViewById(R.id.inputEmail);
inputPassword = (EditText) findViewById(R.id.inputPassword);
Button btnLogin = (Button) findViewById(R.id.btnLogin);
// button click event
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// creating new product in background thread
validation();
}
});
}
public void validation()
{
if(inputEmail.getText().toString().equals("") || inputPassword.getText().toString().equals(""))
{
Toast.makeText( getApplicationContext(),"Fill Empty Fields",Toast.LENGTH_SHORT ).show();
}
else
{
new phpconnect().execute();
}
}
class phpconnect extends AsyncTask<String, String, String>{
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Main.this);
pDialog.setMessage("Logging in..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected String doInBackground(String... params) {
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("eadd", inputEmail.getText().toString()));
postParameters.add(new BasicNameValuePair("password", inputPassword.getText().toString()));
//Passing Parameter to the php web service for authentication
//String valid = "1";
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://10.0.2.2/TheCalling/log_in.php", postParameters); //Enter Your remote PHP,ASP, Servlet file link
String res=response.toString();
//res = res.trim();
res= res.replaceAll("\\s+","");
//error.setText(res);
if(res.equals("1"))
{
Toast.makeText( getApplicationContext(),"Correct Username or Password",Toast.LENGTH_SHORT ).show();
Intent i = new Intent(Main.this,MainMenu.class);
startActivity(i);
}
else
if(res.equals("0"))
{
Toast.makeText( getApplicationContext(),"Sorry!! Incorrect Username or Password",Toast.LENGTH_SHORT ).show();
}
} catch (Exception e) {
inputEmail.setText(e.toString());
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
**
This again my PHP codes:
**
<?php
include("db_config.php");
$eadd=addslashes($_POST['eadd']);
$password=addslashes($_POST['password']);
$sql="SELECT * FROM users WHERE eadd='$eadd' and password='$password'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$count=mysql_num_rows($result);
if($count==1)
{
echo "1";
//(If result found send 1 to android)
}
else
{
echo "0";
//(If result not found send o to android)
}
?>
**
You get an error because of:
catch (Exception e) {
inputEmail.setText(e.toString());
}
Over here you try to alter the app's UI by altering the contents of an EditText from the background thread of the AsyncTask. Since I doubt you actually want to show an error to the user in the email input field and are doing this just for debugging purposes, try using e.printStackTrace() instead of inputEmail.setText(e.toString());
Additionally, you'll want to wrap your Toast.show() calls in a runOnUiThread() runnable.
You can't show the toast messages in doInBackground just move it to onPostExecute. and yes also this
catch (Exception e) {
inputEmail.setText(e.toString());
}