0

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

 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)

编辑:获取外部 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

1 回答 1

0

当您在 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);
     }
}

试试这个,它会工作的。有关更多详细信息,请参阅此演示代码

我的 wordepress 博客

于 2012-10-12T05:29:37.807 回答