Hi all,

I'm facing problem when trying to use JSON object to access the .net project
database.
please let me know, where i'm doing mistake, i given more than 4 days to
rectify.

logcat details -

05-16 17:32:26.132: INFO/System.out(1656): response.getProtocolVersion()
=HTTP/1.0
05-16 17:32:26.132: INFO/System.out(1656):
response.getStatusLine().getStatusCode()  =404
05-16 17:32:26.132: INFO/System.out(1656):
response.getStatusLine().getReasonPhrase()  = Not Found
05-16 17:32:26.141: INFO/System.out(1656):
response.getStatusLine().toString()   =HTTP/1.0 404 Not Found
05-16 17:32:26.141: INFO/System.out(1656): inside try 3
05-16 17:32:26.151: INFO/System.out(1656): connection entity.toString()=
org.apache.http.conn.BasicManagedEntity@43ea4288
05-16 17:32:26.161: INFO/global(1656): Default buffer size used in
BufferedReader constructor. It would be better to be explicit if an 8k-char
buffer is required.
05-16 17:32:26.161: INFO/System.out(1656): result =
05-16 17:32:26.171: WARN/System.err(1656): org.json.JSONException: End of
input at character 0 of
05-16 17:32:26.182: WARN/System.err(1656):     at
org.json.JSONTokener.syntaxError(JSONTokener.java:446)
05-16 17:32:26.182: WARN/System.err(1656):     at
org.json.JSONTokener.nextValue(JSONTokener.java:93)
05-16 17:32:26.182: WARN/System.err(1656):     at
org.json.JSONObject.<init>(JSONObject.java:154)
05-16 17:32:26.182: WARN/System.err(1656):     at
org.json.JSONObject.<init>(JSONObject.java:171)
05-16 17:32:26.182: WARN/System.err(1656):     at
com.sst.restaurant.RestJsonClient.connect(RestJsonClient.java:75)
05-16 17:32:26.192: WARN/System.err(1656):     at
com.sst.restaurant.LoginDetails.onClick(LoginDetails.java:141)
05-16 17:32:26.192: WARN/System.err(1656):     at
android.view.View.performClick(View.java:2408)
05-16 17:32:26.192: WARN/System.err(1656):     at
android.view.View$PerformClick.run(View.java:8816)
05-16 17:32:26.192: WARN/System.err(1656):     at
android.os.Handler.handleCallback(Handler.java:587)
05-16 17:32:26.192: WARN/System.err(1656):     at
android.os.Handler.dispatchMessage(Handler.java:92)
05-16 17:32:26.212: WARN/System.err(1656):     at
android.os.Looper.loop(Looper.java:123)
05-16 17:32:26.306: DEBUG/dalvikvm(1656): GC_FOR_MALLOC freed 2764 objects /
179392 bytes in 92ms
05-16 17:32:26.306: WARN/System.err(1656):     at
android.app.ActivityThread.main(ActivityThread.java:4627)
05-16 17:32:26.306: WARN/System.err(1656):     at
java.lang.reflect.Method.invokeNative(Native Method)
05-16 17:32:26.311: WARN/System.err(1656):     at
java.lang.reflect.Method.invoke(Method.java:521)
05-16 17:32:26.311: WARN/System.err(1656):     at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-16 17:32:26.311: WARN/System.err(1656):     at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-16 17:32:26.311: WARN/System.err(1656):     at
dalvik.system.NativeStart.main(Native Method)
05-16 17:32:26.321: WARN/System.err(1656): org.json.JSONException: No value
for Login
05-16 17:32:26.321: WARN/System.err(1656):     at
org.json.JSONObject.get(JSONObject.java:354)
05-16 17:32:26.321: WARN/System.err(1656):     at
org.json.JSONObject.getString(JSONObject.java:510)
05-16 17:32:26.341: WARN/System.err(1656):     at
com.sst.restaurant.LoginDetails.onClick(LoginDetails.java:142)
05-16 17:32:26.341: WARN/System.err(1656):     at
android.view.View.performClick(View.java:2408)
05-16 17:32:26.341: WARN/System.err(1656):     at
android.view.View$PerformClick.run(View.java:8816)
05-16 17:32:26.341: WARN/System.err(1656):     at
android.os.Handler.handleCallback(Handler.java:587)
05-16 17:32:26.341: WARN/System.err(1656):     at
android.os.Handler.dispatchMessage(Handler.java:92)
05-16 17:32:26.341: WARN/System.err(1656):     at
android.os.Looper.loop(Looper.java:123)
05-16 17:32:26.341: WARN/System.err(1656):     at
android.app.ActivityThread.main(ActivityThread.java:4627)
05-16 17:32:26.341: WARN/System.err(1656):     at
java.lang.reflect.Method.invokeNative(Native Method)
05-16 17:32:26.341: WARN/System.err(1656):     at
java.lang.reflect.Method.invoke(Method.java:521)
05-16 17:32:26.352: WARN/System.err(1656):     at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-16 17:32:26.352: WARN/System.err(1656):     at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-16 17:32:26.372: WARN/System.err(1656):     at
dalvik.system.NativeStart.main(Native Method)


my source code -  jsno client code -

public class RestJsonClient {

