2

my app is working perfect when i am saving data to web server,but when i save it on local host database it is giving Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject error..

here is my code:

try
    {
     HttpClient httpclient = new DefaultHttpClient();   
     HttpPost httppost = new HttpPost("www.website.com/exmpl/insertData.php");      //working perfectly
     HttpPost httppost = new HttpPost("http://192.168.0.1/exmpl/insertData.php");
     httppost.setEntity(new UrlEncodedFormEntity(params));
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity();
     is = entity.getContent();
     Log.e("pass 1", "connection success ");         
    }

log cat:

01-05 19:02:53.150: E/pass 1(28604): connection success 
01-05 19:02:53.410: E/Fail 3(28604): org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

EDIT errors if i change the ip address to 192.168.0.123

01-06 10:00:37.560: E/Fail 1(4261): org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.0.123 refused
01-06 10:00:37.570: E/AndroidRuntime(4261): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

and i am not using async task,,i am extending activity

even tried 192.168.0.123:80 followed MR.HU'NG

Error again

Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
Prabs
  • 4,923
  • 6
  • 38
  • 59

1 Answers1

0

The return value of 192.168.0.1/exmpl/insertData.php on the ANDROID DEVICE are not the same of your PC, because, 192.168.0.1 in the DEVICE points to itself, not to you apache server.

You must use the IP of your machine, assigned by the local router, or configure your emulator to use some fixed ip.

Probably, the 192.168.0.1/exmpl/insertData.php are returning something like "page not found".

Tiago Gouvêa
  • 15,036
  • 4
  • 75
  • 81
  • `127.0.0.1` is always local, so 192.168.0.1 might very well be the IP assigned to the PC by the router (although I would have guessed it's the router itself, not the client PC). So if phone and PC are in the same LAN, why wouldn't it work.. – Blacklight Jan 05 '15 at 15:16
  • @Tiago now i changed the ip address to local ip and updated my question with the errors..please have a look and i even tried all the solutions provided at http://stackoverflow.com/questions/20015883/connecting-localhost-via-android-connection-to-10-0-2-2-refused but nothing worked – Prabs Jan 06 '15 at 04:48
  • A easy way to test it, is open the browser on the device and type that url there. If it open, it will run ok on your app, if not, the app will go bad too. Check you machine IP on you local network and use it on your code. – Tiago Gouvêa Jan 13 '15 at 12:09
  • @TiagoGouvêa almost an year.. Your help did worked. the address that worked for me is: " http://192.168.0.130/PrabS/books.php " i.e., System IP address and htdocs folder. Actually "String cannot be converted to JSONObject error.." was the json response error – Prabs Dec 31 '15 at 07:11