1

hi I have made my first android application and it got a webservices to connect to mysql DB in my local PC using XAMPP I can run it perfectly from the Android emulator with no hassle but I need to run from my real device but I got the issue of the connection to the DB

so far I have changed the IP address in the file that contains the connection details for the web service and also the userfunction.java in eclipse (this one for the user reg. and login functions)

in the phone side , my PC uses LAN and my phone connects to the same connection using connectify wireless

but when I click on login button from my real device I got this in my logcat

05-18 12:19:40.600: E/SensorManager(13663): thread start
05-18 12:19:40.725: E/SpannableStringBuilder(13663): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-18 12:19:40.725: E/SpannableStringBuilder(13663): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-18 12:19:47.695: E/JSON Parser(13663): Error parsing data org.json.JSONException: End of input at character 0 of 
05-18 12:19:47.700: E/AndroidRuntime(13663): FATAL EXCEPTION: main
05-18 12:19:47.700: E/AndroidRuntime(13663): java.lang.NullPointerException
05-18 12:19:47.700: E/AndroidRuntime(13663):    at com.mohammed.watzIslam.LoginActivity$1.onClick(LoginActivity.java:62)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at android.view.View.performClick(View.java:4211)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at android.view.View$PerformClick.run(View.java:17267)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at android.os.Handler.handleCallback(Handler.java:615)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at android.os.Looper.loop(Looper.java:137)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at android.app.ActivityThread.main(ActivityThread.java:4898)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at java.lang.reflect.Method.invokeNative(Native Method)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at java.lang.reflect.Method.invoke(Method.java:511)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at dalvik.system.NativeStart.main(Native Method)

this is the userfunction.java that keeps the location address of user reg. and login webserives

\
public class UserFunctions {

private JSONParser jsonParser;

private static String loginURL = "http://xxxx:port#/android_login_api/";
private static String registerURL = "http://xxxx:port#/android_login_api/";

private static String login_tag = "login";
private static String register_tag = "register";

// constructor
public UserFunctions(){
jsonParser = new JSONParser();
}

/**
* function make Login Request
* @param email
* @param password
* */
public JSONObject loginUser(String email, String password){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
// return json
// Log.e("JSON", json.toString());
return json;
}

/**
* function make reg. Request
* @param name
* @param email
* @param password
* */
public JSONObject registerUser(String name, String password, String email){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", register_tag));
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));



// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
// return json
return json;
}

/**
* Function get Login status
* */
public boolean isUserLoggedIn(Context context){
DatabaseHandler db = new DatabaseHandler(context);
int count = db.getRowCount();
if(count > 0){
// user logged in
return true;
}
return false;
}

/**
* Function to logout user
* Reset Database
* */
public boolean logoutUser(Context context){
DatabaseHandler db = new DatabaseHandler(context);
db.resetTables();
return true;
}

}

can someone points out for any mistake or suggest me a solution

thanks

