【问题标题】:Error with sending data from android to php将数据从android发送到php时出错
【发布时间】:2012-10-12 04:22:26
【问题描述】:

下面是我的应用程序的代码 sn-p 试图将数据发送到我的 php 地址。但它出现了一个错误。 Ans 当我检查我的 php 服务器时,它没有显示出来。这是下面的代码 sn-p:

 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);
         }
    }

}

错误是:

 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)

编辑: sn-p 获取外部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;
}

【问题讨论】:

  • 网址无效。 (第 4 行)。你需要一个主机名。
  • ilyushin.ph不存在,在错误输出的第2行明确说明...
  • @Marc B - 我明白了。假设 URL 有效,我的代码可以吗?还是我的代码是正确的?
  • 您是否在 AndroidManifest.xml 文件中添加了 INTERNET 权限?
  • 是的,我有.. 我想我知道问题所在了.. URL 是有效的。只是我的模拟器无法连接到互联网。我在手机上试过了。但是,我想我没有得到外部 IP 地址。我需要外部 IP 地址。请参阅上面的编辑。这是我如何获得外部 IP 的 sn-p。

标签: php android http http-post


【解决方案1】:

当您在 List params 中传递参数时,无需将它们写在 url 中,

以这种方式尝试您的代码,

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);
     }
}

试试这个,它会起作用的。更多详情请查看此演示代码

my wordepress blog

【讨论】:

  • 感谢您的回答。但是,如果我想使用这个xxxxxxxxxxxxxxxxxxxxxxx.php?ip=" + ip + "&date=" + formattedDate + "&appname=amigosmexican"+ "&appid="+ android_id 怎么办?所以我不再需要列出清单了吗?
  • 我认为它与我想要获取的 ip 地址有关。你有什么办法获取外部IP吗?因为我的外部 ip 是 116.x.x.x。但是,在我的 php 服务器上,它显示 192.x.x.x.
  • 如果您在 url 中使用参数,则无需添加列表参数
  • 你想获取哪个IP地址?设备运行在或其他?参考[stackoverflow.com/questions/6064510/…这个
  • 设备的外网ip地址
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2021-05-06
  • 2019-08-07
相关资源
最近更新 更多