php - Android web service not parsing data from localhost -
i trying connect android app java webservice php parsed values.
php script :
<?php $data = array('name' => 'froyo', 'version' => 'android 2.2'); print (json_encode($data)); ?>
and java webservice :
package com.json.php; import android.app.activity; import android.os.bundle; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import android.widget.textview; import org.apache.http.httpresponse; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonexception; import org.json.jsonobject; public class jsonexampleactivity extends activity { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://127.0.0.1/test.php"); textview textview = (textview)findviewbyid(r.id.textview1); try { httpresponse response = httpclient.execute(httppost); string jsonresult = inputstreamtostring(response.getentity().getcontent()).tostring(); jsonobject object = new jsonobject(jsonresult); string name = object.getstring("name"); string verion = object.getstring("version"); textview.settext(name + " - " + verion); } catch (jsonexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } private stringbuilder inputstreamtostring(inputstream is) { string rline = ""; stringbuilder answer = new stringbuilder(); bufferedreader rd = new bufferedreader(new inputstreamreader(is)); try { while ((rline = rd.readline()) != null) { answer.append(rline); } } catch (ioexception e) { e.printstacktrace(); } return answer; } }
i tried both http://127.0.0.1/test.php , http://localhost/test.php
in httppost, doesn't works. checked php file on browser , gives result fine. going wrong? working on mac, anyway related problem because of permission issues?
just take @ page : http://developer.android.com/tools/devices/emulator.html#networkaddresses
you'll notice refer to:
10.0.2.2
special alias host loopback interface (i.e., 127.0.0.1 on development machine)127.0.0.1
emulated device's own loopback interface
hope answered , solved problem :)
Comments
Post a Comment