user2396035
  • 163
  • 1
  • 1
  • 10
  • `Error parsing data org.json.JSONException`. <--- To fix – Anirudh Ramanathan May 18 '13 at 04:39
  • If it works in the emulator then I'd bet that it's a connectivity issue. The first thing I would do is connect to the web service from another pc and see if I can get all the correct responses. The JSON error is that the JSON response from the server is 0 bytes long - again which point so the connectivity not being the same as from the emulator. Please post the code that connects to the service. – Preet Sangha May 18 '13 at 04:41
  • @DarkCthulhu but when I run it last time on the emulator it was okay...I'm just thinking it might be a problem with the connection – user2396035 May 18 '13 at 04:46
  • @user2396035 That seems likely. – Anirudh Ramanathan May 18 '13 at 04:47
  • @PreetSangha in my application I have reg. login also a link to a website"froum" in my DB so which code of them should I post here – user2396035 May 18 '13 at 04:48
  • @DarkCthulhu and that's what I need to find a solution for!!! – user2396035 May 18 '13 at 04:48
  • com.mohammed.watzIslam.LoginActivity$1.onClick – Preet Sangha May 18 '13 at 04:51
  • http://stackoverflow.com/questions/13670374/android-span-exclusive-exclusive-spans-cannot-have-a-zero-length/13986224#13986224 – Gunaseelan May 18 '13 at 04:51
  • Type the url in your browser of your device and see if you can access it – Homam May 18 '13 at 04:55
  • @PreetSangha my login activity it keeps no ip address there is another .java file which I mentioned in my question userfunction that holds the location addresses for the webservice...I guess that will help give me a sec. will update the question with the code – user2396035 May 18 '13 at 04:55
  • @Homam I can access the website in my PC from the phone browser only if both devices are connected to the same wifi....but if my PC is on LAN and my phone sharing the LAN through connectify it doesn't work...!!!! – user2396035 May 18 '13 at 05:56
  • Have you tested after connecting both devices to same wifi? Do you still get the same error? – Homam May 18 '13 at 06:48
  • @Homam yup..it takes a very long time after I click login then it says the application is not responding (wait,close)..then crashes – user2396035 May 18 '13 at 09:17
  • @Homam I forgot to point that when my on wifi and I try to open the website on my browser I only can reach it through the LAN ip address not the wifi address of my PC. isn't strange???? – user2396035 May 18 '13 at 09:33
  • I don't understand you clearly. So I give you example. Suppose your ip of pc is http://192.168.0.10 and if port number for xampp is 8080. Then access this url (http://192.168.0.10:8080) in your android mobile device's browser. See if it shows the default page of xampp. If it shows then you can register and login. Also Use networking code within AsyncTask avoid ANR (Application Not Responding) error. – Homam May 18 '13 at 11:31
  • @Homam I can access the server from my phone browser using public wifi but the application can't reach... – user2396035 May 19 '13 at 03:51

1 Answers1

0

Add these packages:

import android.app.ProgressDialog;
import android.os.AsyncTask;

Declare json as class member field:

private JSONObject json;

Replace JSONObject json = jsonParser.getJSONFromUrl(loginURL, params); with this:

new AsyncTask<Void, Void, Void>() {
    ProgressDialog progressDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(SplashActivity.this);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("Loading, please wait...");
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... pms) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", login_tag));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));
        json = jsonParser.getJSONFromUrl(loginURL, params);
        return null;
    }
    protected void onPostExecute(Void result) {
        if(progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        if(json != null) {
            Log.e("JSON", json.toString());
        }
        else {
            Log.e("JSON", "JSON is NULL");
        }
    };
}.execute();
Homam
  • 5,018
  • 4
  • 36
  • 39
  • but it gives me error AsyncTask cannot be resolved to a type and some others but I guess this is the main cause – user2396035 May 20 '13 at 08:03
  • I have edited my answer to add packages. Please include those packages above in your project.Also copy the entire code properly from new to execute for AsyncTask – Homam May 20 '13 at 09:34
  • now I got this SplashActivity cannot be resolved to a type and The method getJSONFromUrl(String, List) in the type JSONParser is not applicable for the arguments (String, Void[]) and also at the log.e what can I do – user2396035 May 20 '13 at 09:43
  • @user2396035 I have made the changes again. Again copy paste the entire thing and replace it with the old one. Press ctrl+shift+o to import the required packages automatically. – Homam May 20 '13 at 11:58
  • I still getting the same errors plus error with email and password...but I notice you made this as for login put you asked me to place it within the register user methods that should cause a conflict ,isn't it ?? – user2396035 May 20 '13 at 12:15
  • @user2396035 Yes, I have changed that. Declare email id and password as a class member. Assign the value to email and password before executing asynctask – Homam May 20 '13 at 12:29
  • but I don't understand how come my main method is registeruser and it contains the asynctask that holds only the email and password "not equal parameters" also what about this one json = jsonParser.getJSONFromUrl(loginURL, params); don't you think it's in the wrong place with the loginURL shouldn't it be registerURL I hope you could help me to understand this – user2396035 May 20 '13 at 13:33