0

I have tried everything please can anyone help me with this code. It gives error parsing org.json.jsonexception value br type java.lang.String cannot be converted to JSONOobject error. Except this error logcat shows nothing. I think the problem is within the Post Login() method but i could not figure out what is the problem. The problem comes when i click the send button. The problem might be within send button and PostLogin method.And i m new in stack overflow so if something goes wrong please cooperate.I have lost 3 days in doing it but couldn't see what is the real problem.Please anyone how to solve it.Its an emergency.

public class Credit extends Activity implements OnClickListener{

int mYear, mMonth, mDay;
EditText Epickupdate;
Reserve r;
String msg,status;
EditText etname_on_card,etcard_number,etcvv,user_id;
String sflno,stime,sdate,sadd,sdrop_add,sinst;
Button send;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.credit);
    getActionBar().hide();

    StrictMode.ThreadPolicy policy = new    StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy); 

    Epickupdate = (EditText)findViewById(R.id.expiry_date);
    Epickupdate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            datepicker();
        }
    });

        user_id=(EditText)findViewById(R.id.user_id);
        etname_on_card=(EditText)findViewById(R.id.name_on_card);
        etcard_number=(EditText)findViewById(R.id.card_number);

        etcvv=(EditText)findViewById(R.id.cvv);

        send=(Button)findViewById(R.id.proceedcheck);
        send.setOnClickListener(this);  
}

