Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

value <br of type java.lang.String cannot be converted to JSONObject In Android Programming

When I am Create new Android Application to Fetch data from my server like xampp, But Its not Retrieval and i am using catch statement for itz exception then it says as JSONObject cannot convert it, How to Solve?

Solution for this, Please Help me? Thanks in Advance

My Full Java Code

   package com.example.fetch;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;


import android.app.Activity;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;


public class Disp extends Activity {
    TextView user,email,phone;
    String abdu,hos,em,ph;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.disp);
        user=(TextView)findViewById(R.id.user);
        email=(TextView)findViewById(R.id.email);
        phone=(TextView)findViewById(R.id.phone);
        SharedPreferences sandra=getApplicationContext().getSharedPreferences("veena", MODE_WORLD_READABLE);
        abdu=sandra.getString("shyma", "");
        user.setText(abdu);
        aji aj=new aji();
        aj.execute();
    }
    class aji extends AsyncTask<Void, Void, Void>
    {

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            try
            {
                DefaultHttpClient hc=new DefaultHttpClient();
                ResponseHandler<String> res=new BasicResponseHandler();
                HttpPost postMethod=new HttpPost("http://10.0.2.2/std/ret.php");
                List<NameValuePair>nameValuePairs=new ArrayList<NameValuePair>(1);
                nameValuePairs.add(new BasicNameValuePair("username",abdu));
                postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                 hos = hc.execute(postMethod,res);
                 System.out.println(hos);

            }
            catch(Exception e)
            {
                System.out.println("Error:"+e);
                Log.e("Fail 1", e.toString());
                Toast.makeText(getApplicationContext(), "Invalid Login",
                        Toast.LENGTH_LONG).show();

            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            parsingmethod();
        }
    }



    public void parsingmethod()
    {
        try
        {
            JSONObject jobject=new JSONObject(hos);
            JSONObject jobject1=jobject.getJSONObject("Event");
            JSONArray ja=jobject1.getJSONArray("Details");
            int length=ja.length();
            for(int i=0;i<length;i++)
            {
                JSONObject data1=ja.getJSONObject(i);

                em=data1.getString("email");
                ph=data1.getString("pass");

                System.out.println(em+ph);
                email.setText(em);
                phone.setText(ph);

            }
        }
        catch(Exception e)
        {
            /*System.out.println("error:"+e);
            Log.e("Fail 2", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP Address",
                    Toast.LENGTH_LONG).show();*/
            e.printStackTrace();
            displayExceptionMessage(e.getMessage());
        }
        }



    private void displayExceptionMessage(String message) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
    }
    }

Catch Exception Shows that value

My Log Cat

