0

below is the code snippet of my app trying to send the data to my php address.. It comes up with an error though. Ans when I check on my php server, it does not show up.. Here is the code snippet below:

 public void sendData() {
    String ip = getIpAddress();
 HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("http://mysamplephp.php?ip=" + ip + "&date=" + formattedDate + "&appname=amigosmexican"+ "&appid="+ android_id);
    try{
        List<BasicNameValuePair> deviceinfo = new ArrayList<BasicNameValuePair>(3);
         deviceinfo.add(new BasicNameValuePair("id", android_id));
         deviceinfo.add(new BasicNameValuePair("date", formattedDate));
         deviceinfo.add(new BasicNameValuePair("ip", ip));
         httppost.setEntity(new UrlEncodedFormEntity(deviceinfo));  

         HttpResponse response = httpclient.execute(httppost);

         InputStream is = response.getEntity().getContent();
         BufferedInputStream bis = new BufferedInputStream(is);
         ByteArrayBuffer baf = new ByteArrayBuffer(20);
         Log.i("postData", response.toString());
         int current = 0;

         while((current = bis.read()) != -1){
             baf.append((byte)current);
         }



    } catch (ClientProtocolException e) {

    } catch (IOException e) {
        Log.e("log_tag", "Error in http connection ",e);
         }
    }

}

The error is :

 10-12 11:27:21.484: E/log_tag(733): Error in http connection 
10-12 11:27:21.484: E/log_tag(733): java.net.UnknownHostException: ilyushin.ph
10-12 11:27:21.484: E/log_tag(733):     at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
10-12 11:27:21.484: E/log_tag(733):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
10-12 11:27:21.484: E/log_tag(733):     at java.net.InetAddress.getAllByName(InetAddress.java:256)
10-12 11:27:21.484: E/log_tag(733):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
10-12 11:27:21.484: E/log_tag(733):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-12 11:27:21.484: E/log_tag(733):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-12 11:27:21.484: E/log_tag(733):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
10-12 11:27:21.484: E/log_tag(733):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-12 11:27:21.484: E/log_tag(733):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-12 11:27:21.484: E/log_tag(733):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-12 11:27:21.484: E/log_tag(733):     at com.mexican.recipes.Splash.sendData(Splash.java:144)
10-12 11:27:21.484: E/log_tag(733):     at com.mexican.recipes.Splash$PostTask.doInBackground(Splash.java:124)
10-12 11:27:21.484: E/log_tag(733):     at com.mexican.recipes.Splash$PostTask.doInBackground(Splash.java:1)
10-12 11:27:21.484: E/log_tag(733):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-12 11:27:21.484: E/log_tag(733):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
10-12 11:27:21.484: E/log_tag(733):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
10-12 11:27:21.484: E/log_tag(733):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
10-12 11:27:21.484: E/log_tag(733):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
10-12 11:27:21.484: E/log_tag(733):     at java.lang.Thread.run(Thread.java:1019)

EDIT: snippet for getting external ip:

 public String getIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
    NetworkInterface intf = en.nextElement();
    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
        InetAddress inetAddress = enumIpAddr.nextElement();
        if (!inetAddress.isLoopbackAddress()) {
            return inetAddress.getHostAddress().toString();
        }
    }
}
} catch (SocketException ex) {
Log.e("LOG", ex.toString());
}
return null;
}
omi0301
  • 473
  • 2
  • 5
  • 15
  • The url is not valid. (line 4). You need a hostname. – MiniGod Oct 12 '12 at 04:23
  • `ilyushin.ph` does not exist, which is clearly stated in the 2nd line of error output... – Marc B Oct 12 '12 at 04:24
  • @Marc B - i see. assuming the URL is valid, my code is OK? or my code is correct? – omi0301 Oct 12 '12 at 04:27
  • Have you added permission for INTERNET in your AndroidManifest.xml file? – VendettaDroid Oct 12 '12 at 04:55
  • Yes I have.. I think I figured out what the problem is.. The URL is valid. its just that my emulator cant connect to the internet. I tried it on my phone.. However, I think I am not getting the external IP address. I need the external IP address. Please see edit above . that is my snippet on how i get the external IP. – omi0301 Oct 12 '12 at 05:08

1 Answers1

0

when you are passing parameters in List params then no need to write them in url,

try your code this way,

public void sendData() {
String ip = getIpAddress();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mysamplephp.php");
try{
    List<BasicNameValuePair> deviceinfo = new ArrayList<BasicNameValuePair>(3);
     deviceinfo.add(new BasicNameValuePair("id", android_id));
     deviceinfo.add(new BasicNameValuePair("date", formattedDate));
     deviceinfo.add(new BasicNameValuePair("ip", ip));
     httppost.setEntity(new UrlEncodedFormEntity(deviceinfo));  

     HttpResponse response = httpclient.execute(httppost);

     InputStream is = response.getEntity().getContent();
     BufferedInputStream bis = new BufferedInputStream(is);
     ByteArrayBuffer baf = new ByteArrayBuffer(20);
     Log.i("postData", response.toString());
     int current = 0;

     while((current = bis.read()) != -1){
         baf.append((byte)current);
     }



} catch (ClientProtocolException e) {

} catch (IOException e) {
    Log.e("log_tag", "Error in http connection ",e);
     }
}

Try this, it will work. For more details see this demo code

my wordepress blog

Rahul Upadhyay
  • 3,493
  • 2
  • 21
  • 37
  • thanks for your answer. BUt what if i want to use this http://xxxxxxxxxxxxxxxxxxxxxxx.php?ip=" + ip + "&date=" + formattedDate + "&appname=amigosmexican"+ "&appid="+ android_id? SO i no longer need to make a list? – omi0301 Oct 12 '12 at 05:44
  • i think its with the ip address i am trying to get. do you have any way of getting the external ip? because my external ip is 116.x.x.x. However, on my php server, it shows 192.x.x.x. – omi0301 Oct 12 '12 at 05:52
  • if you are using parameters within url then no need to add List params – Rahul Upadhyay Oct 12 '12 at 06:04
  • and which ip address you want to get? Device running on or other? refer [http://stackoverflow.com/questions/6064510/how-to-get-ip-address-of-the-device] this – Rahul Upadhyay Oct 12 '12 at 06:04
  • the external ip address of the device – omi0301 Oct 12 '12 at 06:09