    public static JSONObject connect(String url) {
        String result = null;
        System.out.println("result = " + result);
        HttpClient httpclient = new DefaultHttpClient();

        // Prepare a request object
        HttpGet httpget = new HttpGet("http://10.91.28.203";);

        // Execute the request
        HttpResponse response;

        JSONObject json = new JSONObject();
        System.out.println("result = " + result);
        System.out.println("json object " + json.toString());

        try {
            System.out.println("inside try 1");
            response = httpclient.execute(httpget);

            System.out.println("inside try 2");
            System.out.println("response.getProtocolVersion() ="
                    + response.getProtocolVersion());
            System.out.println("response.getStatusLine().getStatusCode()  ="
                    + response.getStatusLine().getStatusCode());
            System.out.println("response.getStatusLine().getReasonPhrase()
= "
                    + response.getStatusLine().getReasonPhrase());
            System.out.println("response.getStatusLine().toString()   ="
                    + response.getStatusLine().toString());

            HttpEntity entity = response.getEntity();
            System.out.println("inside try 3");

            System.out.println("connection entity.toString()= "
                    + entity.toString());

            if (entity != null) {

                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                result = convertStreamToString(instream);
                System.out.println("result = " + result);

                json = new JSONObject(result);

                instream.close();
            }

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return json;
    }

    /**
     *
     * @param is
     * @return String
     */
    public static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new
InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

}

LoginDetails page -
public class LoginDetails extends Activity implements OnClickListener {

    private static final int SERVER_PORT = 1311;

    private static final String DEB_TAG = "Json_Android";

    private String SERVER_HOST = "http://localhost:1311/";;

    public static final String PREFS_NAME = "HelloAndroidPREFS";
    private SharedPreferences settings;

    private ProgressDialog progress;
    JSONObject json;

    /*
     * protected LoginScreen(Context paramContext) { super(paramContext);
     * AppInstance.getInstance().LoadPreferences(paramContext); }
     */

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle icicle) {

        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        super.onCreate(icicle);

        // Restore preferences

        settings = getSharedPreferences(PREFS_NAME, 0);

        // load up the layout

        setContentView(R.layout.main3);

        // get the button resource in the xml file and assign it to a local
        // variable of type Button

        Button login = (Button) findViewById(R.id.login_button);

        Log.i(DEB_TAG, "onCreate");

        login.setOnClickListener(this);

        setUserNameText(settings.getString("Login", ""));

        setPasswordText(settings.getString("Password", ""));

    }

    public void setUserNameText(String $username) {
        EditText usernameEditText = (EditText)
findViewById(R.id.txt_username);
        usernameEditText.setText($username);
    }

    public void setPasswordText(String username) {
        EditText passwordEditText = (EditText)
findViewById(R.id.txt_password);
        passwordEditText.setText(username);

    }

    /*
     *
     * (non-Javadoc)
     *
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */

    public void onClick(View v) {

        // Handle based on which view was clicked.

        Log.i(DEB_TAG + " onClick ", "onClick");

        // this gets the resources in the xml file

        // and assigns it to a local variable of type EditText

        EditText usernameEditText = (EditText)
findViewById(R.id.txt_username);

        EditText passwordEditText = (EditText)
findViewById(R.id.txt_password);

        // the getText() gets the current value of the text box

        // the toString() converts the value to String data type

        // then assigns it to a variable of type String

        String sUserName = usernameEditText.getText().toString();

        String sPassword = passwordEditText.getText().toString();

        // call the backend using Get parameters (discouraged but works good
for
        // this exampl <img
        // src="
http://www.instropy.com/wp-includes/images/smilies/icon_wink.gif";
        // alt=";)" class="wp-smiley"> )

        String address = "http://"; + SERVER_HOST + ":" + SERVER_PORT
                + "/Login.aspx?action=login&Login=" + sUserName +
"&Password="
                + sPassword + "";

        if ((usernameEditText == null) || (passwordEditText == null)) {
            System.out.println("User Name does not exit");
            Toast.makeText(this.getBaseContext(),
                    "Invalid Username or password", 0).show();
        } else {
            // display the username and the password in string format
            // Toast.makeText(getBaseContext(), "valid Username or
password",
            // 0).show();
            try {

                showBusyCursor(true);

                progress = ProgressDialog.show(this,

                "Please wait...", "Login in process", true);

                Log.i(DEB_TAG, "Username: " + sUserName + " Password: "
                        + sPassword);

                Log.i(DEB_TAG, "Requesting to " + address);

                json = RestJsonClient.connect(address);
                System.out.println("json =" + json.getString("Login"));

            } catch (Exception e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

                showBusyCursor(false);

            }// end try

            progress.dismiss();

            SharedPreferences.Editor editor = settings.edit();
            editor.putString("Login", sUserName);
            editor.putString("Password", sPassword);

            editor.commit();
            showBusyCursor(false);
            next();

        }// end else

    }// end OnClick

    /*

     *

     */

    private void showBusyCursor(Boolean $show) {
        setProgressBarIndeterminateVisibility($show);
    }

    private void next() {
        // you can call another activity by uncommenting the above lines
        Intent myIntent = new Intent(this.getBaseContext(),
                RestaurantHome.class);
        startActivityForResult(myIntent, 0);
    }
    /*
     * protected String doInBackground(String[] paramArrayOfString) {
WebService
     * localWebService = new WebService(); WebService.WebServiceCall
     * localWebServiceCall = WebService.WebServiceCall.LOGIN; Object[]
     * arrayOfObject = new Object[2]; String str1 = paramArrayOfString[0];
     * arrayOfObject[0] = str1; String str2 = paramArrayOfString[1];
     * arrayOfObject[1] = str2; return
     * localWebService.execute(localWebServiceCall, true, arrayOfObject); }
     */

}
-- 
Thanks & Regards

Rakesh Kumar Jha

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to