01-20 16:22:31.432: I/System.out(10797): Error:org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused
01-20 16:22:31.452: I/System.out(10797): error:java.lang.NullPointerException
01-20 16:24:01.342: I/System.out(10996): error:org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-20 16:44:57.302: I/System.out(11503): error:org.json.JSONException: No value for Event
01-20 16:50:19.062: I/System.out(11741): error:org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-20 16:50:59.572: I/System.out(11960): error:org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-20 16:50:59.572: E/Fail 2(11960): org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-20 16:51:26.462: I/PGA(12155): New SOCKET connection: m.example.fetch (pid 12155, tid 12155)
01-20 16:51:29.142: I/System.out(12155): error:org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-20 16:51:29.142: E/Fail 2(12155): org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-20 16:51:49.742: I/System.out(12347): error:org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-20 16:51:49.742: E/Fail 2(12347): org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-20 16:52:11.062: I/System.out(12540): error:org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-20 16:52:11.062: E/Fail 2(12540): org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-20 17:05:45.082: W/System.err(13814): org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-20 17:05:45.082: W/System.err(13814):    at org.json.JSON.typeMismatch(JSON.java:111)
01-20 17:05:45.082: W/System.err(13814):    at org.json.JSONObject.<init>(JSONObject.java:158)
01-20 17:05:45.082: W/System.err(13814):    at org.json.JSONObject.<init>(JSONObject.java:171)
01-20 17:05:45.082: W/System.err(13814):    at com.example.fetch.Disp.parsingmethod(Disp.java:86)
01-20 17:05:45.082: W/System.err(13814):    at com.example.fetch.Disp$aji.onPostExecute(Disp.java:76)
01-20 17:05:45.082: W/System.err(13814):    at com.example.fetch.Disp$aji.onPostExecute(Disp.java:1)
01-20 17:05:45.082: W/System.err(13814):    at android.os.AsyncTask.finish(AsyncTask.java:602)
01-20 17:05:45.082: W/System.err(13814):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
01-20 17:05:45.082: W/System.err(13814):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
01-20 17:05:45.082: W/System.err(13814):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 17:05:45.082: W/System.err(13814):    at android.os.Looper.loop(Looper.java:137)
01-20 17:05:45.082: W/System.err(13814):    at android.app.ActivityThread.main(ActivityThread.java:4424)
01-20 17:05:45.082: W/System.err(13814):    at java.lang.reflect.Method.invokeNative(Native Method)
01-20 17:05:45.082: W/System.err(13814):    at java.lang.reflect.Method.invoke(Method.java:511)
01-20 17:05:45.082: W/System.err(13814):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
01-20 17:05:45.082: W/System.err(13814):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
01-20 17:05:45.082: W/System.err(13814):    at dalvik.system.NativeStart.main(Native Method)
01-20 17:05:51.882: W/System.err(13814): org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-20 17:05:51.882: W/System.err(13814):    at org.json.JSON.typeMismatch(JSON.java:111)
01-20 17:05:51.882: W/System.err(13814):    at org.json.JSONObject.<init>(JSONObject.java:158)
01-20 17:05:51.882: W/System.err(13814):    at org.json.JSONObject.<init>(JSONObject.java:171)
01-20 17:05:51.892: W/System.err(13814):    at com.example.fetch.Disp.parsingmethod(Disp.java:86)
01-20 17:05:51.892: W/System.err(13814):    at com.example.fetch.Disp$aji.onPostExecute(Disp.java:76)
01-20 17:05:51.892: W/System.err(13814):    at com.example.fetch.Disp$aji.onPostExecute(Disp.java:1)
01-20 17:05:51.892: W/System.err(13814):    at android.os.AsyncTask.finish(AsyncTask.java:602)
01-20 17:05:51.892: W/System.err(13814):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
01-20 17:05:51.892: W/System.err(13814):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
01-20 17:05:51.892: W/System.err(13814):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 17:05:51.892: W/System.err(13814):    at android.os.Looper.loop(Looper.java:137)
01-20 17:05:51.892: W/System.err(13814):    at android.app.ActivityThread.main(ActivityThread.java:4424)
01-20 17:05:51.892: W/System.err(13814):    at java.lang.reflect.Method.invokeNative(Native Method)
01-20 17:05:51.892: W/System.err(13814):    at java.lang.reflect.Method.invoke(Method.java:511)
01-20 17:05:51.892: W/System.err(13814):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
01-20 17:05:51.892: W/System.err(13814):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
01-20 17:05:51.892: W/System.err(13814):    at dalvik.system.NativeStart.main(Native Method)
01-20 17:06:26.712: W/System.err(13814): org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-20 17:06:26.712: W/System.err(13814):    at org.json.JSON.typeMismatch(JSON.java:111)
01-20 17:06:26.712: W/System.err(13814):    at org.json.JSONObject.<init>(JSONObject.java:158)
01-20 17:06:26.712: W/System.err(13814):    at org.json.JSONObject.<init>(JSONObject.java:171)
01-20 17:06:26.712: W/System.err(13814):    at com.example.fetch.Disp.parsingmethod(Disp.java:86)
01-20 17:06:26.712: W/System.err(13814):    at com.example.fetch.Disp$aji.onPostExecute(Disp.java:76)
01-20 17:06:26.712: W/System.err(13814):    at com.example.fetch.Disp$aji.onPostExecute(Disp.java:1)
01-20 17:06:26.712: W/System.err(13814):    at android.os.AsyncTask.finish(AsyncTask.java:602)
01-20 17:06:26.712: W/System.err(13814):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
01-20 17:06:26.712: W/System.err(13814):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
01-20 17:06:26.712: W/System.err(13814):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 17:06:26.712: W/System.err(13814):    at android.os.Looper.loop(Looper.java:137)
01-20 17:06:26.712: W/System.err(13814):    at android.app.ActivityThread.main(ActivityThread.java:4424)
01-20 17:06:26.712: W/System.err(13814):    at java.lang.reflect.Method.invokeNative(Native Method)
01-20 17:06:26.712: W/System.err(13814):    at java.lang.reflect.Method.invoke(Method.java:511)
01-20 17:06:26.712: W/System.err(13814):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
01-20 17:06:26.712: W/System.err(13814):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
01-20 17:06:26.712: W/System.err(13814):    at dalvik.system.NativeStart.main(Native Method)
01-20 17:07:20.232: W/System.err(13814): org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-20 17:07:20.242: W/Sy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

what i can see, in your java code, you are sending http-param username but in your PHP page you are reading param name user $_REQUEST['user'] i think this may cause an issue... it will be like NULL in php ?

try to use same name on both Java and PHP you need to change the line in android as below

nameValuePairs.add(new BasicNameValuePair("user",abdu));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...