public void postLoginData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost("http://atlcity.qa.apps.jasperit.net/index.php");

    SharedPreferences pr= getSharedPreferences("reserve", 0);
    String sflno=pr.getString("flightno", "null");
    String stime=pr.getString("stime", "null");
    String sdate=pr.getString("sdate", "null");
    String  sadd=pr.getString("sadd", "null");
    String sdrop_add=pr.getString("sdrop_add", "null");
    String  sinst=pr.getString("sinst", "null");

    try {
        // Add user name and password

        String sname_on_card = etname_on_card.getText().toString();         
        String scard_number = etcard_number.getText().toString();
        String expiry_dates = Epickupdate.getText().toString();         
        String cvvs = etcvv.getText().toString();           
        String suser_id = user_id.getText().toString();

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("action", "makeReservation"));
        nameValuePairs.add(new BasicNameValuePair("app_secret", "jkhljkUILJGJkljhkjUGLG87796587687HGKJhghkjKUYGKJHjhgjUYGKUY7865876hgKUYGK"));
        nameValuePairs.add(new BasicNameValuePair("flight_number", sflno));
        nameValuePairs.add(new BasicNameValuePair("pick_up_time", stime));
        nameValuePairs.add(new BasicNameValuePair("pick_up_date",sdate));
        nameValuePairs.add(new BasicNameValuePair("pick_up_address",sadd));
        nameValuePairs.add(new BasicNameValuePair("drop_off_address", sdrop_add));
        nameValuePairs.add(new BasicNameValuePair("special_instruction", sinst));
        nameValuePairs.add(new BasicNameValuePair("user_id", suser_id));            
        nameValuePairs.add(new BasicNameValuePair("name_on_card",sname_on_card));
        nameValuePairs.add(new BasicNameValuePair("card_number",scard_number));
        nameValuePairs.add(new BasicNameValuePair("expiry_date", expiry_dates));
        nameValuePairs.add(new BasicNameValuePair("cvv", cvvs));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        String str = inputStreamToString(response.getEntity().getContent()).toString();
        Log.w("Response", str);
        try {
            JSONObject reader = new JSONObject(str);
            msg = reader.getString("msg");// server le request gareko
            status = reader.getString("status");
            Toast.makeText(getApplicationContext()," "+msg,Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "parsing error "+e, Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private StringBuilder inputStreamToString(InputStream is) {
    String line = "";
    StringBuilder total = new StringBuilder();
    // Wrap a BufferedReader around the InputStream
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    // Read response until the end
    try {
        while ((line = rd.readLine()) != null) {
            total.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    // Return full string
    return total;
}

@Override
public void onClick(View v) {

String sname_on_card = etname_on_card.getText().toString();         
String scard_number = etcard_number.getText().toString();
String expiry_dates = Epickupdate.getText().toString();         
String cvvs = etcvv.getText().toString();           
String suser_id = user_id.getText().toString();

    try {
        if (v.getId() == R.id.proceedcheck) {       
            if (sname_on_card.matches("")) {
                Toast.makeText(this, "please fill up all the field", Toast.LENGTH_SHORT).show();
                return;
            }else if(scard_number.matches("")){
                 Toast.makeText(this, "please fill up all the field", Toast.LENGTH_SHORT).show();
                return;
            }else if(expiry_dates.matches("")){
                 Toast.makeText(this, "please fill up all the field", Toast.LENGTH_SHORT).show();
                return;
            }else if(cvvs.matches("")){
                 Toast.makeText(this, "please fill up all the field", Toast.LENGTH_SHORT).show();
                return;
            }else if(suser_id.matches("")){
                 Toast.makeText(this, "please fill up all the field", Toast.LENGTH_SHORT).show();
                return;
            }else{
                try {
                    postLoginData();
                } catch (Exception e) {
                    Toast.makeText(getBaseContext(), "postlogin error " +e,Toast.LENGTH_LONG);
                    e.printStackTrace();
                }

            }}

    } catch (Exception e) {
         Toast.makeText(this,"error in onclick"+e, Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }

}

}

Logcat:

09-05 00:48:11.147: W/Response(1635): <br /><b>Fatal error</b>:  Call to a member function fetch_object() on a non-object in <b>/home/appsjasp/public_html/atlcity.qa/index.php</b> on line <b>124</b><br />
09-05 00:48:11.177: W/System.err(1635): org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
09-05 00:48:11.187: W/System.err(1635):     at org.json.JSON.typeMismatch(JSON.java:111)
09-05 00:48:11.187: W/System.err(1635):     at org.json.JSONObject.<init>(JSONObject.java:159)
09-05 00:48:11.197: W/System.err(1635):     at org.json.JSONObject.<init>(JSONObject.java:172)
09-05 00:48:11.197: W/System.err(1635):     at example.atlcitylimo.Credit.postLoginData(Credit.java:172)
09-05 00:48:11.197: W/System.err(1635):     at example.atlcitylimo.Credit.onClick(Credit.java:247)
09-05 00:48:11.197: W/System.err(1635):     at android.view.View.performClick(View.java:4438)
09-05 00:48:11.207: W/System.err(1635):     at android.view.View$PerformClick.run(View.java:18422)
09-05 00:48:11.207: W/System.err(1635):     at android.os.Handler.handleCallback(Handler.java:733)
09-05 00:48:11.207: W/System.err(1635):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-05 00:48:11.207: W/System.err(1635):     at android.os.Looper.loop(Looper.java:136)
09-05 00:48:11.207: W/System.err(1635):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-05 00:48:11.207: W/System.err(1635):     at java.lang.reflect.Method.invokeNative(Native Method)
09-05 00:48:11.207: W/System.err(1635):     at java.lang.reflect.Method.invoke(Method.java:515)
09-05 00:48:11.217: W/System.err(1635):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-05 00:48:11.227: W/System.err(1635):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-05 00:48:11.227: W/System.err(1635):     at dalvik.system.NativeStart.main(Native Method)
8
  • post your logcat. According to this error java.lang.String cannot be converted to JSONOobject error it says you are converting String to JSONObject . Commented Sep 5, 2014 at 4:44
  • can you please post json respone ? Commented Sep 5, 2014 at 4:45
  • logcat is blank.. the above error also pops up in toast. there is nothing in logcat Commented Sep 5, 2014 at 4:45
  • Please post what response you are getting from the server. Commented Sep 5, 2014 at 4:46
  • 1
    Please post what response you are getting from the server. as @Aniruddha said Commented Sep 5, 2014 at 4:51

1 Answer 1

1

As Response from Server is not an JSONObject its firing exception org.json.JSONException

Response

<br /><b>Fatal error</b>:  Call to a member function fetch_object() on a non-object in <b>/home/appsjasp/public_html/atlcity.qa/index.php</b> on line <b>124</b><br />

Response is in XML instead of JSON so it will fire org.json.JSONException while creating object of JSONObject and passing XML String

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.